Permalink
Browse files

Refactored method arel_attributes_values.

  • Loading branch information...
1 parent dc80715 commit 6024d5353469e04722beaba994e059cd5c3fb7a8 @robinroestenburg robinroestenburg committed Mar 5, 2012
Showing with 34 additions and 20 deletions.
  1. +34 −20 activerecord/lib/active_record/attribute_methods.rb
View
54 activerecord/lib/active_record/attribute_methods.rb
@@ -214,29 +214,15 @@ def clone_attribute_value(reader_method, attribute_name)
value
end
- # Returns a copy of the attributes hash where all the values have been safely quoted for use in
- # an Arel insert/update method.
- def arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
+ # Returns a copy of the attributes hash where all the values have been
+ # typecasted for use in an Arel insert/update method.
+ def arel_attributes_values(pk_attr_allowed = true, readonly_attr_allowed = true, attribute_names = @attributes.keys)
attrs = {}
- klass = self.class
- arel_table = klass.arel_table
+ arel_table = self.class.arel_table
attribute_names.each do |name|
- if (column = column_for_attribute(name)) && (include_primary_key || !column.primary)
-
- if include_readonly_attributes || !self.class.readonly_attributes.include?(name)
-
- value = if klass.serialized_attributes.include?(name)
- @attributes[name].serialized_value
- else
- # FIXME: we need @attributes to be used consistently.
- # If the values stored in @attributes were already type
- # casted, this code could be simplified
- read_attribute(name)
- end
-
- attrs[arel_table[name]] = value
- end
+ if attribute_allowed?(pk_attr_allowed, readonly_attr_allowed, name)
+ attrs[arel_table[name]] = typecasted_attribute_value(name)
end
end
@@ -246,5 +232,33 @@ def arel_attributes_values(include_primary_key = true, include_readonly_attribut
def attribute_method?(attr_name)
defined?(@attributes) && @attributes.include?(attr_name)
end
+
+ private
+
+ def attribute_allowed?(pk_attribute_allowed, readonly_attribute_allowed, name)
+ return unless column = column_for_attribute(name)
+
+ (pk_attribute_allowed || !pk_attribute?(column)) &&
+ (readonly_attribute_allowed || !readonly_attribute?(name))
+ end
+
+ def readonly_attribute?(name)
+ self.class.readonly_attributes.include?(name)
+ end
+
+ def pk_attribute?(column)
+ column.primary
+ end
+
+ def typecasted_attribute_value(name)
+ if self.class.serialized_attributes.include?(name)
+ @attributes[name].serialized_value
+ else
+ # 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
end
end

0 comments on commit 6024d53

Please sign in to comment.