aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-11-23 13:38:20 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-11-23 14:29:05 -0800
commitd717cb29136b8e4f557e6f6ddf076ae3de8476fc (patch)
treeee01ffe72cc12cf64d9dcff7b15e9c8e237b828b /activerecord
parentfe4388eb15d44612710f2fc2f81c90a890278b23 (diff)
downloadrails-d717cb29136b8e4f557e6f6ddf076ae3de8476fc.tar.gz
rails-d717cb29136b8e4f557e6f6ddf076ae3de8476fc.tar.bz2
rails-d717cb29136b8e4f557e6f6ddf076ae3de8476fc.zip
clone and dup are working on 1.8
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/base.rb15
-rw-r--r--activerecord/test/cases/dup_test.rb15
2 files changed, 27 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 7cc4f957fe..d2fa3bed35 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1591,19 +1591,28 @@ MSG
@attributes.frozen?
end
+ # Backport dup from 1.9 so that initialize_dup() gets called
+ unless Object.respond_to?(:initialize_dup)
+ def dup # :nodoc:
+ copy = super
+ copy.initialize_dup(self)
+ copy
+ end
+ end
+
# Duped objects have no id assigned and are treated as new records. Note
# that this is a "shallow" copy as it copies the object's attributes
# only, not its associations. The extent of a "deep" copy is application
# specific and is therefore left to the application to implement according
# to its need.
def initialize_dup(other)
- super
- _run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
cloned_attributes = other.clone_attributes(:read_attribute_before_type_cast)
cloned_attributes.delete(self.class.primary_key)
@attributes = cloned_attributes
+ _run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
+
@changed_attributes = {}
attributes_from_column_definition.each do |attr, orig_value|
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
@@ -1612,7 +1621,7 @@ MSG
clear_aggregation_cache
clear_association_cache
@attributes_cache = {}
- @persisted = false
+ @persisted = false
ensure_proper_type
populate_with_current_scope_attributes
diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb
index 768474e4ac..a1fc639290 100644
--- a/activerecord/test/cases/dup_test.rb
+++ b/activerecord/test/cases/dup_test.rb
@@ -9,6 +9,21 @@ module ActiveRecord
assert !Topic.new.freeze.dup.frozen?
end
+ def test_not_readonly
+ topic = Topic.first
+
+ duped = topic.dup
+ assert !topic.readonly?, 'should not be readonly'
+ end
+
+ def test_is_readonly
+ topic = Topic.first
+ topic.readonly!
+
+ duped = topic.dup
+ assert topic.readonly?, 'should be readonly'
+ end
+
def test_dup_not_persisted
topic = Topic.first
duped = topic.dup