2010-01-21 22:14:20 +00:00
|
|
|
require 'active_support/core_ext/module/delegation'
|
|
|
|
|
|
|
|
module Rails
|
|
|
|
class Engine < Railtie
|
|
|
|
class << self
|
|
|
|
attr_accessor :called_from
|
2010-01-23 15:59:32 +00:00
|
|
|
delegate :middleware, :root, :paths, :to => :config
|
2010-01-21 22:14:20 +00:00
|
|
|
|
2010-01-22 00:10:31 +00:00
|
|
|
def original_root
|
|
|
|
@original_root ||= find_root_with_file_flag("lib")
|
2010-01-21 22:14:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
2010-01-22 00:10:31 +00:00
|
|
|
@config ||= Configuration.new(original_root)
|
2010-01-21 22:14:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def inherited(base)
|
|
|
|
base.called_from = begin
|
|
|
|
call_stack = caller.map { |p| p.split(':').first }
|
|
|
|
File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] })
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_root_with_file_flag(flag, default=nil)
|
|
|
|
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-22 00:10:31 +00:00
|
|
|
root = File.exist?("#{root_path}/#{flag}") ? root_path : default
|
2010-01-21 22:14:20 +00:00
|
|
|
raise "Could not find root path for #{self}" unless root
|
|
|
|
|
|
|
|
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
|
2010-01-23 15:59:32 +00:00
|
|
|
Pathname.new(root).expand_path : Pathname.new(root).realpath
|
2010-01-21 22:14:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-23 15:59:32 +00:00
|
|
|
delegate :middleware, :paths, :root, :config, :to => :'self.class'
|
2010-01-21 22:14:20 +00:00
|
|
|
|
2010-01-23 16:13:25 +00:00
|
|
|
def reloadable?(app)
|
|
|
|
app.config.reload_plugins
|
|
|
|
end
|
|
|
|
|
2010-01-21 22:14:20 +00:00
|
|
|
# Add configured load paths to ruby load paths and remove duplicates.
|
2010-01-23 00:29:29 +00:00
|
|
|
initializer :set_load_path do
|
|
|
|
config.load_paths.reverse_each do |path|
|
2010-01-22 15:24:44 +00:00
|
|
|
$LOAD_PATH.unshift(path) if File.directory?(path)
|
|
|
|
end
|
2010-01-21 22:14:20 +00:00
|
|
|
$LOAD_PATH.uniq!
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the paths from which Rails will automatically load source files,
|
|
|
|
# and the load_once paths.
|
2010-01-23 16:13:25 +00:00
|
|
|
initializer :set_autoload_paths do |app|
|
2010-01-23 00:29:29 +00:00
|
|
|
ActiveSupport::Dependencies.load_paths.concat(config.load_paths)
|
2010-01-21 22:14:20 +00:00
|
|
|
|
2010-01-23 16:13:25 +00:00
|
|
|
if reloadable?(app)
|
|
|
|
ActiveSupport::Dependencies.load_once_paths.concat(config.load_once_paths)
|
|
|
|
else
|
|
|
|
ActiveSupport::Dependencies.load_once_paths.concat(config.load_paths)
|
2010-01-21 22:14:20 +00:00
|
|
|
end
|
|
|
|
|
2010-01-23 00:29:29 +00:00
|
|
|
# Freeze so future modifications will fail rather than do nothing mysteriously
|
2010-01-23 16:13:25 +00:00
|
|
|
config.load_paths.freeze
|
2010-01-21 22:14:20 +00:00
|
|
|
config.load_once_paths.freeze
|
|
|
|
end
|
|
|
|
|
2010-01-23 14:05:13 +00:00
|
|
|
initializer :add_routing_files do
|
2010-01-23 00:29:29 +00:00
|
|
|
config.paths.config.routes.to_a.each do |route|
|
2010-01-23 14:05:13 +00:00
|
|
|
config.action_dispatch.route_files.unshift(route) if File.exists?(route)
|
2010-01-23 00:29:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer :add_locales do
|
2010-01-23 01:03:11 +00:00
|
|
|
config.i18n.load_path.unshift(*config.paths.config.locales.to_a)
|
2010-01-21 22:14:20 +00:00
|
|
|
end
|
|
|
|
|
2010-01-22 00:10:31 +00:00
|
|
|
initializer :add_view_paths do
|
2010-01-23 00:29:29 +00:00
|
|
|
views = config.paths.app.views.to_a
|
|
|
|
ActionController::Base.view_paths.concat(views) if defined?(ActionController)
|
|
|
|
ActionMailer::Base.view_paths.concat(views) if defined?(ActionMailer)
|
2010-01-22 00:10:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
initializer :load_application_initializers do
|
2010-01-23 00:29:29 +00:00
|
|
|
config.paths.config.initializers.each do |initializer|
|
|
|
|
load(initializer)
|
2010-01-22 00:10:31 +00:00
|
|
|
end
|
2010-01-21 22:14:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
initializer :load_application_classes do |app|
|
|
|
|
next if $rails_rake_task
|
|
|
|
|
|
|
|
if app.config.cache_classes
|
|
|
|
config.eager_load_paths.each do |load_path|
|
|
|
|
matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
|
|
|
|
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
|
|
|
|
require_dependency file.sub(matcher, '\1')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|