From 02c5137eadbb3530033d919b7aebeb6f4f389b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 22 Jan 2010 01:10:31 +0100 Subject: Add view paths to Engine setup. --- railties/lib/rails/engine.rb | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'railties/lib/rails/engine.rb') diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 21c78036bd..a0139f9983 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -1,18 +1,19 @@ require 'active_support/core_ext/module/delegation' module Rails - # TODO Move I18n and views path setup + # TODO Move I18n here + # TODO Set routes namespaces class Engine < Railtie class << self attr_accessor :called_from - def root - @root ||= find_root_with_file_flag("lib") + def original_root + @original_root ||= find_root_with_file_flag("lib") end def config - @config ||= Configuration.new(root) + @config ||= Configuration.new(original_root) end def inherited(base) @@ -20,6 +21,7 @@ module Rails 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 @@ -33,7 +35,7 @@ module Rails root_path = parent != root_path && parent end - root = File.exist?("#{root_path}/flag") ? root_path : default + root = File.exist?("#{root_path}/#{flag}") ? root_path : default raise "Could not find root path for #{self}" unless root @@ -43,8 +45,8 @@ module Rails end end - delegate :root, :config, :to => :'self.class' - delegate :middleware, :to => :config + delegate :config, :to => :'self.class' + delegate :middleware, :root, :to => :config # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path, :before => :container do @@ -57,10 +59,11 @@ module Rails initializer :set_autoload_paths, :before => :container do require 'active_support/dependencies' - ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) + ActiveSupport::Dependencies.load_paths = expand_load_path(config.load_paths) ActiveSupport::Dependencies.load_once_paths = expand_load_path(config.load_once_paths) - extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths + extra = ActiveSupport::Dependencies.load_once_paths - + ActiveSupport::Dependencies.load_paths unless extra.empty? abort <<-end_error @@ -73,15 +76,24 @@ module Rails config.load_once_paths.freeze end - initializer :load_application_initializers do - Dir["#{root}/config/initializers/**/*.rb"].sort.each do |initializer| - load(initializer) - end + # Routing must be initialized after plugins to allow the former to extend the routes + initializer :add_routing_files do |app| + routes = select_existing(config.paths.config.routes) + app.route_configuration_files.concat(routes) end - # Routing must be initialized after plugins to allow the former to extend the routes - initializer :initialize_routing do |app| - app.route_configuration_files.concat(config.paths.config.routes.to_a) + initializer :add_view_paths do + views = select_existing(config.paths.app.views) + ActionController::Base.view_paths.concat(views) if defined? ActionController + ActionMailer::Base.view_paths.concat(views) if defined? ActionMailer + end + + initializer :load_application_initializers do + select_existing(config.paths.config.initializers).each do |initializers| + Dir["#{initializers}/**/*.rb"].sort.each do |initializer| + load(initializer) + end + end end # Eager load application classes @@ -100,6 +112,10 @@ module Rails private + def select_existing(paths) + paths.to_a.select { |path| File.exists?(path) }.uniq + end + def expand_load_path(load_paths) load_paths.map { |path| Dir.glob(path.to_s) }.flatten.uniq end -- cgit v1.2.3