hook_for
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-38)
- 3.1.0 (-1)
- 3.2.1 (8)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-11)
- 4.1.8 (0)
- 4.2.1 (-1)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (3)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (4)
- 7.1.3.4 (0)
- What's this?
hook_for(*names, &block)
public
Invoke a generator based on the value supplied by the user to the given option named “name”. A class option is created when this method is invoked and you can set a hash to customize it.
Examples
module Rails::Generators class ControllerGenerator < Base hook_for :test_framework, aliases: "-t" end end
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: :in and :as.
Let’s suppose you are creating a generator that needs to invoke the controller generator from test unit. Your first attempt is:
class AwesomeGenerator < Rails::Generators::Base hook_for :test_framework end
The lookup in this case for test_unit as input is:
"test_unit:awesome", "test_unit"
Which is not the desired lookup. You can change it by providing the :as option:
class AwesomeGenerator < Rails::Generators::Base hook_for :test_framework, as: :controller end
And now it will lookup at:
"test_unit:controller", "test_unit"
Similarly, if you want it to also lookup in the rails namespace, you just need to provide the :in value:
class AwesomeGenerator < Rails::Generators::Base hook_for :test_framework, in: :rails, as: :controller end
And the lookup is exactly the same as previously:
"rails:test_unit", "test_unit:controller", "test_unit"
Switches
All hooks come with switches for user interface. If you do not want to use any test framework, you can do:
rails generate controller Account --skip-test-framework
Or similarly:
rails generate controller Account --no-test-framework
Boolean hooks
In some cases, you may want to provide a boolean hook. For example, webrat developers might want to have webrat available on controller generator. This can be achieved as:
Rails::Generators::ControllerGenerator.hook_for :webrat, type: :boolean
Then, if you want webrat to be invoked, just supply:
rails generate controller Account --webrat
The hooks lookup is similar as above:
"rails:generators:webrat", "webrat:generators:controller", "webrat"
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.
hook_for :resource_controller do |instance, controller| instance.invoke controller, [ instance.name.pluralize ] end