2010-01-23 16:30:17 -05:00
|
|
|
require 'rails/initializable'
|
|
|
|
require 'rails/configuration'
|
2010-03-01 21:52:07 -05:00
|
|
|
require 'active_support/inflector'
|
2011-06-07 05:21:38 -04:00
|
|
|
require 'active_support/core_ext/module/introspection'
|
2011-08-15 16:06:21 -04:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
2010-01-23 16:30:17 -05:00
|
|
|
|
2009-12-31 16:11:54 -05:00
|
|
|
module Rails
|
2011-02-19 01:20:04 -05:00
|
|
|
# Railtie is the core of the Rails framework and provides several hooks to extend
|
2010-02-02 14:05:26 -05:00
|
|
|
# Rails and/or modify the initialization process.
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2010-02-02 14:05:26 -05:00
|
|
|
# Every major component of Rails (Action Mailer, Action Controller,
|
2011-05-15 15:34:36 -04:00
|
|
|
# Action View and Active Record) is a Railtie. Each of
|
2011-02-19 01:20:04 -05:00
|
|
|
# them is responsible for their own initialization. This makes Rails itself
|
|
|
|
# absent of any component hooks, allowing other components to be used in
|
|
|
|
# place of any of the Rails defaults.
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2010-02-07 23:08:10 -05:00
|
|
|
# Developing a Rails extension does _not_ require any implementation of
|
|
|
|
# Railtie, but if you need to interact with the Rails framework during
|
2011-02-19 01:20:04 -05:00
|
|
|
# or after boot, then Railtie is needed.
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2011-02-19 01:20:04 -05:00
|
|
|
# For example, an extension doing any of the following would require Railtie:
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2010-02-02 14:05:26 -05:00
|
|
|
# * creating initializers
|
2011-02-19 01:20:04 -05:00
|
|
|
# * configuring a Rails framework for the application, like setting a generator
|
2012-04-28 01:46:45 -04:00
|
|
|
# * +adding config.*+ keys to the environment
|
2011-02-19 01:20:04 -05:00
|
|
|
# * setting up a subscriber with ActiveSupport::Notifications
|
|
|
|
# * adding rake tasks
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2010-02-02 14:05:26 -05:00
|
|
|
# == Creating your Railtie
|
|
|
|
#
|
2011-02-19 01:20:04 -05:00
|
|
|
# To extend Rails using Railtie, create a Railtie class which inherits
|
|
|
|
# from Rails::Railtie within your extension's namespace. This class must be
|
|
|
|
# loaded during the Rails boot process.
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2011-02-19 01:20:04 -05:00
|
|
|
# The following example demonstrates an extension which can be used with or without Rails.
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2011-02-19 01:20:04 -05:00
|
|
|
# # lib/my_gem/railtie.rb
|
|
|
|
# module MyGem
|
|
|
|
# class Railtie < Rails::Railtie
|
2010-01-21 22:54:32 -05:00
|
|
|
# end
|
2011-02-19 01:20:04 -05:00
|
|
|
# end
|
2010-06-21 06:07:13 -04:00
|
|
|
#
|
2011-02-19 01:20:04 -05:00
|
|
|
# # lib/my_gem.rb
|
|
|
|
# require 'my_gem/railtie' if defined?(Rails)
|
2010-01-21 22:54:32 -05:00
|
|
|
#
|
2010-02-02 14:05:26 -05:00
|
|
|
# == 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
|
|
|
|
#
|
2010-06-21 06:07:13 -04:00
|
|
|
# If specified, the block can also receive the application object, in case you
|
2010-04-05 18:32:03 -04:00
|
|
|
# need to access some application specific configuration, like middleware:
|
2010-02-02 14:05:26 -05:00
|
|
|
#
|
|
|
|
# class MyRailtie < Rails::Railtie
|
|
|
|
# initializer "my_railtie.configure_rails_initialization" do |app|
|
2010-07-24 15:28:45 -04:00
|
|
|
# app.middleware.use MyRailtie::Middleware
|
2010-02-02 14:05:26 -05:00
|
|
|
# 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
|
2010-10-02 12:38:23 -04:00
|
|
|
# config.app_generators.orm :my_railtie_orm
|
2010-02-02 14:05:26 -05:00
|
|
|
#
|
|
|
|
# # Add a to_prepare block which is executed once in production
|
2010-08-17 21:29:37 -04:00
|
|
|
# # and before each request in development
|
2010-02-02 14:05:26 -05:00
|
|
|
# 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
|
2011-06-11 17:56:29 -04:00
|
|
|
# rake_tasks:
|
2010-02-02 14:05:26 -05:00
|
|
|
#
|
2010-11-16 10:26:21 -05:00
|
|
|
# class MyRailtie < Rails::Railtie
|
2010-02-02 14:05:26 -05:00
|
|
|
# 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:
|
|
|
|
#
|
2010-11-16 10:26:21 -05:00
|
|
|
# class MyRailtie < Rails::Railtie
|
2010-02-02 14:05:26 -05:00
|
|
|
# generators do
|
|
|
|
# require "path/to/my_railtie_generator"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2012-01-02 15:49:18 -05:00
|
|
|
# == Application and Engine
|
2010-02-02 14:05:26 -05:00
|
|
|
#
|
|
|
|
# A Rails::Engine is nothing more than a Railtie with some initializers already set.
|
2012-01-02 15:49:18 -05:00
|
|
|
# And since Rails::Application is an engine, the same configuration described here
|
|
|
|
# can be used in both.
|
2010-02-02 14:05:26 -05:00
|
|
|
#
|
|
|
|
# Be sure to look at the documentation of those specific classes for more information.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-12-31 16:11:54 -05:00
|
|
|
class Railtie
|
2010-01-23 16:30:17 -05:00
|
|
|
autoload :Configurable, "rails/railtie/configurable"
|
|
|
|
autoload :Configuration, "rails/railtie/configuration"
|
2010-01-23 12:41:53 -05:00
|
|
|
|
2009-12-31 16:11:54 -05:00
|
|
|
include Initializable
|
|
|
|
|
2012-01-02 15:49:18 -05:00
|
|
|
ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Engine Rails::Application)
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
class << self
|
2010-07-19 11:53:14 -04:00
|
|
|
private :new
|
|
|
|
|
2010-01-23 12:41:53 -05:00
|
|
|
def subclasses
|
|
|
|
@subclasses ||= []
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
def inherited(base)
|
2010-03-26 09:41:40 -04:00
|
|
|
unless base.abstract_railtie?
|
2010-07-19 11:53:14 -04:00
|
|
|
base.send(:include, Railtie::Configurable)
|
2010-01-23 12:41:53 -05:00
|
|
|
subclasses << base
|
|
|
|
end
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
def rake_tasks(&blk)
|
|
|
|
@rake_tasks ||= []
|
|
|
|
@rake_tasks << blk if blk
|
|
|
|
@rake_tasks
|
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-07-17 04:59:41 -04:00
|
|
|
def console(&blk)
|
|
|
|
@load_console ||= []
|
|
|
|
@load_console << blk if blk
|
|
|
|
@load_console
|
|
|
|
end
|
|
|
|
|
2012-05-29 10:31:27 -04:00
|
|
|
def runner(&blk)
|
|
|
|
@load_runner ||= []
|
|
|
|
@load_runner << blk if blk
|
|
|
|
@load_runner
|
|
|
|
end
|
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
def generators(&blk)
|
|
|
|
@generators ||= []
|
|
|
|
@generators << blk if blk
|
|
|
|
@generators
|
|
|
|
end
|
2010-01-23 12:41:53 -05:00
|
|
|
|
2010-03-26 09:41:40 -04:00
|
|
|
def abstract_railtie?
|
|
|
|
ABSTRACT_RAILTIES.include?(name)
|
2010-03-01 21:52:07 -05:00
|
|
|
end
|
2010-07-26 11:05:04 -04:00
|
|
|
|
|
|
|
def railtie_name(name = nil)
|
2010-07-26 13:33:58 -04:00
|
|
|
@railtie_name = name.to_s if name
|
2010-08-03 16:36:12 -04:00
|
|
|
@railtie_name ||= generate_railtie_name(self.name)
|
2010-07-26 11:05:04 -04:00
|
|
|
end
|
2010-08-03 16:36:12 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
def generate_railtie_name(class_or_module)
|
2012-04-03 09:16:09 -04:00
|
|
|
ActiveSupport::Inflector.underscore(class_or_module).tr("/", "_")
|
2010-08-03 16:36:12 -04:00
|
|
|
end
|
2010-01-19 12:43:09 -05:00
|
|
|
end
|
|
|
|
|
2010-07-26 11:05:04 -04:00
|
|
|
delegate :railtie_name, :to => "self.class"
|
|
|
|
|
2010-07-19 11:53:14 -04:00
|
|
|
def config
|
|
|
|
@config ||= Railtie::Configuration.new
|
|
|
|
end
|
|
|
|
|
2010-05-15 17:48:56 -04:00
|
|
|
def eager_load!
|
|
|
|
end
|
|
|
|
|
2011-06-15 14:52:45 -04:00
|
|
|
def load_console(app=self)
|
2011-05-24 19:37:55 -04:00
|
|
|
self.class.console.each { |block| block.call(app) }
|
2010-01-19 12:43:09 -05:00
|
|
|
end
|
|
|
|
|
2012-05-29 10:31:27 -04:00
|
|
|
def load_runner(app=self)
|
|
|
|
self.class.runner.each { |block| block.call(app) }
|
|
|
|
end
|
|
|
|
|
2011-06-15 14:52:45 -04:00
|
|
|
def load_tasks(app=self)
|
2011-05-20 20:02:20 -04:00
|
|
|
extend Rake::DSL if defined? Rake::DSL
|
2011-11-27 08:49:37 -05:00
|
|
|
self.class.rake_tasks.each { |block| self.instance_exec(app, &block) }
|
2010-10-09 14:11:36 -04:00
|
|
|
|
|
|
|
# load also tasks from all superclasses
|
|
|
|
klass = self.class.superclass
|
|
|
|
while klass.respond_to?(:rake_tasks)
|
2011-05-24 19:37:55 -04:00
|
|
|
klass.rake_tasks.each { |t| self.instance_exec(app, &t) }
|
2010-10-09 14:11:36 -04:00
|
|
|
klass = klass.superclass
|
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
end
|
2010-01-19 12:43:09 -05:00
|
|
|
|
2011-06-15 14:52:45 -04:00
|
|
|
def load_generators(app=self)
|
2011-05-24 19:03:15 -04:00
|
|
|
self.class.generators.each { |block| block.call(app) }
|
2010-01-19 12:43:09 -05:00
|
|
|
end
|
2011-06-07 05:21:38 -04:00
|
|
|
|
|
|
|
def railtie_namespace
|
2011-11-23 14:06:45 -05:00
|
|
|
@railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) }
|
2011-06-07 05:21:38 -04:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
end
|
|
|
|
end
|