diff options
author | Michael Siebert <siebertm85@googlemail.com> | 2009-12-28 19:02:01 +0100 |
---|---|---|
committer | Eloy Duran <eloy.de.enige@gmail.com> | 2009-12-28 21:09:19 +0100 |
commit | 07b615fb897017d7acfaafa88606bc88be30f6e4 (patch) | |
tree | d4d68e6f8a322e0309dca307f8cdbe018f9cd6f0 /activerecord/test | |
parent | 9c771a9608f54ebdfcb6fca819c83038489ce50d (diff) | |
download | rails-07b615fb897017d7acfaafa88606bc88be30f6e4.tar.gz rails-07b615fb897017d7acfaafa88606bc88be30f6e4.tar.bz2 rails-07b615fb897017d7acfaafa88606bc88be30f6e4.zip |
Add an :update_only option to accepts_nested_attributes_for for to-one associations. [#2563 state:resolved]
Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/nested_attributes_test.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 5367907fb7..5e4fc2b8d9 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -245,6 +245,37 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase def test_should_automatically_enable_autosave_on_the_association assert Pirate.reflect_on_association(:ship).options[:autosave] end + + def test_should_accept_update_only_option + Pirate.accepts_nested_attributes_for :ship, :update_only => true + @pirate.update_attribute(:ship_attributes, { :id => @pirate.ship.id, :name => 'Mayflower' }) + + Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? } + end + + def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true + Pirate.accepts_nested_attributes_for :ship, :update_only => true + @ship.delete + + assert_difference('Ship.count', 1) do + @pirate.reload.update_attribute(:ship_attributes, { :name => 'Mayflower' }) + end + + Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? } + end + + + def test_should_update_existing_when_update_only_is_true_and_no_id_is_given + Pirate.accepts_nested_attributes_for :ship, :update_only => true + + assert_no_difference('Ship.count') do + @pirate.reload.update_attributes(:ship_attributes => { :name => 'Mayflower' }) + end + + assert_equal 'Mayflower', @ship.reload.name + + Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? } + end end class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase |