Permalink
Browse files

Minor refactor in ActiveRecord#initialize_dup

* There is no need to delete the primary key from cloned attributes,
  since it sets the same pk to nil afterwards.
* Check for empty? instead of any? to run initialize callbacks.
  • Loading branch information...
1 parent e371c66 commit ec39055456339f69e0171aec89c7d3a711137f13 @carlosantoniodasilva carlosantoniodasilva committed Sep 7, 2012
@@ -1,4 +1,3 @@
-
module ActiveRecord
module AttributeMethods
module Query
@@ -180,9 +180,7 @@ def initialize(attributes = nil, options = {})
@columns_hash = self.class.column_types.dup
init_internals
-
ensure_proper_type
-
populate_with_current_scope_attributes
assign_attributes(attributes, options) if attributes
@@ -202,7 +200,7 @@ def initialize(attributes = nil, options = {})
# post.init_with('attributes' => { 'title' => 'hello world' })
# post.title # => 'hello world'
def init_with(coder)
- @attributes = self.class.initialize_attributes(coder['attributes'])
+ @attributes = self.class.initialize_attributes(coder['attributes'])
@columns_hash = self.class.column_types.merge(coder['column_types'] || {})
init_internals
@@ -246,12 +244,10 @@ def initialize_dup(other) # :nodoc:
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
self.class.initialize_attributes(cloned_attributes, :serialized => false)
- cloned_attributes.delete(self.class.primary_key)
-
@attributes = cloned_attributes
@attributes[self.class.primary_key] = nil
- run_callbacks(:initialize) if _initialize_callbacks.any?
+ run_callbacks(:initialize) unless _initialize_callbacks.empty?
@changed_attributes = {}
self.class.column_defaults.each do |attr, orig_value|
@@ -310,7 +306,8 @@ def hash
# Freeze the attributes hash such that associations are still accessible, even on destroyed records.
def freeze
- @attributes.freeze; self
+ @attributes.freeze
+ self
end
# Returns +true+ if the attributes hash has been frozen.
@@ -322,8 +319,6 @@ def frozen?
def <=>(other_object)
if other_object.is_a?(self.class)
self.to_key <=> other_object.to_key
- else
- nil
end
end
@@ -380,7 +375,6 @@ def to_ary # :nodoc:
def init_internals
pk = self.class.primary_key
-
@attributes[pk] = nil unless @attributes.key?(pk)
@aggregation_cache = {}
@@ -1,4 +1,3 @@
-
module ActiveRecord
module Sanitization
extend ActiveSupport::Concern

0 comments on commit ec39055

Please sign in to comment.