Skip to content
This repository
Browse code

always call `read_attribute` from the reader method

  • Loading branch information...
commit 3f97a97fc0512967ec61017cd727d1b8f3b63738 1 parent e54d5cc
Aaron Patterson authored February 08, 2012
26  activerecord/lib/active_record/attribute_methods/read.rb
@@ -94,13 +94,7 @@ def cacheable_column?(column)
94 94
         end
95 95
 
96 96
         def internal_attribute_access_code(attr_name, cast_code)
97  
-          method = instance_cast_method(attr_name)
98  
-
99  
-          if cache_attribute?(attr_name)
100  
-            "cached_cast_attribute('#{attr_name}', :#{method})"
101  
-          else
102  
-            "cast_attribute('#{attr_name}', :#{method})"
103  
-          end
  97
+          "read_attribute('#{attr_name}') { |n| missing_attribute(n, caller) }"
104 98
         end
105 99
 
106 100
         def external_attribute_access_code(attr_name, cast_code)
@@ -125,14 +119,18 @@ def instance_cast_method(attr_name)
125 119
       # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
126 120
       # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
127 121
       def read_attribute(attr_name)
128  
-        if @columns_hash.key? attr_name
129  
-          if self.class.cache_attribute?(attr_name)
130  
-            @attributes_cache[attr_name] ||= @columns_hash[attr_name].type_cast(@attributes[attr_name])
131  
-          else
132  
-            @columns_hash[attr_name].type_cast @attributes[attr_name]
133  
-          end
  122
+        column = @columns_hash.fetch(attr_name) {
  123
+          return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
  124
+        }
  125
+
  126
+        value = @attributes.fetch(attr_name) {
  127
+          return block_given? ? yield(attr_name) : nil
  128
+        }
  129
+
  130
+        if self.class.cache_attribute?(attr_name)
  131
+          @attributes_cache[attr_name] ||= column.type_cast(value)
134 132
         else
135  
-          self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
  133
+          column.type_cast value
136 134
         end
137 135
       end
138 136
 

0 notes on commit 3f97a97

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