diff options
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 7 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 20 |
2 files changed, 25 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index c0ee5ddfa8..5f3c35839b 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -65,7 +65,7 @@ module Dependencies #:nodoc: # Record that we've seen this file *before* loading it to avoid an # infinite loop with mutual dependencies. loaded << expanded - + if load? log "loading #{file_name}" begin @@ -179,6 +179,11 @@ module Dependencies #:nodoc: def load_missing_constant(from_mod, const_name) log_call from_mod, const_name + # If we have an anonymous module, all we can do is attempt to load from Object. + from_mod = Object if from_mod.name.empty? + + raise ArgumentError, "Expected #{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) + qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore name_error = NameError.new("uninitialized constant #{qualified_name}") diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index f4b173101b..f043bbd438 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -346,10 +346,28 @@ class DependenciesTest < Test::Unit::TestCase end def test_const_missing_should_not_double_load + $counting_loaded_times = 0 with_loading 'autoloading_fixtures' do require_dependency '././counting_loader' assert_equal 1, $counting_loaded_times - Dependencies.load_missing_constant Object, :CountingLoader + assert_raises(ArgumentError) { Dependencies.load_missing_constant Object, :CountingLoader } + assert_equal 1, $counting_loaded_times + end + end + + def test_const_missing_within_anonymous_module + $counting_loaded_times = 0 + m = Module.new + m.module_eval "def a() CountingLoader; end" + extend m + kls = nil + with_loading 'autoloading_fixtures' do + kls = nil + assert_nothing_raised { kls = a } + assert_equal "CountingLoader", kls.name + assert_equal 1, $counting_loaded_times + + assert_nothing_raised { kls = a } assert_equal 1, $counting_loaded_times end end |