Skip to content
Browse files

Get initializer_test.rb to pass with the new initializer.

  • Loading branch information...
1 parent a3309e4 commit f2aea4d3eac467b85a63593ba7e2de28b2a2eb0a Yehuda Katz + Carl Lerche committed Jun 19, 2009
Showing with 114 additions and 18 deletions.
  1. +101 −5 railties/lib/initializer.rb
  2. +2 −2 railties/lib/rails/plugin/loader.rb
  3. +11 −11 railties/test/initializer_test.rb
View
106 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 @@ def framework_paths
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 @@ def config
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 @@ def self.run(initializer = nil, config = nil)
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
View
4 railties/lib/rails/plugin/loader.rb
@@ -105,7 +105,7 @@ def locate_plugins
end
def register_plugin_as_loaded(plugin)
- initializer.loaded_plugins << plugin
+ initializer.config.loaded_plugins << plugin
end
def configuration
@@ -174,7 +174,7 @@ def registered_plugin_names
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!
View
22 railties/test/initializer_test.rb
@@ -194,44 +194,44 @@ def setup
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
plugin_names = [:stubby, :acts_as_chunky_bacon, :all]
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
plugin_names = [:stubby, :all, :acts_as_chunky_bacon]
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
plugin_names = ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
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 @@ def test_load_error_messages_mention_missing_plugins_and_no_others
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 @@ def test_load_error_messages_mention_missing_plugins_and_no_others
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 @@ def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
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 @@ def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
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

0 comments on commit f2aea4d

Please sign in to comment.
Something went wrong with that request. Please try again.