Skip to content

Commit

Permalink
Raise NameError instead of ArgumentError in ActiveSupport::Dependencies
Browse files Browse the repository at this point in the history
ActiveSupport::Dependencies now raises NameError if it finds an existing
constant in load_missing_constant. This better reflects the nature of
the error which is usually caused by calling constantize on a nested constant.

Closes rails#1423
  • Loading branch information
pixeltrix committed Jun 1, 2011
1 parent 5ca2e57 commit f1fe3c2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
1 change: 0 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -1308,7 +1308,6 @@ def compute_type(type_name)
rescue NameError => e
# We don't want to swallow NoMethodError < NameError errors
raise e unless e.instance_of?(NameError)
rescue ArgumentError
end
end

Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -1773,6 +1773,13 @@ def test_compute_type_no_method_error
end
end

def test_compute_type_argument_error
ActiveSupport::Dependencies.stubs(:constantize).raises(ArgumentError)
assert_raises ArgumentError do
ActiveRecord::Base.send :compute_type, 'InvalidModel'
end
end

def test_clear_cache!
# preheat cache
c1 = Post.columns
Expand Down
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*

* ActiveSupport::Dependencies now raises NameError if it finds an existing constant in load_missing_constant. This better reflects the nature of the error which is usually caused by calling constantize on a nested constant. [Andrew White]

* Deprecated ActiveSupport::SecureRandom in favour of SecureRandom from the standard library [Jon Leighton]

* New reporting method Kernel#quietly. [fxn]
Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/dependencies.rb
Expand Up @@ -474,7 +474,7 @@ def load_missing_constant(from_mod, const_name)
raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
end

raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if local_const_defined?(from_mod, const_name)
raise NameError, "#{from_mod} is not missing constant #{const_name}!" if local_const_defined?(from_mod, const_name)

qualified_name = qualified_name_for from_mod, const_name
path_suffix = qualified_name.underscore
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/dependencies_test.rb
Expand Up @@ -441,7 +441,7 @@ def test_const_missing_should_not_double_load
with_autoloading_fixtures do
require_dependency '././counting_loader'
assert_equal 1, $counting_loaded_times
assert_raise(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader }
assert_raise(NameError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader }
assert_equal 1, $counting_loaded_times
end
end
Expand Down

0 comments on commit f1fe3c2

Please sign in to comment.