Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix ActiveRecord::Base.compute_type swallowing NoMethodError. [#4751
…state:resolved]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
aughr authored and dhh committed Jun 8, 2010
1 parent bdcf70c commit 0e9b9d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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:
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 0e9b9d5

Please sign in to comment.