Examples

The example above will create a test framework option and will invoke a
generator based on the user supplied value.

For example, if the user invoke the controller generator as:

railsgeneratecontrollerAccount--test-framework=test_unit

The controller generator will then try to invoke the following generators:

"rails:test_unit","test_unit:controller","test_unit"

Notice that “rails:generators:test_unit” could be loaded as well, what
Rails looks for is the first and last parts of
the namespace.
This is what allows any test framework to hook into Rails as long as it provides any of the hooks above.

Options

The first and last part used to find the generator to be invoked are
guessed based on class invokes hook_for, as noticed in the example above.
This can be customized with two options: :base and :as.

Let’s suppose you are creating a generator that needs to invoke the
controller generator from test unit. Your first attempt is:

Custom invocations

You can also supply a block to hook_for to
customize how the hook is going to be invoked. The block receives two
arguments, an instance of the current class and the class to be invoked.

For example, in the resource generator, the controller should be invoked
with a pluralized class name. But by default it is invoked with the same
name as the resource generator, which is singular. To change this, we can
give a block to customize how the controller can be invoked.

# File railties/lib/rails/generators/base.rb, line 163def self.hook_for(*names,&block)options=names.extract_options!in_base=options.delete(:in)||base_nameas_hook=options.delete(:as)||generator_namenames.eachdo|name|unlessclass_options.key?(name)defaults=ifoptions[:type]==:boolean{}elsif[true,false].include?(default_value_for_option(name,options)){banner:""}else{desc:"#{name.to_s.humanize} to be invoked",banner:"NAME"}endclass_option(name,defaults.merge!(options))endhooks[name]=[in_base,as_hook]invoke_from_option(name,options,&block)endend