LoginSignup
17
22

More than 5 years have passed since last update.

rails g実行時に自動作成したくないものの設定を作成する方法

Last updated at Posted at 2018-06-07

rails g scaffold sampleなどのrails gコマンドは一瞬で機能ができてしまう優れものだが、いらないファイルまで作りがちになってしまうのが玉に瑕なところ。

xxxxxxxxxxxxxxxxxxBook-Air:quasi-case-exam xxxxxxxxxxx$ rails g scaffold sample
Running via Spring preloader in process 12333
      invoke  active_record
      create    db/migrate/20180607231739_create_samples.rb
      create    app/models/sample.rb
      invoke  resource_route
       route    resources :samples
      invoke  scaffold_controller
      create    app/controllers/samples_controller.rb
      invoke    haml
      create      app/views/samples
      create      app/views/samples/index.html.haml
      create      app/views/samples/edit.html.haml
      create      app/views/samples/show.html.haml
      create      app/views/samples/new.html.haml
      create      app/views/samples/_form.html.haml
      invoke    helper
      create      app/helpers/samples_helper.rb
      invoke    jbuilder
      create      app/views/samples/index.json.jbuilder
      create      app/views/samples/show.json.jbuilder
      create      app/views/samples/_sample.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/samples.coffee
      invoke    scss
      create      app/assets/stylesheets/samples.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

この時、config/application.rbに設定を追記すればこの自動生成を制御することができる。

例えば

application.rb
require_relative 'boot'

(省略)

module MyPreset
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1
    config.i18n.default_locale = :ja
    config.time_zone = 'Tokyo'
    config.generators do |g|
      g.assets false
      g.helper false
      g.jbuilder false
    end

(省略)

    config.time_zone = 'Tokyo'
    config.active_record.default_timezone = :local
  end
end

のように、

application.rb
    config.generators do |g|
      g.assets false
      g.helper false
      g.jbuilder false
    end

と書けば、

xxxxxxxxxxxxxxMacBook-Air:quasi-case-exam xxxxxxxxx$ rails g scaffold sample
Running via Spring preloader in process 13362
      invoke  active_record
      create    db/migrate/20180607234014_create_samples.rb
      create    app/models/sample.rb
      invoke  resource_route
       route    resources :samples
      invoke  scaffold_controller
      create    app/controllers/samples_controller.rb
      invoke    haml
      create      app/views/samples
      create      app/views/samples/index.html.haml
      create      app/views/samples/edit.html.haml
      create      app/views/samples/show.html.haml
      create      app/views/samples/new.html.haml
      create      app/views/samples/_form.html.haml
xxxxxxxxxxxxxxxxxxxxxxxxacBook-Air:quasi-case-exam shibatadaiki$

このように、指定した部分の自動生成を抑えることができる。

で、その指定する部分(の、記述方法)はどうやって知れば良いのか?というと、invokeの部分と階層を見て知ることが(おそらく)できる。

例えば、Controllerコマンドで作成される部分に着目した場合、rails g scaffold実行時のログは

      invoke  scaffold_controller
      create    app/controllers/samples_controller.rb
      invoke    haml
      create      app/views/samples
      create      app/views/samples/index.html.haml
      create      app/views/samples/edit.html.haml
      create      app/views/samples/show.html.haml
      create      app/views/samples/new.html.haml
      create      app/views/samples/_form.html.haml

のように、invoke scaffold_controllerとあり、その下の階層にcontrollerとhamlのview群などが来ているので

application.rb
(省略)

    config.generators do |g|
      g.scaffold_controller false

(省略)

と追記すれば、

xxxxxxxxxxxxxxxcBook-Air:quasi-case-exam xxxxxxxxxx$ rails g scaffold sample
Running via Spring preloader in process 13530
      invoke  active_record
      create    db/migrate/20180607234137_create_samples.rb
      create    app/models/sample.rb
      invoke  resource_route
       route    resources :samples
xxxxxxxxxxxxxMacBook-Air:quasi-case-exam sh

このように、cotroller以下で作成されるファイル群を制御することができる。

17
22
1

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
17
22