Project Ramon

A learning journey from a Ruby noob perspective

Today I’m Reading About: Rails Generators Part[2]

generators2_header_img

Hello!

In the last post, we covered a quick introduction in getting comfortable with generators in rails. We covered a way for us to determine command and options available to a specific generator, determined where we should place any of our custom generators and also used the rails generate to actually generate our generator.

Today I’ll be covering chapter 3 in the Rails docs: creating generators with generators.

Generators: Checking Out the Scene

At the end of my last post I shared a couple of videos from Ryan Bates about generators in rails. He covers a few things of interest, one of them being how to change required arguments into optional ones. Check out the end of my last post to find the video links.

Taking a stroll through the RubyDocs for Rails generators shed some light on the few modules and many classes that make up all that rails provides as far as code generators go. Its quite a list and some generators of note (i.e. generators you’ve probably used before) are ActiveModel, ControllerGenerator, MigrationGenerator, etc..

We can see for instance, that the ModelGenerator inherits several methods from the NamedBase class and includes methods from the Actions module.

Here is a quick peak, and you can head straight to the docs using this link:

generators2_img1

Creating Generators with Generators

I should start out by saying that I don’t understand this portion of the documentation. It appears that we created an InitializerGenerator which inherits from Rails::Generators::Base in the prior chapter 2 Creating Your First Generator. Is 3 Creating Generators with Generators a different way of performing the same task as in Chapter 2?

Here is the portion of the docs I’m referring to:

generators2_img2

In any case, we can add a description in our generator file like so:

class MyFirstGenerator < Rails::Generators::Base
  # we can add the following to add a better description for our generator
  desc "This generator creates my_first_template file at lib/generators/templates"    

  def say_hello
    "Hello!"
  end

  def create_template_file
    create_file "lib/generators/templates/my_first_generator.erb"
  end
end

Generators are set up to execute each method one right after another that we define inside our generator class. If we’d like to create methods that aren’t executed when we run rails generate my_first_template in our terminal, we can just restrict them to private like so:

class MyFirstGenerator < Rails::Generators::Base
  include Naming

  # we can add the following to add a better description for our generator
  desc "This generator creates my_first_template file at lib/generators/templates"    

  def say_hello
    "Hello! #{first_name}"
  end

  def create_template_file
    create_file "lib/generators/templates/my_first_generator.erb"
  end

  private
  def first_name
    # first name stuff here
  end
end

Now first_name will not be called automatically when we run the rails g my_first_generator command.

Another insightful thing I learned from Ryan Bate’s videos were that we can change required commands into options. For instance, when we see our console output something like so:

generators2_img3

We see where our description would have displayed at the bottom of the image. If we look for the line Usage: in the middle of the image underneath the line rails g rspec:install --help, we’ll see the required argument rspec:install followed by a list of options which are listed underneath.

We can define the parameters that our generator takes by using the argument method. I planned on showing which class in Rails::Generators that argument comes from, but could not find it in Rubydoc in the method list. If any of you know where this comes from feel free to share in a comment!

Here is a quick example I found from this RailsCasts however, and the argument method can be used like so:

class LayoutGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  argument(:layout_name, type: :string, default: "application")
end

Conclusion

I will more than likely need a little more practice with generators to get a better feel for using them to improve my workflow. Its been an enjoyable process learning about them however. I hope that this has been informative for you as well.

Stay tuned…

Advertisements

Categories: Ruby on rails

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s