aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-02-08 14:42:20 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-02-08 14:42:20 -0800
commit3f97a97fc0512967ec61017cd727d1b8f3b63738 (patch)
tree0a88bc0e21246440c232a72bdad7839d338fd4e0 /activerecord/lib/active_record/attribute_methods
parente54d5cc5dabfde7b3d5afd4dcdaf10394986178b (diff)
downloadrails-3f97a97fc0512967ec61017cd727d1b8f3b63738.tar.gz
rails-3f97a97fc0512967ec61017cd727d1b8f3b63738.tar.bz2
rails-3f97a97fc0512967ec61017cd727d1b8f3b63738.zip
always call `read_attribute` from the reader method
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb26
1 files changed, 12 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index 0738c4c580..54a5258e4c 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -94,13 +94,7 @@ module ActiveRecord
end
def internal_attribute_access_code(attr_name, cast_code)
- method = instance_cast_method(attr_name)
-
- if cache_attribute?(attr_name)
- "cached_cast_attribute('#{attr_name}', :#{method})"
- else
- "cast_attribute('#{attr_name}', :#{method})"
- end
+ "read_attribute('#{attr_name}') { |n| missing_attribute(n, caller) }"
end
def external_attribute_access_code(attr_name, cast_code)
@@ -125,14 +119,18 @@ module ActiveRecord
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name)
- if @columns_hash.key? attr_name
- if self.class.cache_attribute?(attr_name)
- @attributes_cache[attr_name] ||= @columns_hash[attr_name].type_cast(@attributes[attr_name])
- else
- @columns_hash[attr_name].type_cast @attributes[attr_name]
- end
+ column = @columns_hash.fetch(attr_name) {
+ return self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
+ }
+
+ value = @attributes.fetch(attr_name) {
+ return block_given? ? yield(attr_name) : nil
+ }
+
+ if self.class.cache_attribute?(attr_name)
+ @attributes_cache[attr_name] ||= column.type_cast(value)
else
- self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
+ column.type_cast value
end
end