提交 08302d2f 编写于 作者: J James Le Cuirot 提交者: José Valim

When not overwriting unsaved updates in nested attributes, allow already-saved...

When not overwriting unsaved updates in nested attributes, allow already-saved records to be refreshed.
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 c7e875ab
......@@ -349,7 +349,11 @@ def load_target
begin
if !loaded?
if @target.is_a?(Array) && @target.any?
@target = find_target.map { |f| i = @target.index(f); i ? @target.delete_at(i) : f } + @target
@target = find_target.map do |f|
i = @target.index(f)
t = @target.delete_at(i) if i
(t && t.changed?) ? t : f
end + @target
else
@target = find_target
end
......
......@@ -468,15 +468,22 @@ def test_should_not_load_association_when_updating_existing_records
def test_should_not_overwrite_unsaved_updates_when_loading_association
@pirate.reload
@pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
@pirate.send(@association_name).send(:load_target)
assert_equal 'Grace OMalley', @pirate.send(@association_name).target.find { |r| r.id == @child_1.id }.name
assert_equal 'Grace OMalley', @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.name
end
def test_should_preserve_order_when_not_overwriting_unsaved_updates
@pirate.reload
@pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
@pirate.send(@association_name).send(:load_target)
assert_equal @pirate.send(@association_name).target.first.id, @child_1.id
assert_equal @child_1.id, @pirate.send(@association_name).send(:load_target).first.id
end
def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
@pirate.reload
record = @pirate.class.reflect_on_association(@association_name).klass.new(:name => 'Grace OMalley')
@pirate.send(@association_name) << record
record.save!
@pirate.send(@association_name).last.update_attributes!(:name => 'Polly')
assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name
end
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册