mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
dup is working better
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
43
activerecord/test/cases/duplication_test.rb
Normal file
43
activerecord/test/cases/duplication_test.rb
Normal 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
|
||||
Reference in New Issue
Block a user