diff options
-rw-r--r-- | railties/lib/initializer.rb | 106 | ||||
-rw-r--r-- | railties/lib/rails/plugin/loader.rb | 4 | ||||
-rw-r--r-- | railties/test/initializer_test.rb | 22 |
3 files changed, 114 insertions, 18 deletions
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 55fef30dbe..b2dfb6c970 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -2,13 +2,21 @@ module Rails class Configuration attr_accessor :cache_classes, :load_paths, :eager_load_paths, :framework_paths, :load_once_paths, :gems_dependencies_loaded, :after_initialize_blocks, - :frameworks, :framework_root_path, :root_path + :frameworks, :framework_root_path, :root_path, :plugin_paths, :plugins, + :plugin_loader, :plugin_locators, :gems, :loaded_plugins, :reload_plugins, + :i18n def initialize @framework_paths = [] @load_once_paths = [] @after_initialize_blocks = [] @frameworks = [] + @plugin_paths = [] + @loaded_plugins = [] + @plugin_loader = default_plugin_loader + @plugin_locators = default_plugin_locators + @gems = default_gems + @i18n = default_i18n end def after_initialize(&blk) @@ -29,6 +37,44 @@ module Rails def framework_root_path defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails" end + + # TODO: Fix this when there is an application object + def middleware + require 'action_controller' + ActionController::Dispatcher.middleware + end + + def default_plugin_loader + require 'rails/plugin/loader' + Plugin::Loader + end + + def default_plugin_locators + require 'rails/plugin/locator' + locators = [] + locators << Plugin::GemLocator if defined? Gem + locators << Plugin::FileSystemLocator + end + + def default_i18n + i18n = Rails::OrderedOptions.new + i18n.load_path = [] + + if File.exist?(File.join(RAILS_ROOT, 'config', 'locales')) + i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')] + i18n.load_path.flatten! + end + + i18n + end + + def default_gems + [] + end + + def reload_plugins? + @reload_plugins + end end class Initializer @@ -52,21 +98,26 @@ module Rails def gems_dependencies_loaded config.gems_dependencies_loaded end - end - def plugin_loader - @plugin_loader ||= configuration.plugin_loader.new(self) + def plugin_loader + @plugin_loader ||= configuration.plugin_loader.new(self) + end end def gems_dependencies_loaded self.class.gems_dependencies_loaded end + + def plugin_loader + self.class.plugin_loader + end end class Runner attr_reader :names, :initializers - attr_writer :config + attr_accessor :config + alias configuration config def initialize(parent = nil) @names = parent ? parent.names.dup : {} @@ -596,4 +647,49 @@ Run `rake gems:install` to install the missing gems. ActiveSupport::Dependencies.unhook! end end +end + +# Needs to be duplicated from Active Support since its needed before Active +# Support is available. Here both Options and Hash are namespaced to prevent +# conflicts with other implementations AND with the classes residing in Active Support. +# --- +# TODO: w0t? +module Rails + class << self + def root + Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT) + end + end + + class OrderedOptions < Array #:nodoc: + def []=(key, value) + key = key.to_sym + + if pair = find_pair(key) + pair.pop + pair << value + else + self << [key, value] + end + end + + def [](key) + pair = find_pair(key.to_sym) + pair ? pair.last : nil + end + + def method_missing(name, *args) + if name.to_s =~ /(.*)=$/ + self[$1.to_sym] = args.first + else + self[name] + end + end + + private + def find_pair(key) + self.each { |i| return i if i.first == key } + return false + end + end end
\ No newline at end of file diff --git a/railties/lib/rails/plugin/loader.rb b/railties/lib/rails/plugin/loader.rb index bc22dfc591..7ea9c7c0f3 100644 --- a/railties/lib/rails/plugin/loader.rb +++ b/railties/lib/rails/plugin/loader.rb @@ -105,7 +105,7 @@ module Rails end def register_plugin_as_loaded(plugin) - initializer.loaded_plugins << plugin + initializer.config.loaded_plugins << plugin end def configuration @@ -174,7 +174,7 @@ module Rails end def loaded?(plugin_name) - initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s } + initializer.config.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s } end def ensure_all_registered_plugins_are_loaded! diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 744b0d32d1..5caa5858a4 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -194,20 +194,20 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list only_load_the_following_plugins! [] @initializer.run :load_plugins - assert_equal [], @initializer.loaded_plugins + assert_equal [], @configuration.loaded_plugins end def test_only_the_specified_plugins_are_located_in_the_order_listed plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon] only_load_the_following_plugins! plugin_names load_plugins! - assert_plugins plugin_names, @initializer.loaded_plugins + assert_plugins plugin_names, @configuration.loaded_plugins end def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched failure_tip = "It's likely someone has added a new plugin fixture without updating this list" load_plugins! - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @configuration.loaded_plugins, failure_tip end def test_all_plugins_loaded_when_all_is_used @@ -215,7 +215,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @configuration.loaded_plugins, failure_tip end def test_all_plugins_loaded_after_all @@ -223,7 +223,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @configuration.loaded_plugins, failure_tip end def test_plugin_names_may_be_strings @@ -231,7 +231,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip + assert_plugins plugin_names, @configuration.loaded_plugins, failure_tip end def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error @@ -250,7 +250,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase flunk "Expected a LoadError but did not get one" rescue LoadError => e failure_tip = "It's likely someone renamed or deleted plugin fixtures without updating this test" - assert_plugins valid_plugin_names, @initializer.loaded_plugins, failure_tip + assert_plugins valid_plugin_names, @configuration.loaded_plugins, failure_tip invalid_plugin_names.each do |plugin| assert_match(/#{plugin.to_s}/, e.message, "LoadError message should mention plugin '#{plugin}'") end @@ -264,7 +264,7 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon] - @initializer.add_plugin_load_paths + @initializer.run(:add_plugin_load_paths) assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) @@ -273,8 +273,8 @@ class InitializerPluginLoadingTests < Test::Unit::TestCase private def load_plugins! - @initializer.add_plugin_load_paths - @initializer.load_plugins + @initializer.run(:add_plugin_load_paths) + @initializer.run(:load_plugins) end end @@ -314,7 +314,7 @@ class InitializerSetupI18nTests < Test::Unit::TestCase File.expand_path(File.dirname(__FILE__) + "/../../activemodel/lib/active_model/locale/en.yml"), File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"), "my/test/locale.yml", - "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path } + "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /\.\./ ? File.expand_path(path) : path } end def test_setting_another_default_locale |