2010-01-23 16:30:17 -05:00
|
|
|
require 'rails/railtie'
|
2010-03-01 22:29:12 -05:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
|
|
|
require 'pathname'
|
2010-04-26 11:25:07 -04:00
|
|
|
require 'rbconfig'
|
2010-01-21 17:14:20 -05:00
|
|
|
|
|
|
|
module Rails
|
2010-02-02 14:05:26 -05:00
|
|
|
# Rails::Engine allows you to wrap a specific Rails application and share it accross
|
2010-02-07 23:08:10 -05:00
|
|
|
# different applications. Since Rails 3.0, every Rails::Application is nothing
|
|
|
|
# more than an Engine, allowing you to share it very easily.
|
2010-02-02 14:05:26 -05:00
|
|
|
#
|
|
|
|
# Any Rails::Engine is also a Rails::Railtie, so the same methods (like rake_tasks and
|
|
|
|
# generators) and configuration available in the latter can also be used in the former.
|
|
|
|
#
|
|
|
|
# == Creating an Engine
|
|
|
|
#
|
|
|
|
# In Rails versions before to 3.0, your gems automatically behaved as Engine, however
|
|
|
|
# this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically
|
|
|
|
# behave as Engine, you have to specify an Engine for it somewhere inside your plugin
|
|
|
|
# lib folder (similar with how we spceify a Railtie):
|
|
|
|
#
|
|
|
|
# # lib/my_engine.rb
|
|
|
|
# module MyEngine
|
|
|
|
# class Engine < Rails::Engine
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Then ensure that this file is loaded at the top of your config/application.rb (or in
|
|
|
|
# your Gemfile) and it will automatically load models, controllers, helpers and metals
|
|
|
|
# inside app, load routes at "config/routes.rb", load locales at "config/locales/*",
|
|
|
|
# load tasks at "lib/tasks/*".
|
|
|
|
#
|
|
|
|
# == Configuration
|
|
|
|
#
|
|
|
|
# Besides the Railtie configuration which is shared across the application, in a
|
|
|
|
# Rails::Engine you can access load_paths, eager_load_paths and load_once_paths,
|
|
|
|
# which differently from a Railtie, are scoped to the current Engine.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class MyEngine < Rails::Engine
|
|
|
|
# # Add a load path for this specific Engine
|
|
|
|
# config.load_paths << File.expand_path("../lib/some/path", __FILE__)
|
2010-04-05 18:32:03 -04:00
|
|
|
#
|
|
|
|
# initializer "my_engine.add_middleware" do |app|
|
|
|
|
# app.middlewares.use MyEngine::Middleware
|
|
|
|
# end
|
2010-02-02 14:05:26 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# == Paths
|
|
|
|
#
|
2010-02-07 23:08:10 -05:00
|
|
|
# Since Rails 3.0, both your Application and Engines do not have hardcoded paths.
|
2010-02-02 14:05:26 -05:00
|
|
|
# This means that you are not required to place your controllers at "app/controllers",
|
|
|
|
# but in any place which you find convenient.
|
|
|
|
#
|
|
|
|
# For example, let's suppose you want to lay your controllers at lib/controllers, all
|
|
|
|
# you need to do is:
|
|
|
|
#
|
|
|
|
# class MyEngine < Rails::Engine
|
|
|
|
# paths.app.controllers = "lib/controllers"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# You can also have your controllers being loaded from both "app/controllers" and
|
|
|
|
# "lib/controllers":
|
|
|
|
#
|
|
|
|
# class MyEngine < Rails::Engine
|
|
|
|
# paths.app.controllers << "lib/controllers"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# The available paths in an Engine are:
|
|
|
|
#
|
|
|
|
# class MyEngine < Rails::Engine
|
|
|
|
# paths.app = "app"
|
|
|
|
# paths.app.controllers = "app/controllers"
|
|
|
|
# paths.app.helpers = "app/helpers"
|
|
|
|
# paths.app.models = "app/models"
|
|
|
|
# paths.app.metals = "app/metal"
|
|
|
|
# paths.app.views = "app/views"
|
|
|
|
# paths.lib = "lib"
|
|
|
|
# paths.lib.tasks = "lib/tasks"
|
|
|
|
# paths.config = "config"
|
|
|
|
# paths.config.initializers = "config/initializers"
|
|
|
|
# paths.config.locales = "config/locales"
|
|
|
|
# paths.config.routes = "config/routes.rb"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Your Application class adds a couple more paths to this set. And as in your Application,
|
|
|
|
# all folders under "app" are automatically added to the load path. So if you have
|
|
|
|
# "app/observers", it's added by default.
|
|
|
|
#
|
2010-01-21 17:14:20 -05:00
|
|
|
class Engine < Railtie
|
2010-01-23 16:30:17 -05:00
|
|
|
autoload :Configurable, "rails/engine/configurable"
|
|
|
|
autoload :Configuration, "rails/engine/configuration"
|
2010-01-23 12:41:53 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
class << self
|
|
|
|
attr_accessor :called_from
|
|
|
|
|
2010-03-26 13:47:55 -04:00
|
|
|
# TODO Remove this. It's deprecated.
|
|
|
|
alias :engine_name :railtie_name
|
2010-01-24 06:23:21 -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-01-23 12:41:53 -05:00
|
|
|
base.called_from = begin
|
2010-02-18 12:39:39 -05:00
|
|
|
# Remove the line number from backtraces making sure we don't leave anything behind
|
2010-02-18 12:21:01 -05:00
|
|
|
call_stack = caller.map { |p| p.split(':')[0..-2].join(':') }
|
2010-02-27 20:10:04 -05:00
|
|
|
File.dirname(call_stack.detect { |p| p !~ %r[railties[\w\-\.]*/lib/rails|rack[\w\-\.]*/lib/rack] })
|
2010-01-23 12:41:53 -05:00
|
|
|
end
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2010-01-23 12:41:53 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-01-23 12:41:53 -05:00
|
|
|
def find_root_with_flag(flag, default=nil)
|
2010-01-21 17:14:20 -05:00
|
|
|
root_path = self.called_from
|
|
|
|
|
|
|
|
while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
|
|
|
|
parent = File.dirname(root_path)
|
|
|
|
root_path = parent != root_path && parent
|
|
|
|
end
|
|
|
|
|
2010-01-21 19:10:31 -05:00
|
|
|
root = File.exist?("#{root_path}/#{flag}") ? root_path : default
|
2010-01-21 17:14:20 -05:00
|
|
|
raise "Could not find root path for #{self}" unless root
|
|
|
|
|
2010-04-26 11:25:07 -04:00
|
|
|
Config::CONFIG['host_os'] =~ /mswin|mingw/ ?
|
2010-01-23 10:59:32 -05:00
|
|
|
Pathname.new(root).expand_path : Pathname.new(root).realpath
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-26 13:47:55 -04:00
|
|
|
delegate :middleware, :paths, :root, :to => :config
|
2010-01-23 11:51:48 -05:00
|
|
|
|
|
|
|
def load_tasks
|
2010-02-28 17:39:01 -05:00
|
|
|
super
|
2010-01-23 11:51:48 -05:00
|
|
|
config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
|
|
|
|
end
|
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
# Add configured load paths to ruby load paths and remove duplicates.
|
2010-01-25 16:59:08 -05:00
|
|
|
initializer :set_load_path, :before => :bootstrap_load_path do
|
2010-01-22 19:29:29 -05:00
|
|
|
config.load_paths.reverse_each do |path|
|
2010-01-22 10:24:44 -05:00
|
|
|
$LOAD_PATH.unshift(path) if File.directory?(path)
|
|
|
|
end
|
2010-01-21 17:14:20 -05:00
|
|
|
$LOAD_PATH.uniq!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the paths from which Rails will automatically load source files,
|
|
|
|
# and the load_once paths.
|
2010-01-25 16:59:08 -05:00
|
|
|
initializer :set_autoload_paths, :before => :bootstrap_load_path do |app|
|
2010-01-25 16:00:07 -05:00
|
|
|
ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths)
|
2010-01-21 17:14:20 -05:00
|
|
|
|
2010-01-23 11:13:25 -05:00
|
|
|
if reloadable?(app)
|
2010-01-25 16:00:07 -05:00
|
|
|
ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_once_paths)
|
2010-01-23 11:13:25 -05:00
|
|
|
else
|
2010-01-25 16:00:07 -05:00
|
|
|
ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_paths)
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
|
|
|
|
2010-01-22 19:29:29 -05:00
|
|
|
# Freeze so future modifications will fail rather than do nothing mysteriously
|
2010-01-23 11:13:25 -05:00
|
|
|
config.load_paths.freeze
|
2010-01-21 17:14:20 -05:00
|
|
|
config.load_once_paths.freeze
|
|
|
|
end
|
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
initializer :add_routing_paths do |app|
|
2010-01-24 08:48:00 -05:00
|
|
|
paths.config.routes.to_a.each do |route|
|
2010-01-27 11:39:35 -05:00
|
|
|
app.routes_reloader.paths.unshift(route) if File.exists?(route)
|
2010-01-22 19:29:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-28 17:39:01 -05:00
|
|
|
# DEPRECATED: Remove in 3.1
|
2010-01-24 05:06:06 -05:00
|
|
|
initializer :add_routing_namespaces do |app|
|
2010-01-24 08:48:00 -05:00
|
|
|
paths.app.controllers.to_a.each do |load_path|
|
2010-01-24 05:06:06 -05:00
|
|
|
load_path = File.expand_path(load_path)
|
2010-03-06 13:19:26 -05:00
|
|
|
Dir["#{load_path}/*/**/*_controller.rb"].collect do |path|
|
2010-04-19 15:43:42 -04:00
|
|
|
namespace = File.dirname(path).sub(/#{Regexp.escape(load_path)}\/?/, '')
|
2010-01-24 05:06:06 -05:00
|
|
|
app.routes.controller_namespaces << namespace unless namespace.empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-25 16:00:07 -05:00
|
|
|
# I18n load paths are a special case since the ones added
|
|
|
|
# later have higher priority.
|
2010-01-22 19:29:29 -05:00
|
|
|
initializer :add_locales do
|
2010-02-20 09:46:55 -05:00
|
|
|
config.i18n.railties_load_path.concat(paths.config.locales.to_a)
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
|
|
|
|
2010-01-21 19:10:31 -05:00
|
|
|
initializer :add_view_paths do
|
2010-01-24 08:48:00 -05:00
|
|
|
views = paths.app.views.to_a
|
2010-03-29 20:08:08 -04:00
|
|
|
ActiveSupport.on_load(:action_controller) do
|
|
|
|
prepend_view_path(views)
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport.on_load(:action_mailer) do
|
|
|
|
prepend_view_path(views)
|
|
|
|
end
|
2010-01-21 19:10:31 -05:00
|
|
|
end
|
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
initializer :add_metals do |app|
|
|
|
|
app.metal_loader.paths.unshift(*paths.app.metals.to_a)
|
2010-01-24 08:48:00 -05:00
|
|
|
end
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
initializer :load_config_initializers do
|
2010-01-24 08:48:00 -05:00
|
|
|
paths.config.initializers.to_a.sort.each do |initializer|
|
2010-01-22 19:29:29 -05:00
|
|
|
load(initializer)
|
2010-01-21 19:10:31 -05:00
|
|
|
end
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
|
|
|
|
2010-04-05 04:52:47 -04:00
|
|
|
initializer :load_app_classes do |app|
|
2010-01-21 17:14:20 -05:00
|
|
|
next if $rails_rake_task
|
|
|
|
|
|
|
|
if app.config.cache_classes
|
|
|
|
config.eager_load_paths.each do |load_path|
|
2010-01-24 08:48:00 -05:00
|
|
|
matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
|
2010-01-21 17:14:20 -05:00
|
|
|
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
|
|
|
|
require_dependency file.sub(matcher, '\1')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-01-23 12:41:53 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def reloadable?(app)
|
2010-01-27 12:56:31 -05:00
|
|
|
app.config.reload_engines
|
2010-01-23 12:41:53 -05:00
|
|
|
end
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2010-02-26 16:29:44 -05:00
|
|
|
end
|