Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix ActiveRecord::Base.compute_type swallowing NoMethodError. [#4751

…state:resolved]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information...
commit 0e9b9d59859efa46a82b56e0715784fa52656650 1 parent bdcf70c
@aughr aughr authored dhh committed
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.0.0 [beta 4] (June 8th, 2010)*
+* Fixed that ActiveRecord::Base.compute_type would swallow NoMethodError #4751 [Andrew Bloomgarden, Andrew White]
+
* Add index length support for MySQL. #1852 [Emili Parreno, Pratik Naik]
Example:
View
4 activerecord/lib/active_record/base.rb
@@ -1219,7 +1219,9 @@ def compute_type(type_name)
begin
constant = candidate.constantize
return constant if candidate == constant.to_s
- rescue NameError
+ rescue NameError => e
+ # We don't want to swallow NoMethodError < NameError errors
+ raise e unless e.instance_of?(NameError)
rescue ArgumentError
end
end
View
17 activerecord/test/cases/base_test.rb
@@ -2334,6 +2334,23 @@ def test_dup
assert !Minimalistic.new.freeze.dup.frozen?
end
+ def test_compute_type_success
+ assert_equal Author, ActiveRecord::Base.send(:compute_type, 'Author')
+ end
+
+ def test_compute_type_nonexistent_constant
+ assert_raises NameError do
+ ActiveRecord::Base.send :compute_type, 'NonexistentModel'
+ end
+ end
+
+ def test_compute_type_no_method_error
+ String.any_instance.stubs(:constantize).raises(NoMethodError)
+ assert_raises NoMethodError do
+ ActiveRecord::Base.send :compute_type, 'InvalidModel'
+ end
+ end
+
protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
Please sign in to comment.
Something went wrong with that request. Please try again.