aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-09-07 14:31:49 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-09-07 14:31:49 -0300
commitec39055456339f69e0171aec89c7d3a711137f13 (patch)
treef7acf39e8913ea8d98cfdc56e1a04dca2ba22ff7 /activerecord/lib/active_record/core.rb
parente371c6603541ffeae35d3388f39b853b0d372144 (diff)
downloadrails-ec39055456339f69e0171aec89c7d3a711137f13.tar.gz
rails-ec39055456339f69e0171aec89c7d3a711137f13.tar.bz2
rails-ec39055456339f69e0171aec89c7d3a711137f13.zip
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.
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r--activerecord/lib/active_record/core.rb14
1 files changed, 4 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 791ff4eaec..cf64985ddb 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -180,9 +180,7 @@ module ActiveRecord
@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 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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 @@ module ActiveRecord
def init_internals
pk = self.class.primary_key
-
@attributes[pk] = nil unless @attributes.key?(pk)
@aggregation_cache = {}