Reduce Application Clutter, Disable Unwanted Rails Generators

Disable rails generators

Rails has some handy generators to help streamline the creation of models, views, controllers, and a bunch of other goodies.

~/dev/kitten_factory $ rails g controller Kittens
  create  app/controllers/kittens_controller.rb
  invoke  erb
  create    app/views/kittens
  invoke  helper
  create    app/helpers/kittens_helper.rb
  invoke  assets
  invoke    coffee
  create      app/assets/javascripts/
  invoke    scss
  create      app/assets/stylesheets/kittens.css.scss

Unfortunately, over time the unused files that are created (and not deleted) add clutter to your application.

~/dev/kitten_factory $ wc -l app/helpers/*
       2 app/helpers/allergies_helper.rb
      14 app/helpers/application_helper.rb
       2 app/helpers/invitations_helper.rb
       # lots of unused helper files
       # ...
       2 app/helpers/trainers_helper.rb
       2 app/helpers/vets_helper.rb
     194 total # 178 lines of unused application code

Luckily with a small adjustment to your application environment we can avoid the generation of unnecessary files.

# config/application.rb
module KittenFactory
  class Application < Rails::Application
    # Disable generation of helpers, javascripts, css, and view specs
    config.generators do |generate|
      generate.helper false
      generate.assets false
      generate.view_specs false

    # ...

This allow us to tailor the generators to only create the files we need.

~/dev/kitten_factory $ rails g controller Users
      create  app/controllers/users_controller.rb
      invoke  erb
      create    app/views/users
      invoke  assets
      invoke    scss

If you use Suspenders, this and more will be automatically configured for you.


Sharpen your programming skills by completing coding exercises that are reviewed by other developers at Upcase today.