What I don’t know about the command “rails generate”

Tram Ho

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:

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:

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?

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:

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!

Share the news now

Source : Viblo