class Rails::Railtie
Be sure to look at the documentation of those specific classes for more information.
used in both.Rails::Application is an engine, the same configuration described here can be
An engine is nothing more than a railtie with some initializers already set. And since
== Application and Engine
end
end
require ‘path/to/my_railtie_generator’
generators do
class MyRailtie < Rails::Railtie
will load them during normal generators lookup:
your generators at a different location, you can specify in your railtie a block which
By default, Rails loads generators from your load path. However, if you want to place
end
end
load ‘path/to/my_railtie.tasks’
rake_tasks do
class MyRailtie < Rails::Railtierake_tasks:
If your railtie has Rake tasks, you can tell Rails to load them through the method
== Loading Rake Tasks and Generators
end
end
MyRailtie.setup!
config.to_prepare do
# and before each request in development.
# Add a to_prepare block which is executed once in production
config.app_generators.orm :my_railtie_orm
# Customize the ORM
class MyRailtie < Rails::Railtie
railties and the application:
Railties can access a config object which contains configuration shared by all
== Configuration
initialization process.initializer, in case you want to couple it with a specific step in the
Finally, you can also pass :before and :after as options to
end
end
app.middleware.use MyRailtie::Middleware
initializer “my_railtie.configure_rails_initialization” do |app|
class MyRailtie < Rails::Railtie
need to access some application-specific configuration, like middleware:
If specified, the block can also receive the application object, in case you
end
end
# some initialization behavior
initializer “my_railtie.configure_rails_initialization” do
class MyRailtie < Rails::Railtie
define the initialization code with the initializer macro:
To add an initialization step to the Rails boot process from your railtie, just
== Initializers
require ‘my_gem/railtie’ if defined?(Rails)
# lib/my_gem.rb
end
end
class Railtie < Rails::Railtie
module MyGem
# lib/my_gem/railtie.rb
without Rails.
The following example demonstrates an extension which can be used with or
called MyNamespace::Railtie.
This class must be loaded during the Rails boot process, and is conventionally
To extend Rails using a railtie, create a subclass of Rails::Railtie.
== Creating a Railtie
* adding Rake tasks
* setting up a subscriber with ActiveSupport::Notifications
* adding config.* keys to the environment
* configuring a Rails framework for the application, like setting a generator
* creating initializers
For example, an extension doing any of the following would need a railtie:
a railtie is needed.
if you need to interact with the Rails framework during or after boot, then
Developing a Rails extension does not require implementing a railtie, but
allowing other components to be used in place of any of the Rails defaults.
own initialization. This makes Rails itself absent of any component hooks,
Record, etc.) implements a railtie. Each of them is responsible for their
Every major component of Rails (Action Mailer, Action Controller, Active
several hooks to extend Rails and/or modify the initialization process.Rails::Railtie is the core of the Rails framework and provides
def abstract_railtie?
def abstract_railtie? ABSTRACT_RAILTIES.include?(name) end
def config
Railtie::Configuration, that is used by Railties and Application to store
This is used to create the config object on Railties, an instance of
def config @config ||= Railtie::Configuration.new end
def configure(&block)
Railtie::Configurable, but this module is no longer required for all
Allows you to configure the railtie. This is the same method seen in
def configure(&block) instance.configure(&block) end
def configure(&block) #:nodoc:
def configure(&block) #:nodoc: instance_eval(&block) end
def console(&blk)
def console(&blk) @load_console ||= [] @load_console << blk if blk @load_console end
def each_registered_block(type, &block)
def each_registered_block(type, &block) klass = self.class while klass.respond_to?(type) klass.public_send(type).each(&block) klass = klass.superclass end end
def generate_railtie_name(string) #:nodoc:
def generate_railtie_name(string) #:nodoc: ActiveSupport::Inflector.underscore(string).tr("/", "_") end
def generators(&blk)
def generators(&blk) @generators ||= [] @generators << blk if blk @generators end
def inherited(base)
def inherited(base) unless base.abstract_railtie? subclasses << base end end
def initialize #:nodoc:
def initialize #:nodoc: if self.class.abstract_railtie? raise "#{self.class.name} is abstract, you cannot instantiate it directly." end end
def instance
Since Rails::Railtie cannot be instantiated, any methods that call
def instance @instance ||= new end
def method_missing(name, *args, &block)
If the class method does not have a method, then send the method call
def method_missing(name, *args, &block) if instance.respond_to?(name) instance.public_send(name, *args, &block) else super end end
def railtie_name(name = nil)
def railtie_name(name = nil) @railtie_name = name.to_s if name @railtie_name ||= generate_railtie_name(self.name) end
def railtie_namespace #:nodoc:
def railtie_namespace #:nodoc: @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) } end
def rake_tasks(&blk)
def rake_tasks(&blk) @rake_tasks ||= [] @rake_tasks << blk if blk @rake_tasks end
def respond_to_missing?(*args)
def respond_to_missing?(*args) instance.respond_to?(*args) || super end
def run_console_blocks(app) #:nodoc:
def run_console_blocks(app) #:nodoc: each_registered_block(:console) { |block| block.call(app) } end
def run_generators_blocks(app) #:nodoc:
def run_generators_blocks(app) #:nodoc: each_registered_block(:generators) { |block| block.call(app) } end
def run_runner_blocks(app) #:nodoc:
def run_runner_blocks(app) #:nodoc: each_registered_block(:runner) { |block| block.call(app) } end
def run_tasks_blocks(app) #:nodoc:
def run_tasks_blocks(app) #:nodoc: extend Rake::DSL each_registered_block(:rake_tasks) { |block| instance_exec(app, &block) } end
def runner(&blk)
def runner(&blk) @load_runner ||= [] @load_runner << blk if blk @load_runner end
def subclasses
def subclasses @subclasses ||= [] end