Skip to content
This repository
Browse code

Clarify semantics of ActiveRecord::Base#respond_to? Closes #2560.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2705 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3ab3a70b7cc2d40d8c0682f814e5f0bb0caf6213 1 parent d0a3185
Jeremy Kemper authored October 22, 2005
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *1.12.1* (October 19th, 2005)
2 2
 
  3
+* Clarify semantics of ActiveRecord::Base#respond_to?  #2560 [skaes@web.de]
  4
+
3 5
 * Fixed Association#clear for associations which have not yet been accessed. #2524 [Patrick Lenz <patrick@lenz.sh>]
4 6
 
5 7
 * HABTM finders shouldn't return readonly records.  #2525 [Patrick Lenz <patrick@lenz.sh>]
39  activerecord/lib/active_record/base.rb
@@ -731,10 +731,11 @@ def content_columns
731 731
       # is available.
732 732
       def column_methods_hash
733 733
         @dynamic_methods_hash ||= column_names.inject(Hash.new(false)) do |methods, attr|
734  
-          methods[attr.to_sym]       = true
735  
-          methods["#{attr}=".to_sym] = true
736  
-          methods["#{attr}?".to_sym] = true
737  
-          methods["#{attr}_before_type_cast".to_sym] = true
  734
+          attr_name = attr.to_s
  735
+          methods[attr.to_sym]       = attr_name
  736
+          methods["#{attr}=".to_sym] = attr_name
  737
+          methods["#{attr}?".to_sym] = attr_name
  738
+          methods["#{attr}_before_type_cast".to_sym] = attr_name
738 739
           methods
739 740
         end
740 741
       end
@@ -1089,8 +1090,8 @@ def validate_find_options(options)
1089 1090
 
1090 1091
         def encode_quoted_value(value)
1091 1092
           quoted_value = connection.quote(value)
1092  
-          quoted_value = "'#{quoted_value[1..-2].gsub(/\'/, "\\\\'")}'" if quoted_value.include?("\\\'")
1093  
-          quoted_value
  1093
+          quoted_value = "'#{quoted_value[1..-2].gsub(/\'/, "\\\\'")}'" if quoted_value.include?("\\\'") # (for ruby mode) " 
  1094
+          quoted_value 
1094 1095
         end
1095 1096
     end
1096 1097
 
@@ -1269,6 +1270,11 @@ def attribute_present?(attribute)
1269 1270
         !value.blank? or value == 0
1270 1271
       end
1271 1272
 
  1273
+      # Returns true if the given attribute is in the attributes hash
  1274
+      def has_attribute?(attr_name)
  1275
+        @attributes.has_key?(attr_name.to_s)
  1276
+      end
  1277
+
1272 1278
       # Returns an array of names for the attributes available on this object sorted alphabetically.
1273 1279
       def attribute_names
1274 1280
         @attributes.keys.sort
@@ -1304,7 +1310,17 @@ def hash
1304 1310
       # A Person object with a name attribute can ask person.respond_to?("name"), person.respond_to?("name="), and
1305 1311
       # person.respond_to?("name?") which will all return true.
1306 1312
       def respond_to?(method, include_priv = false)
1307  
-        self.class.column_methods_hash[method.to_sym] || respond_to_without_attributes?(method, include_priv)
  1313
+        if attr_name = self.class.column_methods_hash[method.to_sym]
  1314
+          return true if @attributes.include?(attr_name) || attr_name == self.class.primary_key
  1315
+          return false if self.class.read_methods.include?(attr_name)
  1316
+        elsif @attributes.include?(method_name = method.to_s)
  1317
+          return true
  1318
+        elsif md = /(=|\?|_before_type_cast)$/.match(method_name)
  1319
+          return true if @attributes.include?(md.pre_match)
  1320
+        end
  1321
+        # super must be called at the end of the method, because the inherited respond_to?
  1322
+        # would return true for generated readers, even if the attribute wasn't present
  1323
+        super
1308 1324
       end
1309 1325
 
1310 1326
       # Just freeze the attributes hash, such that associations are still accessible even on destroyed records.
@@ -1424,7 +1440,7 @@ def read_attribute_before_type_cast(attr_name)
1424 1440
       # ActiveRecord::Base.generate_read_methods is set to true.
1425 1441
       def define_read_methods
1426 1442
         self.class.columns_hash.each do |name, column|
1427  
-          unless column.primary || self.class.serialized_attributes[name] || respond_to_without_attributes?(name)
  1443
+          unless self.class.serialized_attributes[name] || respond_to_without_attributes?(name)
1428 1444
             define_read_method(name.to_sym, name, column)
1429 1445
           end
1430 1446
         end
@@ -1434,15 +1450,12 @@ def define_read_methods
1434 1450
       def define_read_method(symbol, attr_name, column)
1435 1451
         cast_code = column.type_cast_code('v')
1436 1452
         access_code = cast_code ? "(v=@attributes['#{attr_name}']) && #{cast_code}" : "@attributes['#{attr_name}']"
1437  
-        body = access_code
1438 1453
 
1439  
-        # The following 3 lines behave exactly like method_missing if the
1440  
-        # attribute isn't present.
1441 1454
         unless symbol == :id
1442  
-          body = body.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
  1455
+          access_code = access_code.insert(0, "raise NoMethodError, 'missing attribute: #{attr_name}', caller unless @attributes.has_key?('#{attr_name}'); ")
1443 1456
         end
1444  
-        self.class.class_eval("def #{symbol}; #{body} end")
1445 1457
 
  1458
+        self.class.class_eval("def #{symbol}; #{access_code}; end")
1446 1459
         self.class.read_methods[attr_name] = true unless symbol == :id
1447 1460
       end
1448 1461
 
16  activerecord/test/associations_test.rb
@@ -1099,6 +1099,22 @@ def test_adding_uses_explicit_values_on_join_table
1099 1099
     assert_equal 3, project.access_level.to_i
1100 1100
   end
1101 1101
 
  1102
+  def test_hatbm_attribute_access_and_respond_to
  1103
+    project = developers(:jamis).projects[0]
  1104
+    assert project.has_attribute?("name")
  1105
+    assert project.has_attribute?("joined_on")
  1106
+    assert project.has_attribute?("access_level")
  1107
+    assert project.respond_to?("name")
  1108
+    assert project.respond_to?("name=")
  1109
+    assert project.respond_to?("name?")
  1110
+    assert project.respond_to?("joined_on")
  1111
+    assert project.respond_to?("joined_on=")
  1112
+    assert project.respond_to?("joined_on?")
  1113
+    assert project.respond_to?("access_level")
  1114
+    assert project.respond_to?("access_level=")
  1115
+    assert project.respond_to?("access_level?")
  1116
+  end
  1117
+
1102 1118
   def test_habtm_adding_before_save
1103 1119
     no_of_devels = Developer.count
1104 1120
     no_of_projects = Project.count
7  activerecord/test/base_test.rb
@@ -211,6 +211,13 @@ def test_reader_generation
211 211
     end
212 212
   end
213 213
 
  214
+  def test_non_attribute_access_and_assignment
  215
+    topic = Topic.new
  216
+    assert !topic.respond_to?("mumbo")
  217
+    assert_raises(NoMethodError) { topic.mumbo }
  218
+    assert_raises(NoMethodError) { topic.mumbo = 5 }
  219
+  end
  220
+
214 221
   def test_preserving_date_objects
215 222
     # SQL Server doesn't have a separate column type just for dates, so all are returned as time
216 223
     if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter
8  activerecord/test/finder_test.rb
@@ -96,7 +96,13 @@ def test_unexisting_record_exception_handling
96 96
   end
97 97
   
98 98
   def test_find_only_some_columns
99  
-    assert_raises(NoMethodError) { Topic.find(1, :select => "author_name").title }
  99
+    topic = Topic.find(1, :select => "author_name")
  100
+    assert_raises(NoMethodError) { topic.title }
  101
+    assert_equal "David", topic.author_name
  102
+    assert !topic.attribute_present?("title")
  103
+    assert !topic.respond_to?("title")
  104
+    assert topic.attribute_present?("author_name")
  105
+    assert topic.respond_to?("author_name")
100 106
   end
101 107
 
102 108
   def test_find_on_conditions
2  railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -130,7 +130,7 @@ def add_options!(opt)
130 130
   )
131 131
 
132 132
   MYSQL_SOCKET_LOCATIONS = [ "/tmp/mysql.sock", #default
133  
-                             "/var/run/mysqld/mysqld.sock", #debian
  133
+                             "/var/run/mysqld/mysqld.sock", #debian/gentoo
134 134
                              "/var/tmp/mysql.sock", # freebsd
135 135
                              "/var/lib/mysql/mysql.sock" ]  #fedora
136 136
 end

0 notes on commit 3ab3a70

Please sign in to comment.
Something went wrong with that request. Please try again.