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:

rails generate controller Account --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 klass 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.