Class: Rails::Railtie
- Inherits:
-
Object
- Object
- Rails::Railtie
- Includes:
- Initializable
- Defined in:
- railties/lib/rails/railtie.rb,
railties/lib/rails/railtie/configurable.rb,
railties/lib/rails/railtie/configuration.rb
Overview
Railtie is the core of the Rails Framework and provides several hooks to extend Rails and/or modify the initialization process.
Every major component of Rails (Action Mailer, Action Controller, Action View, Active Record and Active Resource) are all Railties, so each of them is responsible to set their own initialization. This makes, for example, Rails absent of any Active Record hook, allowing any other ORM framework to hook in.
Developing a Rails extension does not require any implementation of Railtie, but if you need to interact with the Rails framework during or after boot, then Railtie is what you need to do that interaction.
For example, the following would need you to implement Railtie in your plugin:
-
creating initializers
-
configuring a Rails framework or the Application, like setting a generator
-
adding Rails config.* keys to the environment
-
setting up a subscriber to the Rails ActiveSupport::Notifications
-
adding rake tasks into rails
Creating your Railtie
Implementing Railtie in your Rails extension is done by creating a class Railtie that has your extension name and making sure that this gets loaded during boot time of the Rails stack.
You can do this however you wish, but here is an example if you want to provide it for a gem that can be used with or without Rails:
-
Create a file (say, lib/my_gem/railtie.rb) which contains class Railtie inheriting from Rails::Railtie and is namespaced to your gem:
# lib/my_gem/railtie.rb module MyGem class Railtie < Rails::Railtie end end -
Require your own gem as well as rails in this file:
# lib/my_gem/railtie.rb require 'my_gem' require 'rails' module MyGem class Railtie < Rails::Railtie end end
Initializers
To add an initialization step from your Railtie to Rails boot process, you just need to create an initializer block:
class MyRailtie < Rails::Railtie
initializer "my_railtie.configure_rails_initialization" do
# some initialization behavior
end
end
If specified, the block can also receive the application object, in case you need to access some application specific configuration, like middleware:
class MyRailtie < Rails::Railtie
initializer "my_railtie.configure_rails_initialization" do |app|
app.middleware.use MyRailtie::Middleware
end
end
Finally, you can also pass :before and :after as option to initializer, in case you want to couple it with a specific step in the initialization process.
Configuration
Inside the Railtie class, you can access a config object which contains configuration shared by all railties and the application:
class MyRailtie < Rails::Railtie
# Customize the ORM
config.app_generators.orm :my_railtie_orm
# Add a to_prepare block which is executed once in production
# and before each request in development
config.to_prepare do
MyRailtie.setup!
end
end
Loading rake tasks and generators
If your railtie has rake tasks, you can tell Rails to load them through the method rake tasks:
class MyRailtie < Rails::Railtie
rake_tasks do
load "path/to/my_railtie.tasks"
end
end
By default, Rails load generators from your load path. However, if you want to place your generators at a different location, you can specify in your Railtie a block which will load them during normal generators lookup:
class MyRailtie < Rails::Railtie
generators do
require "path/to/my_railtie_generator"
end
end
Application, Plugin and Engine
A Rails::Engine is nothing more than a Railtie with some initializers already set. And since Rails::Application and Rails::Plugin are engines, the same configuration described here can be used in all three.
Be sure to look at the documentation of those specific classes for more information.
Defined Under Namespace
Modules: Configurable Classes: Configuration
Constant Summary
- ABSTRACT_RAILTIES =
%w(Rails::Railtie
Class Method Summary (collapse)
- + (Boolean) abstract_railtie?
- + (Object) console(&blk)
- + (Object) generators(&blk)
- + (Object) inherited(base)
- + (Object) railtie_name(name = nil)
- + (Object) rake_tasks(&blk)
- + (Object) subclasses
Instance Method Summary (collapse)
- - (Object) config
- - (Object) eager_load!
- - (Object) load_console
- - (Object) load_generators
- - (Object) load_tasks
Methods included from Initializable
included, #initializers, #run_initializers
Class Method Details
+ (Boolean) abstract_railtie?
164 165 166 |
# File 'railties/lib/rails/railtie.rb', line 164 def abstract_railtie? ABSTRACT_RAILTIES.include?(name) end |
+ (Object) console(&blk)
152 153 154 155 156 |
# File 'railties/lib/rails/railtie.rb', line 152 def console(&blk) @load_console ||= [] @load_console << blk if blk @load_console end |
+ (Object) generators(&blk)
158 159 160 161 162 |
# File 'railties/lib/rails/railtie.rb', line 158 def generators(&blk) @generators ||= [] @generators << blk if blk @generators end |
+ (Object) inherited(base)
139 140 141 142 143 144 |
# File 'railties/lib/rails/railtie.rb', line 139 def inherited(base) unless base.abstract_railtie? base.send(:include, Railtie::Configurable) subclasses << base end end |
+ (Object) railtie_name(name = nil)
168 169 170 171 |
# File 'railties/lib/rails/railtie.rb', line 168 def railtie_name(name = nil) @railtie_name = name.to_s if name @railtie_name ||= generate_railtie_name(self.name) end |
+ (Object) rake_tasks(&blk)
146 147 148 149 150 |
# File 'railties/lib/rails/railtie.rb', line 146 def rake_tasks(&blk) @rake_tasks ||= [] @rake_tasks << blk if blk @rake_tasks end |
+ (Object) subclasses
135 136 137 |
# File 'railties/lib/rails/railtie.rb', line 135 def subclasses @subclasses ||= [] end |
Instance Method Details
- (Object) config
181 182 183 |
# File 'railties/lib/rails/railtie.rb', line 181 def config @config ||= Railtie::Configuration.new end |
- (Object) eager_load!
185 186 |
# File 'railties/lib/rails/railtie.rb', line 185 def eager_load! end |
- (Object) load_console
188 189 190 |
# File 'railties/lib/rails/railtie.rb', line 188 def load_console self.class.console.each(&:call) end |
- (Object) load_generators
203 204 205 |
# File 'railties/lib/rails/railtie.rb', line 203 def load_generators self.class.generators.each(&:call) end |
- (Object) load_tasks
192 193 194 195 196 197 198 199 200 201 |
# File 'railties/lib/rails/railtie.rb', line 192 def load_tasks self.class.rake_tasks.each(&:call) # load also tasks from all superclasses klass = self.class.superclass while klass.respond_to?(:rake_tasks) klass.rake_tasks.each { |t| self.instance_exec(&t) } klass = klass.superclass end end |