Skip to content

Commit

Permalink
load plugin view_paths to action_mailer view_paths and make action_ma…
Browse files Browse the repository at this point in the history
…iler use them [#2031 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
Sven Fuchs authored and josh committed Feb 22, 2009
1 parent ff1afbd commit 441e4e2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
4 changes: 3 additions & 1 deletion actionmailer/lib/action_mailer/base.rb
Expand Up @@ -254,6 +254,8 @@ class Base
private_class_method :new #:nodoc:

class_inheritable_accessor :view_paths
self.view_paths = []

cattr_accessor :logger

@@smtp_settings = {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/initializer.rb
Expand Up @@ -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

Expand Down
50 changes: 26 additions & 24 deletions railties/lib/rails/plugin/loader.rb
Expand Up @@ -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) }
Expand All @@ -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
Expand All @@ -43,29 +43,29 @@ 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|
$LOAD_PATH.insert(application_lib_index + 1, path)

ActiveSupport::Dependencies.load_paths << path

unless Rails.configuration.reload_plugins?
unless configuration.reload_plugins?
ActiveSupport::Dependencies.load_once_paths << path
end
end
end

$LOAD_PATH.uniq!
end


protected
def configure_engines
if engines.any?
Expand All @@ -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
Expand All @@ -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?
Expand All @@ -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)
Expand All @@ -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) }
Expand All @@ -180,7 +182,7 @@ def ensure_all_registered_plugins_are_loaded!
end
end
end

end
end
end
5 changes: 3 additions & 2 deletions railties/test/initializer_test.rb
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions railties/test/plugin_loader_test.rb
@@ -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
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 441e4e2

Please sign in to comment.