Skip to content
This repository
Browse code

Get initializer_test.rb to pass with the new initializer.

  • Loading branch information...
commit f2aea4d3eac467b85a63593ba7e2de28b2a2eb0a 1 parent a3309e4
Yehuda Katz + Carl Lerche authored
106 railties/lib/initializer.rb
@@ -2,13 +2,21 @@ module Rails
2 2 class Configuration
3 3 attr_accessor :cache_classes, :load_paths, :eager_load_paths, :framework_paths,
4 4 :load_once_paths, :gems_dependencies_loaded, :after_initialize_blocks,
5   - :frameworks, :framework_root_path, :root_path
  5 + :frameworks, :framework_root_path, :root_path, :plugin_paths, :plugins,
  6 + :plugin_loader, :plugin_locators, :gems, :loaded_plugins, :reload_plugins,
  7 + :i18n
6 8
7 9 def initialize
8 10 @framework_paths = []
9 11 @load_once_paths = []
10 12 @after_initialize_blocks = []
11 13 @frameworks = []
  14 + @plugin_paths = []
  15 + @loaded_plugins = []
  16 + @plugin_loader = default_plugin_loader
  17 + @plugin_locators = default_plugin_locators
  18 + @gems = default_gems
  19 + @i18n = default_i18n
12 20 end
13 21
14 22 def after_initialize(&blk)
@@ -29,6 +37,44 @@ def framework_paths
29 37 def framework_root_path
30 38 defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails"
31 39 end
  40 +
  41 + # TODO: Fix this when there is an application object
  42 + def middleware
  43 + require 'action_controller'
  44 + ActionController::Dispatcher.middleware
  45 + end
  46 +
  47 + def default_plugin_loader
  48 + require 'rails/plugin/loader'
  49 + Plugin::Loader
  50 + end
  51 +
  52 + def default_plugin_locators
  53 + require 'rails/plugin/locator'
  54 + locators = []
  55 + locators << Plugin::GemLocator if defined? Gem
  56 + locators << Plugin::FileSystemLocator
  57 + end
  58 +
  59 + def default_i18n
  60 + i18n = Rails::OrderedOptions.new
  61 + i18n.load_path = []
  62 +
  63 + if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
  64 + i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
  65 + i18n.load_path.flatten!
  66 + end
  67 +
  68 + i18n
  69 + end
  70 +
  71 + def default_gems
  72 + []
  73 + end
  74 +
  75 + def reload_plugins?
  76 + @reload_plugins
  77 + end
32 78 end
33 79
34 80 class Initializer
@@ -52,21 +98,26 @@ def config
52 98 def gems_dependencies_loaded
53 99 config.gems_dependencies_loaded
54 100 end
55   - end
56 101
57   - def plugin_loader
58   - @plugin_loader ||= configuration.plugin_loader.new(self)
  102 + def plugin_loader
  103 + @plugin_loader ||= configuration.plugin_loader.new(self)
  104 + end
59 105 end
60 106
61 107 def gems_dependencies_loaded
62 108 self.class.gems_dependencies_loaded
63 109 end
  110 +
  111 + def plugin_loader
  112 + self.class.plugin_loader
  113 + end
64 114 end
65 115
66 116 class Runner
67 117
68 118 attr_reader :names, :initializers
69   - attr_writer :config
  119 + attr_accessor :config
  120 + alias configuration config
70 121
71 122 def initialize(parent = nil)
72 123 @names = parent ? parent.names.dup : {}
@@ -596,4 +647,49 @@ def self.run(initializer = nil, config = nil)
596 647 ActiveSupport::Dependencies.unhook!
597 648 end
598 649 end
  650 +end
  651 +
  652 +# Needs to be duplicated from Active Support since its needed before Active
  653 +# Support is available. Here both Options and Hash are namespaced to prevent
  654 +# conflicts with other implementations AND with the classes residing in Active Support.
  655 +# ---
  656 +# TODO: w0t?
  657 +module Rails
  658 + class << self
  659 + def root
  660 + Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
  661 + end
  662 + end
  663 +
  664 + class OrderedOptions < Array #:nodoc:
  665 + def []=(key, value)
  666 + key = key.to_sym
  667 +
  668 + if pair = find_pair(key)
  669 + pair.pop
  670 + pair << value
  671 + else
  672 + self << [key, value]
  673 + end
  674 + end
  675 +
  676 + def [](key)
  677 + pair = find_pair(key.to_sym)
  678 + pair ? pair.last : nil
  679 + end
  680 +
  681 + def method_missing(name, *args)
  682 + if name.to_s =~ /(.*)=$/
  683 + self[$1.to_sym] = args.first
  684 + else
  685 + self[name]
  686 + end
  687 + end
  688 +
  689 + private
  690 + def find_pair(key)
  691 + self.each { |i| return i if i.first == key }
  692 + return false
  693 + end
  694 + end
599 695 end
4 railties/lib/rails/plugin/loader.rb
@@ -105,7 +105,7 @@ def locate_plugins
105 105 end
106 106
107 107 def register_plugin_as_loaded(plugin)
108   - initializer.loaded_plugins << plugin
  108 + initializer.config.loaded_plugins << plugin
109 109 end
110 110
111 111 def configuration
@@ -174,7 +174,7 @@ def registered_plugin_names
174 174 end
175 175
176 176 def loaded?(plugin_name)
177   - initializer.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
  177 + initializer.config.loaded_plugins.detect { |plugin| plugin.name == plugin_name.to_s }
178 178 end
179 179
180 180 def ensure_all_registered_plugins_are_loaded!
22 railties/test/initializer_test.rb
@@ -194,20 +194,20 @@ def setup
194 194 def test_no_plugins_are_loaded_if_the_configuration_has_an_empty_plugin_list
195 195 only_load_the_following_plugins! []
196 196 @initializer.run :load_plugins
197   - assert_equal [], @initializer.loaded_plugins
  197 + assert_equal [], @configuration.loaded_plugins
198 198 end
199 199
200 200 def test_only_the_specified_plugins_are_located_in_the_order_listed
201 201 plugin_names = [:plugin_with_no_lib_dir, :acts_as_chunky_bacon]
202 202 only_load_the_following_plugins! plugin_names
203 203 load_plugins!
204   - assert_plugins plugin_names, @initializer.loaded_plugins
  204 + assert_plugins plugin_names, @configuration.loaded_plugins
205 205 end
206 206
207 207 def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched
208 208 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
209 209 load_plugins!
210   - assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip
  210 + assert_plugins [:a, :acts_as_chunky_bacon, :engine, :gemlike, :plugin_with_no_lib_dir, :stubby], @configuration.loaded_plugins, failure_tip
211 211 end
212 212
213 213 def test_all_plugins_loaded_when_all_is_used
@@ -215,7 +215,7 @@ def test_all_plugins_loaded_when_all_is_used
215 215 only_load_the_following_plugins! plugin_names
216 216 load_plugins!
217 217 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
218   - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip
  218 + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :engine, :gemlike, :plugin_with_no_lib_dir], @configuration.loaded_plugins, failure_tip
219 219 end
220 220
221 221 def test_all_plugins_loaded_after_all
@@ -223,7 +223,7 @@ def test_all_plugins_loaded_after_all
223 223 only_load_the_following_plugins! plugin_names
224 224 load_plugins!
225 225 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
226   - assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip
  226 + assert_plugins [:stubby, :a, :engine, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @configuration.loaded_plugins, failure_tip
227 227 end
228 228
229 229 def test_plugin_names_may_be_strings
@@ -231,7 +231,7 @@ def test_plugin_names_may_be_strings
231 231 only_load_the_following_plugins! plugin_names
232 232 load_plugins!
233 233 failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
234   - assert_plugins plugin_names, @initializer.loaded_plugins, failure_tip
  234 + assert_plugins plugin_names, @configuration.loaded_plugins, failure_tip
235 235 end
236 236
237 237 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
250 250 flunk "Expected a LoadError but did not get one"
251 251 rescue LoadError => e
252 252 failure_tip = "It's likely someone renamed or deleted plugin fixtures without updating this test"
253   - assert_plugins valid_plugin_names, @initializer.loaded_plugins, failure_tip
  253 + assert_plugins valid_plugin_names, @configuration.loaded_plugins, failure_tip
254 254 invalid_plugin_names.each do |plugin|
255 255 assert_match(/#{plugin.to_s}/, e.message, "LoadError message should mention plugin '#{plugin}'")
256 256 end
@@ -264,7 +264,7 @@ def test_load_error_messages_mention_missing_plugins_and_no_others
264 264 def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path
265 265 only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon]
266 266
267   - @initializer.add_plugin_load_paths
  267 + @initializer.run(:add_plugin_load_paths)
268 268
269 269 assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib'))
270 270 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
273 273 private
274 274
275 275 def load_plugins!
276   - @initializer.add_plugin_load_paths
277   - @initializer.load_plugins
  276 + @initializer.run(:add_plugin_load_paths)
  277 + @initializer.run(:load_plugins)
278 278 end
279 279 end
280 280
@@ -314,7 +314,7 @@ def test_config_defaults_and_settings_should_be_added_to_i18n_defaults
314 314 File.expand_path(File.dirname(__FILE__) + "/../../activemodel/lib/active_model/locale/en.yml"),
315 315 File.expand_path(File.dirname(__FILE__) + "/../../activerecord/lib/active_record/locale/en.yml"),
316 316 "my/test/locale.yml",
317   - "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /^\./ ? File.expand_path(path) : path }
  317 + "my/other/locale.yml" ], I18n.load_path.collect { |path| path =~ /\.\./ ? File.expand_path(path) : path }
318 318 end
319 319
320 320 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.