1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails/engine.rb

138 lines
4.3 KiB
Ruby
Raw Normal View History

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'
module Rails
class Engine < Railtie
2010-01-23 16:30:17 -05:00
autoload :Configurable, "rails/engine/configurable"
autoload :Configuration, "rails/engine/configuration"
class << self
attr_accessor :called_from
alias :engine_name :railtie_name
alias :engine_names :railtie_names
def inherited(base)
unless abstract_railtie?(base)
base.called_from = begin
# Remove the line number from backtraces making sure we don't leave anything behind
call_stack = caller.map { |p| p.split(':')[0..-2].join(':') }
File.dirname(call_stack.detect { |p| p !~ %r[railties[\w\-\.]*/lib/rails|rack[\w\-\.]*/lib/rack] })
end
end
super
end
def find_root_with_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-21 19:10:31 -05:00
root = File.exist?("#{root_path}/#{flag}") ? root_path : default
raise "Could not find root path for #{self}" unless root
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
2010-01-23 10:59:32 -05:00
Pathname.new(root).expand_path : Pathname.new(root).realpath
end
end
delegate :middleware, :paths, :metal_loader, :root, :to => :config
2010-01-23 11:51:48 -05:00
def load_tasks
super
2010-01-23 11:51:48 -05:00
config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
end
# Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path, :before => :bootstrap_load_path do
config.load_paths.reverse_each do |path|
2010-01-22 10:24:44 -05:00
$LOAD_PATH.unshift(path) if File.directory?(path)
end
$LOAD_PATH.uniq!
end
# Set the paths from which Rails will automatically load source files,
# and the load_once paths.
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)
if reloadable?(app)
2010-01-25 16:00:07 -05:00
ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_once_paths)
else
2010-01-25 16:00:07 -05:00
ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_paths)
end
# Freeze so future modifications will fail rather than do nothing mysteriously
config.load_paths.freeze
config.load_once_paths.freeze
end
initializer :add_routing_paths do |app|
paths.config.routes.to_a.each do |route|
app.routes_reloader.paths.unshift(route) if File.exists?(route)
end
end
# DEPRECATED: Remove in 3.1
initializer :add_routing_namespaces do |app|
paths.app.controllers.to_a.each do |load_path|
load_path = File.expand_path(load_path)
Dir["#{load_path}/*/**/*_controller.rb"].collect do |path|
namespace = File.dirname(path).sub(/#{load_path}\/?/, '')
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.
initializer :add_locales do
config.i18n.railties_load_path.concat(paths.config.locales.to_a)
end
2010-01-21 19:10:31 -05:00
initializer :add_view_paths do
views = paths.app.views.to_a
ActionController.base_hook { prepend_view_path(views) } if defined?(ActionController)
ActionMailer.base_hook { prepend_view_path(views) } if defined?(ActionMailer)
2010-01-21 19:10:31 -05:00
end
initializer :add_metals do |app|
app.metal_loader.paths.unshift(*paths.app.metals.to_a)
end
initializer :add_generator_templates do |app|
config.generators.templates.unshift(*paths.lib.templates.to_a)
end
2010-01-21 19:10:31 -05:00
initializer :load_application_initializers do
paths.config.initializers.to_a.sort.each do |initializer|
load(initializer)
2010-01-21 19:10:31 -05:00
end
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
protected
def reloadable?(app)
app.config.reload_engines
end
end
end