The Generators
The rails generate
command is famously part of the rails generators program
Rails includes a lot of default generators and most notably the rails new
allows us to create new rails applications.
You can access the list of all default generators by going to a rails application and typing rails generate
, and you will see that the following 23 generators are divided into 3 categories:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Rails: application_record assets channel controller generator helper integration_test jbuilder job mailbox mailer migration model resource scaffold scaffold_controller system_test task ActiveRecord: active_record:application_record TestUnit: test_unit:channel test_unit:generator test_unit:mailbox test_unit:plugin |
For more information about a generators, just run rails create my_generator_name --help
How Does it Work?
In the Rails Repo you can find the code for rails generators in the rails/generators
directory:
Do you remember this generators list from the list above? There is a group of 18 generators and if you open the path file as shown in the screenshot, you can check the code for these generators (model, controller, scaffold, etc.).
I’m not going to explain how each generators work, all differently, but I noticed a few things:
- As the Rails documentation says, when a generator is called, each public method in the generator is executed in the sequence in which it was defined.
- Generators built with Thor . If you look at the
base.rb
class, you will see the class inherits from Thor. Document rails announces that Thor provides powerful options for parsing and an excellent API for manipulating files. - We can create our own custom generators – see how this works in the next section.
- Alternatively, we can disable the default behavior of any generator by going to
config/application.rb
, adding options inside the config.generators block. For example:
1 2 3 4 5 | # This will disable the generation of the stylesheets files config.generators do |g| g.scaffold_stylesheet false end |
You Can Create Custom Generators
If the default rails generators aren’t enough for you, you can create your own. For example, to create a new service rails g or to create a new generators set rails g initializer
, etc.
Rails generate generator Do you remember this generators list that we saw at the beginning of the article? Yes, Rails has a default generators that allow you to create your own!
Generate an initializer generator?
1 2 3 4 5 6 7 8 | rails generate generator initializer create lib/generators/initializer create lib/generators/initializer/initializer_generator.rb create lib/generators/initializer/USAGE create lib/generators/initializer/templates invoke test_unit create test/lib/generators/initializer_generator_test.rb |
Create the directory lib / generators / initializer. Create the initializer_generator.rb
file inheriting from Rails::Generators::NamedBase
. As Rails doc said, this means our generators expect at least one argument, which will be the name of the generators and will be available in our code as variable name.
The source_root
method points to where our generators templates will be located. Create the USAGE file, it will actually document the custom generator:
When I run rails generate initializer --help
in terminal, see the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Usage: rails generate initializer NAME [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications) Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist Description: Explain the generator Example: Hello from the initializer doc This will create: a/new/initializer |
We create the directory lib / generators / initializer / templates. This directory will contain the basic template for your custom generator. Finally, create the file initializer_generator_test.rb for our tests.
Last word
If you want to know more about how to create custom builders, check out this tutorial and the official Rails documentation. Hope you learned something. Let me know your feedback, I’d love to improve the post! Thank you!