load plugin view_paths to action_mailer view_paths and make action_mailer use them [#2031 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
Sven Fuchs 2009-02-22 10:39:56 -06:00 committed by Joshua Peek
parent ff1afbd650
commit 441e4e2235
5 changed files with 39 additions and 31 deletions

@ -254,6 +254,8 @@ class Base
private_class_method :new #:nodoc:
class_inheritable_accessor :view_paths
self.view_paths = []
cattr_accessor :logger
@@smtp_settings = {
@ -594,7 +596,7 @@ def template_path
end
def initialize_template_class(assigns)
template = ActionView::Base.new(view_paths, assigns, self)
template = ActionView::Base.new(self.class.view_paths, assigns, self)
template.template_format = default_template_format
template
end

@ -483,8 +483,8 @@ def initialize_framework_logging
def initialize_framework_views
if configuration.frameworks.include?(:action_view)
view_path = ActionView::PathSet.type_cast(configuration.view_path)
ActionMailer::Base.template_root ||= view_path if configuration.frameworks.include?(:action_mailer)
ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.empty?
ActionMailer::Base.template_root = view_path if configuration.frameworks.include?(:action_mailer) && ActionMailer::Base.view_paths.blank?
ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.blank?
end
end

@ -16,7 +16,7 @@ class Loader
def initialize(initializer)
@initializer = initializer
end
# Returns the plugins to be loaded, in the order they should be loaded.
def plugins
@plugins ||= all_plugins.select { |plugin| should_load?(plugin) }.sort { |p1, p2| order_plugins(p1, p2) }
@ -32,9 +32,9 @@ def all_plugins
@all_plugins ||= locate_plugins
@all_plugins
end
def load_plugins
plugins.each do |plugin|
plugins.each do |plugin|
plugin.load(initializer)
register_plugin_as_loaded(plugin)
end
@ -43,12 +43,12 @@ def load_plugins
ensure_all_registered_plugins_are_loaded!
end
# Adds the load paths for every plugin into the $LOAD_PATH. Plugin load paths are
# added *after* the application's <tt>lib</tt> directory, to ensure that an application
# can always override code within a plugin.
#
# Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
# Plugin load paths are also added to Dependencies.load_paths, and Dependencies.load_once_paths.
def add_plugin_load_paths
plugins.each do |plugin|
plugin.load_paths.each do |path|
@ -56,7 +56,7 @@ def add_plugin_load_paths
ActiveSupport::Dependencies.load_paths << path
unless Rails.configuration.reload_plugins?
unless configuration.reload_plugins?
ActiveSupport::Dependencies.load_once_paths << path
end
end
@ -64,8 +64,8 @@ def add_plugin_load_paths
$LOAD_PATH.uniq!
end
protected
def configure_engines
if engines.any?
@ -74,20 +74,22 @@ def configure_engines
add_engine_view_paths
end
end
def add_engine_routing_configurations
engines.select(&:routed?).collect(&:routing_file).each do |routing_file|
ActionController::Routing::Routes.add_configuration_file(routing_file)
end
end
def add_engine_controller_paths
ActionController::Routing.controller_paths += engines.collect(&:controller_path)
end
def add_engine_view_paths
# reverse it such that the last engine can overwrite view paths from the first, like with routes
ActionController::Base.view_paths += ActionView::PathSet.new(engines.collect(&:view_path).reverse)
paths = ActionView::PathSet.new(engines.collect(&:view_path).reverse)
ActionController::Base.view_paths.concat(paths)
ActionMailer::Base.view_paths.concat(paths) if configuration.frameworks.include?(:action_mailer)
end
# The locate_plugins method uses each class in config.plugin_locators to
@ -106,7 +108,7 @@ def register_plugin_as_loaded(plugin)
def configuration
initializer.configuration
end
def should_load?(plugin)
# uses Plugin#name and Plugin#valid?
enabled?(plugin) && plugin.valid?
@ -120,21 +122,21 @@ def order_plugins(plugin_a, plugin_b)
plugin_a <=> plugin_b
else
effective_order_of(plugin_a) <=> effective_order_of(plugin_b)
end
end
end
end
def effective_order_of(plugin)
if explicitly_enabled?(plugin)
registered_plugin_names.index(plugin.name)
registered_plugin_names.index(plugin.name)
else
registered_plugin_names.index('all')
end
end
end
def application_lib_index
$LOAD_PATH.index(File.join(RAILS_ROOT, 'lib')) || 0
end
end
def enabled?(plugin)
!explicit_plugin_loading_order? || registered?(plugin)
@ -155,23 +157,23 @@ def explicitly_enabled?(plugin)
def explicitly_registered?(plugin)
explicit_plugin_loading_order? && registered_plugin_names.include?(plugin.name)
end
def registered_plugins_names_plugin?(plugin)
registered_plugin_names.include?(plugin.name) || registered_plugin_names.include?('all')
end
# The plugins that have been explicitly listed with config.plugins. If this list is nil
# then it means the client does not care which plugins or in what order they are loaded,
# then it means the client does not care which plugins or in what order they are loaded,
# so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is
# non empty, we load the named plugins in the order specified.
def registered_plugin_names
configuration.plugins ? configuration.plugins.map(&:to_s) : nil
end
def loaded?(plugin_name)
initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
end
def ensure_all_registered_plugins_are_loaded!
if explicit_plugin_loading_order?
if configuration.plugins.detect {|plugin| plugin != :all && !loaded?(plugin) }
@ -180,7 +182,7 @@ def ensure_all_registered_plugins_are_loaded!
end
end
end
end
end
end

@ -182,6 +182,7 @@ def assert_framework_path(path)
class InitializerPluginLoadingTests < Test::Unit::TestCase
def setup
@configuration = Rails::Configuration.new
@configuration.frameworks -= [:action_mailer]
@configuration.plugin_paths << plugin_fixture_root_path
@initializer = Rails::Initializer.new(@configuration)
@valid_plugin_path = plugin_fixture_path('default/stubby')
@ -310,8 +311,8 @@ def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
Rails::Initializer.run(:initialize_i18n, config)
assert_equal [
File.expand_path("./test/../../activesupport/lib/active_support/locale/en.yml"),
File.expand_path("./test/../../actionpack/lib/action_view/locale/en.yml"),
File.expand_path(File.dirname(__FILE__) + "/../../activesupport/lib/active_support/locale/en.yml"),
File.expand_path(File.dirname(__FILE__) + "/../../actionpack/lib/action_view/locale/en.yml"),
"my/test/locale.yml",
"my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path }
end

@ -1,7 +1,9 @@
require 'plugin_test_helper'
$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
require 'action_controller'
require 'action_mailer'
# Mocks out the configuration
module Rails
@ -125,14 +127,15 @@ def test_should_add_engine_load_paths_to_Dependencies_load_paths
end
end
def test_engine_controllers_should_have_their_view_path_set_when_loaded
def test_engine_controllers_and_action_mailers_should_have_their_view_path_set_when_loaded
only_load_the_following_plugins!([ :engine ])
@loader.send :add_engine_view_paths
assert_equal [ File.join(plugin_fixture_path('engines/engine'), 'app', 'views') ], ActionController::Base.view_paths
assert_equal [ File.join(plugin_fixture_path('engines/engine'), 'app', 'views') ], ActionMailer::Base.view_paths
end
def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths
only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]