LoginSignup
192
181

More than 5 years have passed since last update.

rails newしたらconfig.generatorsをまず設定しよう

Last updated at Posted at 2014-03-14

rails gで不要なファイルが生成される・・・

Railsで開発する際に、rails gコマンドは欠かせないと思います。
しかし、特にscaffoldやcontrollerの生成時には不要なファイルが生成される場合があります。
例えば、apiを提供するアプリケーションにjsやcssのファイルは必要ありません。

config.generators

application.rbのconfig.generatorsでrails gで生成されるファイルを設定できます!

例えば、

  • apiだからjsとかcssとかいらないな
  • view_helperもいらないな
  • viewのテンプレートエンジンもひとまずなしで
  • viewとview_helperのテストはなくていい
  • fixtureはfactory_girlがいいな。

であれば、こんな感じで設定します

application.rb
config.generators do |g|
  g.stylesheets false
  g.javascripts false
  g.helper false
  g.template_engine false 
  g.test_framework :rspec, view_specs: false, helper_specs: false, fixture: true
  g.fixture_replacement :factory_girl, dir: "spec/support/factories"
end

実行結果はこんな感じ。

> rails g controller hoge/users index
create  app/controllers/hoge/users_controller.rb
 route  get "users/index"
invoke  rspec
create    spec/controllers/hoge/users_controller_spec.rb
invoke  assets
invoke    js
invoke    css

もちろん、テンプレートエンジンにerbを使いたければ

application.rb
  g.template_engine :erb

と書けば生成されます。

この設定を使っているプロジェクトはテンプレートエンジンとしてrablを使用しているのですが、rablはどうも認識されないもよう・・・

config.generatorsを設定してスマートなシステムを作りましょう!

さらに詳しい説明

ruby/rails/RailsGuidesをゆっくり和訳してみたよ/Creating and Customizing Rails Generators

192
181
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
192
181