In our day to day web-application development, we use different Rails built in generators like,

rails g controller users rails g model User ...

Look how many files are generated when you create a controller.

So basically, it will create

View helper file javascript file in assets stylesheet file in assets test files in test directory

They have some default types and extensions. like,

Script file is always coffee script Test cases is default to Test::Unit. CSS is default to scss type. HTML template is always erb.

In this blog post, we are going to customise all these defaults and will get some overview of each generator’s customisation one by one.

To customise this generators you have to make changes in config/application.rb. For example, to use js as default javascript engine

config.generators.javascript_engine = :js

If there are multiple customisation options to override then you can use block like given below,

config.generators do |g|

g.stylesheets false

g.javascripts false

end

Don’t worry about the code inside, we will go through different config options now.

javascript_engine

You can use this to change the default type of JS file which is coffee-script by default. For example, if you don’t want to use coffeescript then add below line in your application.rb

config.generators.javascript_engine = :js

test_framework

If you have Rspec already installed in your application and now you want to change the test framework to use Test::Unit without removing Rspec then just add below line.

config.generators.test_framework :test_unit

If you don’t want to generate test files for views then just add views: false in above config.

stylesheets

If you don’t want to create a stylesheet file everytime while creating a controller just set this value to false.

config.generators.stylesheets false

javascripts

Same as we did for stylesheet, can turn off creating the JS files for each controller

config.generators.javascripts false

template_engine

You can customize your template engine for haml and slim instead of Ruby.

config.generators.template_engine :erb

ORM

You can also change the ORM Mapping for active_record with some other options.

config.generators.orm :active_record

Here is how I customise my generators

config.generators do |g|

g.test_framework :rspec, views: false

g.javascript_engine :js

g.stylesheets false

end

and if you try again generating controller with above config,

You can see that it creates Rspec files for test cases, normal JS file instead of coffee script and it also skips creating a CSS file.

We can create our own generators in Rails using generators, will see in the next post. So stay in touch.!!