aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-07 13:46:22 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-13 10:20:54 -0600
commit6f08db05c00ea05c38d7d9d7bea757a903786a83 (patch)
treebdf051f4286c280d7c724ae05f18692078e1524e /activerecord/lib/active_record/attribute_methods.rb
parent70b931f846cb212f3db16f35a10094fb727a57e2 (diff)
downloadrails-6f08db05c00ea05c38d7d9d7bea757a903786a83.tar.gz
rails-6f08db05c00ea05c38d7d9d7bea757a903786a83.tar.bz2
rails-6f08db05c00ea05c38d7d9d7bea757a903786a83.zip
Introduce an Attribute object to handle the type casting dance
There's a lot more that can be moved to these, but this felt like a good place to introduce the object. Plans are: - Remove all knowledge of type casting from the columns, beyond a reference to the cast_type - Move type_cast_for_database to these objects - Potentially make them mutable, introduce a state machine, and have dirty checking handled here as well - Move `attribute`, `decorate_attribute`, and anything else that modifies types to mess with this object, not the columns hash - Introduce a collection object to manage these, reduce allocations, and not require serializing the types
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb20
1 files changed, 8 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index e626227e7e..ddccb29d09 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -261,9 +261,9 @@ module ActiveRecord
# If the result is true then check for the select case.
# For queries selecting a subset of columns, return false for unselected columns.
- # We check defined?(@raw_attributes) not to issue warnings if called on objects that
+ # We check defined?(@attributes) not to issue warnings if called on objects that
# have been allocated but not yet initialized.
- if defined?(@raw_attributes) && @raw_attributes.any? && self.class.column_names.include?(name)
+ if defined?(@attributes) && @attributes.any? && self.class.column_names.include?(name)
return has_attribute?(name)
end
@@ -280,7 +280,7 @@ module ActiveRecord
# person.has_attribute?('age') # => true
# person.has_attribute?(:nothing) # => false
def has_attribute?(attr_name)
- @raw_attributes.has_key?(attr_name.to_s)
+ @attributes.has_key?(attr_name.to_s)
end
# Returns an array of names for the attributes available on this object.
@@ -292,7 +292,7 @@ module ActiveRecord
# person.attribute_names
# # => ["id", "created_at", "updated_at", "name", "age"]
def attribute_names
- @raw_attributes.keys
+ @attributes.keys
end
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
@@ -400,11 +400,10 @@ module ActiveRecord
protected
- def clone_attributes(reader_method = :read_attribute, attributes = {}) # :nodoc:
- attribute_names.each do |name|
- attributes[name] = clone_attribute_value(reader_method, name)
+ def clone_attributes # :nodoc:
+ @attributes.each_with_object({}) do |(name, attr), h|
+ h[name] = attr.dup
end
- attributes
end
def clone_attribute_value(reader_method, attribute_name) # :nodoc:
@@ -424,7 +423,7 @@ module ActiveRecord
def attribute_method?(attr_name) # :nodoc:
# We check defined? because Syck calls respond_to? before actually calling initialize.
- defined?(@raw_attributes) && @raw_attributes.include?(attr_name)
+ defined?(@attributes) && @attributes.include?(attr_name)
end
private
@@ -465,9 +464,6 @@ module ActiveRecord
end
def typecasted_attribute_value(name)
- # FIXME: we need @attributes to be used consistently.
- # If the values stored in @attributes were already typecasted, this code
- # could be simplified
read_attribute(name)
end
end