dup is working better

This commit is contained in:
Aaron Patterson
2010-11-23 10:13:43 -08:00
parent 0250c3eca4
commit ca7b0a0d1a
3 changed files with 51 additions and 8 deletions

View File

@@ -1420,7 +1420,7 @@ MSG
@attributes = coder['attributes']
@attributes_cache, @previously_changed, @changed_attributes = {}, {}, {}
@readonly = @destroyed = @marked_for_destruction = false
@persisted = true
@persisted = false
_run_find_callbacks
_run_initialize_callbacks
end
@@ -1615,11 +1615,15 @@ MSG
@attributes.frozen?
end
def initialize_dup(other)
super
init_with 'attributes' => other.attributes
self
end
# Returns duplicated record with unfreezed attributes.
def dup
obj = super
obj.instance_variable_set('@attributes', @attributes.dup)
obj
super
end
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back

View File

@@ -1443,10 +1443,6 @@ class BasicsTest < ActiveRecord::TestCase
ActiveRecord::Base.logger = original_logger
end
def test_dup
assert !Minimalistic.new.freeze.dup.frozen?
end
def test_compute_type_success
assert_equal Author, ActiveRecord::Base.send(:compute_type, 'Author')
end

View File

@@ -0,0 +1,43 @@
require "cases/helper"
require 'models/topic'
module ActiveRecord
class DuplicationTest < ActiveRecord::TestCase
fixtures :topics
def test_dup
assert !Minimalistic.new.freeze.dup.frozen?
end
def test_dup_not_persisted
topic = Topic.first
duped = topic.dup
assert !duped.persisted?, 'topic not persisted'
assert duped.new_record?, 'topic is new'
end
def test_dup_has_no_id
topic = Topic.first
duped = topic.dup
assert_nil duped.id
end
def test_clone_persisted
topic = Topic.first
cloned = topic.clone
assert cloned.persisted?, 'topic persisted'
assert !cloned.new_record?, 'topic is not new'
end
def test_clone_keeps_frozen
topic = Topic.first
topic.freeze
cloned = topic.clone
assert cloned.persisted?, 'topic persisted'
assert !cloned.new_record?, 'topic is not new'
assert cloned.frozen?, 'topic is frozen'
end
end
end