From 0e9b9d59859efa46a82b56e0715784fa52656650 Mon Sep 17 00:00:00 2001 From: Andrew Bloomgarden Date: Tue, 1 Jun 2010 22:47:34 -0700 Subject: Fix ActiveRecord::Base.compute_type swallowing NoMethodError. [#4751 state:resolved] Signed-off-by: David Heinemeier Hansson --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 4 +++- activerecord/test/cases/base_test.rb | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 348248e849..e47d291eb3 100644 --- a/activerecord/CHANGELOG +++ b/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: diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index aa2826fb33..7cff6d9f1a 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1219,7 +1219,9 @@ module ActiveRecord #:nodoc: 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 diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 36c572b5e7..5c175de6d4 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -2334,6 +2334,23 @@ class BasicsTest < ActiveRecord::TestCase 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 -- cgit v1.2.3