From c037b98b73024950e531848092affd8df54e354c Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Fri, 24 Oct 2008 15:50:03 -0500 Subject: [PATCH] Fix bug in raising on attempt to access private method of an AR instance. --- .../lib/active_record/attribute_methods.rb | 2 +- activerecord/test/cases/attribute_methods_test.rb | 28 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 1c75352..177d156 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -233,7 +233,7 @@ module ActiveRecord method_name = method_id.to_s if self.class.private_method_defined?(method_name) - raise NoMethodError("Attempt to call private method", method_name, args) + raise NoMethodError.new("Attempt to call private method", method_name, args) end # If we haven't generated any methods yet, generate them, then diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 160716f..7e3d92e 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -237,6 +237,16 @@ class AttributeMethodsTest < ActiveRecord::TestCase topic.send(:title) end + def test_read_attributes_respect_access_control_exception_message + privatize("title") + + topic = @target.new(:title => "The pros and cons of programming naked.") + assert !topic.respond_to?(:title) + t = topic.title + rescue Exception => e + assert_equal e.message, "Attempt to call private method" + end + def test_write_attributes_respect_access_control privatize("title=(value)") @@ -246,6 +256,15 @@ class AttributeMethodsTest < ActiveRecord::TestCase topic.send(:title=, "Very large pants") end + def test_write_attributes_respect_access_control_exception_message + privatize("title=(value)") + + topic = @target.new + topic.title = "Pants" + rescue Exception => e + assert_equal e.message, "Attempt to call private method" + end + def test_question_attributes_respect_access_control privatize("title?") @@ -255,6 +274,15 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert topic.send(:title?) end + def test_question_attributes_respect_access_control_exception_message + privatize("title?") + + topic = @target.new(:title => "Isaac Newton's pants") + t = topic.title? + rescue Exception => e + assert_equal e.message, "Attempt to call private method" + end + def test_bulk_update_respects_access_control privatize("title=(value)") -- 1.5.4.3