Class: Rails::Generators::Base
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
ActiveRecord::Generators::Base, ActiveRecord::Generators::MigrationGenerator, ActiveRecord::Generators::ModelGenerator, Css::Generators::AssetsGenerator, Css::Generators::ScaffoldGenerator, Erb::Generators::Base, Erb::Generators::ControllerGenerator, Erb::Generators::MailerGenerator, Erb::Generators::ScaffoldGenerator, Js::Generators::AssetsGenerator, Rails::Generators::AppBase, Rails::Generators::AppGenerator, Rails::Generators::AssetsGenerator, Rails::Generators::ControllerGenerator, Rails::Generators::GeneratorGenerator, Rails::Generators::HelperGenerator, Rails::Generators::IntegrationTestGenerator, Rails::Generators::JobGenerator, MailerGenerator, Rails::Generators::MigrationGenerator, Rails::Generators::ModelGenerator, NamedBase, Rails::Generators::PluginGenerator, Rails::Generators::ResourceGenerator, Rails::Generators::ResourceRouteGenerator, Rails::Generators::ScaffoldControllerGenerator, Rails::Generators::ScaffoldGenerator, Rails::Generators::TaskGenerator, TestUnit::Generators::Base, TestUnit::Generators::ControllerGenerator, TestUnit::Generators::GeneratorGenerator, TestUnit::Generators::HelperGenerator, TestUnit::Generators::IntegrationGenerator, TestUnit::Generators::JobGenerator, TestUnit::Generators::MailerGenerator, TestUnit::Generators::ModelGenerator, TestUnit::Generators::PluginGenerator, TestUnit::Generators::ScaffoldGenerator
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Thor::Group
|
|
Instance Chain:
self,
Actions,
Thor::Actions,
Thor::Group
|
|
Inherits: |
Thor::Group
|
Defined in: | railties/lib/rails/generators/base.rb |
Class Method Summary
-
.base_root
Returns the base root for a common set of generators.
-
.default_source_root
Returns the default source root for a given generator.
-
.desc(description = nil)
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.
-
.hide!
Convenience method to hide this generator from the available ones when running rails generator command.
-
.hook_for(*names, &block)
Invoke a generator based on the value supplied by the user to the given option named “name”.
-
.namespace(name = nil)
Convenience method to get the namespace from the class name.
-
.remove_hook_for(*names)
Remove a previously added hook.
-
.source_root(path = nil)
Returns the source root for this generator using default_source_root as default.
Instance Method Summary
Actions - Included
#add_source | Add the given source to |
#after_bundle | Registers a callback to be executed after bundle and spring binstubs have run. |
#application | Alias for Actions#environment. |
#capify! | Just run the capify command in root. |
#environment | Adds a line inside the ::Rails::Application class for |
#gem | Adds an entry into |
#gem_group | Wraps gem entries inside a group. |
#generate | Generate something using a generator from ::Rails or a plugin. |
#git | Run a command in git. |
#initializer | Create a new initializer with the provided code (either in a block or a string). |
#lib | Create a new file in the lib/ directory. |
#rake | Runs the supplied rake task. |
#rakefile | Create a new |
#readme | Reads the given file at the source root and prints it in the console. |
#route | Make an entry in ::Rails routing file |
#vendor | Create a new file in the |
Class Method Details
.base_root
Returns the base root for a common set of generators. This is used to dynamically guess the default source root.
# File 'railties/lib/rails/generators/base.rb', line 217
def self.base_root File.dirname(__FILE__) end
.default_source_root
Returns the default source root for a given generator. This is used internally by rails to set its generators source root. If you want to customize your source root, you should use source_root.
.desc(description = nil)
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.
.hide!
Convenience method to hide this generator from the available ones when running rails generator command.
# File 'railties/lib/rails/generators/base.rb', line 50
def self.hide! Rails::Generators.hide_namespace self.namespace end
.hook_for(*names, &block)
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
# File 'railties/lib/rails/generators/base.rb', line 163
def self.hook_for(*names, &block) = names. in_base = .delete(:in) || base_name as_hook = .delete(:as) || generator_name names.each do |name| unless .key?(name) defaults = if [:type] == :boolean { } elsif [true, false].include?(default_value_for_option(name, )) { banner: "" } else { desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" } end class_option(name, defaults.merge!( )) end hooks[name] = [ in_base, as_hook ] invoke_from_option(name, , &block) end end
.namespace(name = nil)
Convenience method to get the namespace from the class name. It's the same as Thor default except that the Generator at the end of the class is removed.
# File 'railties/lib/rails/generators/base.rb', line 43
def self.namespace(name=nil) return super if name @namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':') end
.remove_hook_for(*names)
Remove a previously added hook.
remove_hook_for :orm
# File 'railties/lib/rails/generators/base.rb', line 189
def self.remove_hook_for(*names) remove_invocation(*names) names.each do |name| hooks.delete(name) end end
.source_root(path = nil)
Returns the source root for this generator using default_source_root as default.
# File 'railties/lib/rails/generators/base.rb', line 23
def self.source_root(path=nil) @_source_root = path if path @_source_root ||= default_source_root end