Permalink
Browse files

Add view paths to Engine setup.

  • Loading branch information...
1 parent 7fcf859 commit 02c5137eadbb3530033d919b7aebeb6f4f389b83 @josevalim josevalim committed Jan 22, 2010
@@ -21,8 +21,7 @@ class Railtie < Rails::Railtie
initializer "action_mailer.view_paths" do |app|
# TODO: this should be combined with the logic for default config.action_mailer.view_paths
- view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes)
- ActionMailer::Base.template_root = view_path if ActionMailer::Base.view_paths.blank?
+ ActionMailer::Base.template_root = [] if ActionMailer::Base.view_paths.blank?
end
end
end
@@ -29,8 +29,7 @@ class Railtie < Rails::Railtie
# set to use Configuration#view_path.
initializer "action_controller.initialize_framework_views" do |app|
# TODO: this should be combined with the logic for default config.action_controller.view_paths
- view_path = ActionView::PathSet.type_cast(app.config.view_path, app.config.cache_classes)
- ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank?
+ ActionController::Base.view_paths = [] if ActionController::Base.view_paths.blank?
end
end
@@ -1,24 +1,24 @@
-require "fileutils"
+require 'fileutils'
module Rails
class Application < Engine
include Initializable
class << self
alias :configure :class_eval
- delegate :initialize!, :load_tasks, :load_generators, :to => :instance
+ delegate :initialize!, :load_tasks, :load_generators, :root, :to => :instance
private :new
def instance
@instance ||= new
end
def config
- @config ||= Configuration.new(root)
+ @config ||= Configuration.new(original_root)
end
- def root
- @root ||= find_root_with_file_flag("config.ru", Dir.pwd)
+ def original_root
+ @original_root ||= find_root_with_file_flag("config.ru", Dir.pwd)
end
def inherited(base)
@@ -122,16 +122,16 @@ def call(env)
app.call(env)
end
- initializer :build_middleware_stack, :after => :load_application_initializers do
- app
- end
-
initializer :add_builtin_route do |app|
if Rails.env.development?
app.route_configuration_files << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
end
end
+ initializer :build_middleware_stack, :after => :load_application_initializers do
+ app
+ end
+
# Fires the user-supplied after_initialize block (Configuration#after_initialize)
initializer :after_initialize, :after => :build_middleware_stack do
config.after_initialize_blocks.each do |block|
@@ -70,7 +70,8 @@ def config_keys
end
class Engine::Configuration < Railtie::Configuration
- attr_reader :root
+ attr_reader :root
+ attr_accessor :eager_load_paths, :load_once_paths, :load_paths
def initialize(root)
@root = root
@@ -79,14 +80,15 @@ def initialize(root)
def paths
@paths ||= begin
- paths = Rails::Application::Root.new(root)
+ paths = Rails::Application::Root.new(@root)
paths.app "app", :load_path => true
paths.app_glob "app/*", :load_path => true, :eager_load => true
- paths.app.controllers "app/controllers"
+ paths.app.controllers "app/controllers", :eager_load => true
paths.app.metals "app/metal"
paths.app.views "app/views"
paths.lib "lib", :load_path => true
paths.config "config"
+ paths.config.environment "config/environments/#{Rails.env}.rb"
paths.config.environments "config/environments", :glob => "#{Rails.env}.rb"
paths.config.initializers "config/initializers"
paths.config.locales "config/locales"
@@ -95,6 +97,10 @@ def paths
end
end
+ def root=(value)
+ @root = paths.path = Pathname.new(value).expand_path
+ end
+
def eager_load_paths
@eager_load_paths ||= paths.eager_load
end
@@ -106,6 +112,10 @@ def load_once_paths
def load_paths
@load_paths ||= paths.load_paths
end
+
+ def controller_paths
+ paths.app.controllers.to_a.uniq
+ end
end
class Configuration < Engine::Configuration
@@ -115,9 +125,7 @@ class Configuration < Engine::Configuration
:preload_frameworks, :reload_plugins, :serve_static_assets,
:time_zone, :whiny_nils
- attr_writer :cache_store, :controller_paths,
- :database_configuration_file,
- :i18n, :log_level, :log_path
+ attr_writer :cache_store, :controller_paths, :i18n, :log_level
def initialize(*)
super
@@ -134,7 +142,7 @@ def after_initialize(&blk)
def paths
@paths ||= begin
paths = super
- paths.builtin_controller builtin_directories, :eager_load => true
+ paths.app.controllers << builtin_directories
paths.config.database "config/database.yml"
paths.log "log/#{Rails.env}.log"
paths.tmp "tmp"
@@ -144,7 +152,7 @@ def paths
if File.exists?("#{root}/test/mocks/#{Rails.env}")
ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " <<
- "automatically to load paths anymore in next releases."
+ "automatically to load paths anymore in future releases"
paths.mocks_path "test/mocks/#{Rails.env}", :load_path => true
end
@@ -230,18 +238,6 @@ def log_path
paths.config.log.to_a.first
end
-
-
- # TODO Router needs this, but this wouldn't work with engines.
- # There is a high chance of plugins routes to be broken.
- def controller_paths
- @controller_paths ||= begin
- paths = [File.join(root, 'app', 'controllers')]
- paths.concat builtin_directories
- paths
- end
- end
-
def cache_store
@cache_store ||= begin
if File.exist?("#{root}/tmp/cache/")
@@ -1,25 +1,27 @@
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)
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
@@ -33,7 +35,7 @@ def find_root_with_file_flag(flag, default=nil)
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 @@ def find_root_with_file_flag(flag, default=nil)
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 @@ def find_root_with_file_flag(flag, default=nil)
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 @@ def find_root_with_file_flag(flag, default=nil)
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 @@ def find_root_with_file_flag(flag, default=nil)
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
@@ -14,6 +14,7 @@ def inherited(base)
@@plugins << base unless abstract_railtie?(base)
end
+ # This should be called railtie_name and engine_name
def plugin_name(plugin_name = nil)
@plugin_name ||= name.demodulize.underscore
@plugin_name = plugin_name if plugin_name
@@ -34,9 +34,9 @@ def setup
add_to_config <<-RUBY
config.root = '#{new_app}'
RUBY
-
+
use_frameworks []
-
+
require "#{app_path}/config/environment"
assert_equal Pathname.new(new_app), Rails.application.root
end

0 comments on commit 02c5137

Please sign in to comment.