Search code examples
ruby-on-railsrspec-rails

A good definition for what a Rails-Generator is?


What are Rails Generators?

Are they scripts, which are used to create files and/or boilerplate code?

I couldn't find a good definition for what they actually are ...

Generators aren't just provided by Rails itself? Gems like rspec can add Generators to Rails and after an install Rails has that additional Generators? Do I get that right?


Solution

  • Are they scripts, which are used to create files and/or boilerplate code?

    Yes, I think this is a pretty good definition. Here are two definitions from the Rails documentations (although I agree they're a bit vague).

    The rails generate command uses templates to create a whole lot of things

    Rails scaffolding is a quick way to generate some of the major pieces of an application.

    To summarise, a generator is usually a Rake task which does use a template file to create boilerplate code. This is helpful to quickly develop an application.

    Generators aren't just provided by Rails itself? Gems like rspec can add Generators to Rails and after an install Rails has that additional Generators? Do I get that right?

    Yes, if your gem is a Railties you can install additional generators.

    Rails::Railtie is the core of the Rails framework and provides several hooks to extend Rails and/or modify the initialization process.

    Here is for instance a template from RSpec.

    https://api.rubyonrails.org/classes/Rails/Railtie.html https://api.rubyonrails.org/classes/Rails/Generators.html