Rails Generators

rails generate(or rails g) by itself gives a list of available generators:

$ rails generate
Usage: rails generate GENERATOR [args] [options]
...........
Rails:
assets
channel
controller
generator
helper
integration_test
jbuilder
job
mailer
migration
model
resource
responders_controller
scaffold
scaffold_controller
task
.............
The main generators that rails offers are:
  • controller: The Rails controller coordinates the interaction between the user, the views, and the model.
  • helper: Code in helpers is included in the corresponding view. We can move big loops, method calls or other logic into a method in the helper.
  • mailer: It allows for sending and receiving emails from and to your application.
  • migration: Rails Migration allows you to use Ruby to define changes to your database schema.
  • model: It is Ruby class that talk to the database, store and validate data, perform the business logic.
  • scaffold: It refers to the auto-generation of a simple set of a model, views and controller usually for a single table.
Following is the list of options, which can be used along with generators:

-h, [–help] # Print generator’s options and usage -p, [–pretend] # Run but do not make any changes -f, [–force] # Overwrite files that already exist -s, [–skip] # Skip files that already exist -q, [–quiet] # Suppress status output

Generators

You can create different generators by running rails g in the terminal. Let’s have a look at each of them.
  • rails generate model ModellName

It generates the model and the associated database table. For example, we can add a new model to the app called User with fields name and age as,

Eg:  rails g model User name: string age: integer

  • rails generate controller ListController show edit

It generates controller. It is used if you are creating static views or non-CRUD related features. Let’s create a user controller that will manage the data flow and view for each users.

Eg:  rails g controller users

  • rails generate scaffold ModelName ControllerName

Generates Scaffold. Let’s create a scaffold User with edit and view actions.

Eg:  rails g scaffold User edit view

  • rails generate migration AddNewTable

Generates Table to migrate. We can create migration for adding a table as

Eg:  rails g migration User

  • rails generate plugin PluginName

Generates Plugin.

Eg:  rails g plugin rails_plugin

  • rails generate integration_test TestName

Generates Integration Test.

Eg:  rails generate integration_test User

  • rails generate session_migration

Generates Session Migration.

Creating Generators with Generators

Generators themselves have a generator:
$ 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
This is the generator just created:
class InitializerGenerator < Rails::Generators::NamedBase
  source_root File.expand_path("../templates", __FILE__)
end

First, notice that we are inheriting from Rails::Generators::NamedBase instead of Rails::Generators::Base. This means that our generator expects at least one argument, which will be the name of the initializer.

Customizing Rails generators

We can customize it in, config/application.rb
config.generators do |g|
  g.orm :active_record
  g.template_engine :erb
  g.test_framework :test_unit, fixture: false
  g.stylesheets false
  g.javascripts false
end
So by doing the above steps will stop generating stylesheet, JavaScript, and test fixture files. We can avoid generation of unwanted files from doing these steps. I hope this article help you in some way to know about rails generators.

References

]]>