Rails::Generator is a code generation platform tailored for the Rails web application framework. Generators are easily invoked within Rails applications to add and remove components such as models and controllers. New generators are easy to create and may be distributed as RubyGems, tarballs, or Rails plugins for inclusion system-wide, per-user, or per-application.
For actual examples see the rails_generator/generators directory in the Rails source (or the railties
directory if you have frozen the Rails source
in your application).
Generators may subclass other generators to provide variations that require little or no new logic but replace the template files.
For a RubyGem, put your generator class and templates in the
lib
directory. For a Rails plugin,
make a generators
directory at the root of your plugin.
The layout of generator files can be seen in the built-in
controller
generator:
generators/ controller/ controller_generator.rb templates/ controller.rb functional_test.rb helper.rb view.rhtml
The directory name (controller
) matches the name of the
generator file (controller_generator.rb) and class
(ControllerGenerator
). The files that will be copied or used
as templates are stored in the templates
directory.
The filenames of the templates don’t matter, but choose something that will
be self-explatatory since you will be referencing these in the
manifest
method inside your generator subclass.
- MODULE Rails::Generator::Commands
- MODULE Rails::Generator::Lookup
- MODULE Rails::Generator::Options
- MODULE Rails::Generator::Scripts
- CLASS Rails::Generator::Base
- CLASS Rails::Generator::GemSource
- CLASS Rails::Generator::GeneratedAttribute
- CLASS Rails::Generator::GeneratorError
- CLASS Rails::Generator::Manifest
- CLASS Rails::Generator::NamedBase
- CLASS Rails::Generator::PathSource
- CLASS Rails::Generator::Source
- CLASS Rails::Generator::Spec
- CLASS Rails::Generator::UsageError