DEV Community

MikeKetterling
MikeKetterling

Posted on

Rails Generators

When Ruby on Rails is mentioned in conversation or in a learning curriculum its quickly referenced as an efficient way to code for developers. It was created to make a lot of the mundane and nitty gritty tasks automatic, so developers can get into the important logic within their application quicker. This is accomplished in rails by continuing to build upon other Ruby gems and refactoring code into a lot of reusable pieces. A major tool that rails uses is Generators. Generators can move code mountains for developers very quickly and very accurately.

There are generators that come with rail gems and logic that most rail developers will use pretty frequently, especially as developers start to learn ruby. These common generators help you create models, add columns to tables, create controllers, and many more. Then there are some generators that can create multiple pieces of large code or all the code items you need to run a full application. Generators like scaffold and reference can move these large mountains of code, but another very useful use of generators is the ability to create your own custom generator to use, or edit already existing ones.

After creating a rails application you can make edits within the lib/generators file to any generator that exists. You may be thinking to yourself why would I edit a generator? Well maybe you didn’t want to generate a specific file every time or you wanted an adjustment to that file, this can be accomplished within the generator specific file.

Along the same lines as editing existing generators in the generator specific file, we can create new generators by adding additional generator files and building out their functionality within those files. I’ll walk you through the basic steps of this so if you want to experiment a bit more on your own you are free to do so!

Creating a Generator

Let’s say you wanted a generator to create a file within your application. The first step would be to create a file within your lib directory, make or access a generators directory and inside that create your file for your generator. The file path would look like this: lib/generators/generatorName_generator.rb.

Your next step would be to then add logic to this file so that when the generator is called on it will act in the specific way you want it to. The syntax convention within this file is very important so I’ll provide an example:

class GeneratorNameGenerator < Rails::Generators::Base
    def create_file_in_app
        create_file “config/environments/new_file.rb”, “Add content here”
    end
end
Enter fullscreen mode Exit fullscreen mode

What is most important here is that our file name and class name line up so that later when we invoke the generator it is called on correctly. We also need to recognize that our class is inheriting from Rails::Generators:Base. This gives the correct functionality when this generator is called upon, running the methods inside the file sequentially in the order that is defined. In our single method we are invoking the create_file method that will create a file with the given content at the specified location inside of your app. Now to invoke this generator, all that needs to be ran in the terminal is:

$ rails g  generatorName
Enter fullscreen mode Exit fullscreen mode

A final step we want to make sure we take is to add a description of our generator within our file so that if we call;

$ rails g generatorName --help
Enter fullscreen mode Exit fullscreen mode

Our output will be a helpful description for future use, rather than traveling back in the file to see what the generator does. To add a description of the generator we’ll return back into our generator file and add the desc method like so:

class GeneratorNameGenerator < Rails::Generators::Base
    desc “This generator creates a file at config/environments”
    def create_file_in_app
        create_file “config/environments/new_file.rb”, “Add content here”
    end
end
Enter fullscreen mode Exit fullscreen mode

Now if we or any other developer wee to call on this generator with the help flag it would provide a brief description of the actions the generator would take.

Conclusion

This is just a quick example of the versatility and power of the Rails generators. There are many other use cases for code like this, and ultimately it makes your time coding much more efficient as long as you know you’ll be needing a custom generator for more than just one action. Tools like these also help keep your code DRY and error free, and at the end of the day error free and efficient code is the name of the game. Go have fun!

DIY – Create Your Own Rails Generator
Ruby on Rails - Generators
Creating and Customizing Rails Generators & Templates

Discussion (0)