aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-13 15:45:49 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-13 15:45:49 -0300
commite61e1b30a5751e1e216b18edfb49661b511d7ce2 (patch)
tree4499b68485a21993f2b7d07b850cf07462df07a9 /activerecord/lib/active_record/attribute_methods
parentcc255d3424207ebb3952029df4a485c696b0dc6e (diff)
parent6f08db05c00ea05c38d7d9d7bea757a903786a83 (diff)
downloadrails-e61e1b30a5751e1e216b18edfb49661b511d7ce2.tar.gz
rails-e61e1b30a5751e1e216b18edfb49661b511d7ce2.tar.bz2
rails-e61e1b30a5751e1e216b18edfb49661b511d7ce2.zip
Merge pull request #15593 from sgrif/sg-attribute
Introduce an Attribute object to handle the type casting dance
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/before_type_cast.rb6
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb27
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb15
3 files changed, 19 insertions, 29 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
index 4365f5a1a1..d057f0941a 100644
--- a/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
+++ b/activerecord/lib/active_record/attribute_methods/before_type_cast.rb
@@ -43,7 +43,9 @@ module ActiveRecord
# task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
# task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21"
def read_attribute_before_type_cast(attr_name)
- @raw_attributes[attr_name.to_s]
+ if attr = @attributes[attr_name.to_s]
+ attr.value_before_type_cast
+ end
end
# Returns a hash of attributes before typecasting and deserialization.
@@ -57,7 +59,7 @@ module ActiveRecord
# task.attributes_before_type_cast
# # => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}
def attributes_before_type_cast
- @raw_attributes
+ @attributes.each_with_object({}) { |(k, v), h| h[k] = v.value_before_type_cast }
end
private
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index 7b7e37884b..8c1cc128f7 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -82,25 +82,16 @@ module ActiveRecord
# it has been typecast (for example, "2004-12-12" in a date column is cast
# to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name)
- # If it's cached, just return it
- # We use #[] first as a perf optimization for non-nil values. See https://gist.github.com/jonleighton/3552829.
name = attr_name.to_s
- @attributes[name] || @attributes.fetch(name) {
- column = @column_types_override[name] if @column_types_override
- column ||= @column_types[name]
-
- return @raw_attributes.fetch(name) {
- if name == 'id' && self.class.primary_key != name
- read_attribute(self.class.primary_key)
- end
- } unless column
-
- value = @raw_attributes.fetch(name) {
- return block_given? ? yield(name) : nil
- }
-
- @attributes[name] = column.type_cast_from_database(value)
- }
+ @attributes.fetch(name) {
+ if name == 'id'
+ return read_attribute(self.class.primary_key)
+ elsif block_given? && self.class.columns_hash.key?(name)
+ return yield(name)
+ else
+ return nil
+ end
+ }.value
end
private
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index b72a6219b0..246a2cd8ba 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -69,22 +69,19 @@ module ActiveRecord
def write_attribute_with_type_cast(attr_name, value, should_type_cast)
attr_name = attr_name.to_s
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
- @attributes.delete(attr_name)
- column = type_for_attribute(attr_name)
+ type = type_for_attribute(attr_name)
unless has_attribute?(attr_name) || self.class.columns_hash.key?(attr_name)
raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"
end
- # If we're dealing with a binary column, write the data to the cache
- # so we don't attempt to typecast multiple times.
- if column.binary?
- @attributes[attr_name] = value
- elsif should_type_cast
- @attributes[attr_name] = column.type_cast_from_user(value)
+ if should_type_cast
+ @attributes[attr_name] = Attribute.from_user(value, type)
+ else
+ @attributes[attr_name] = Attribute.from_database(value, type)
end
- @raw_attributes[attr_name] = value
+ value
end
end
end