From 392df8e06ca24c1d05093cc8de564a38c707b3e3 Mon Sep 17 00:00:00 2001 From: James Rosen Date: Tue, 2 Sep 2008 17:23:46 -0400 Subject: [PATCH] refactored respond_to? in anticipation of adding checks on method privacy --- .../lib/active_record/attribute_methods.rb | 22 +++++++++---------- 1 files changed, 10 insertions(+), 12 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index fab16a4..0191ecb 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -336,22 +336,20 @@ module ActiveRecord alias :respond_to_without_attributes? :respond_to? def respond_to?(method, include_priv = false) method_name = method.to_s - if super - return true - elsif !self.class.generated_methods? + return true if super + + unless self.class.generated_methods? self.class.define_attribute_methods - if self.class.generated_methods.include?(method_name) - return true - end + return true if self.class.generated_methods.include?(method_name) end - - if @attributes.nil? - return super - elsif @attributes.include?(method_name) - return true - elsif md = self.class.match_attribute_method?(method_name) + + return super if @attributes.nil? + return true if @attributes.include?(method_name) + + if md = self.class.match_attribute_method?(method_name) return true if @attributes.include?(md.pre_match) end + super end -- 1.5.6.1.1071.g76fb From 166a7861439def4ce8ed6e5000e2b43c43e8a858 Mon Sep 17 00:00:00 2001 From: James Rosen Date: Tue, 2 Sep 2008 18:01:56 -0400 Subject: [PATCH] ActiveRecords' respond_to? now checks for private methods shadowing attributes; added tests --- .../lib/active_record/attribute_methods.rb | 1 + activerecord/test/cases/attribute_methods_test.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 0191ecb..1b2e4d0 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -337,6 +337,7 @@ module ActiveRecord def respond_to?(method, include_priv = false) method_name = method.to_s return true if super + return false if private_methods.include?(method_name) unless self.class.generated_methods? self.class.define_attribute_methods diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 7999e29..f164797 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -47,6 +47,22 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert_equal ['title', 1, 2, 3], topic.send(meth, 1, 2, 3) end end + + def test_does_not_respond_to_private_method_shadowing_attribute + topic = @target.new(:title => 'HR Report') + class < :value2} -- 1.5.6.1.1071.g76fb