From 6df898973c67b8b8fc6da3456777cac338219dad Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 27 Sep 2010 17:58:48 -0400 Subject: [PATCH] backport of #5706 and #5579 to 3-0-stable [#5706 state:resolved] --- .../lib/active_record/nested_attributes.rb | 6 +++--- activerecord/test/cases/nested_attributes_test.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index f5cbc47..ca47530 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -293,7 +293,7 @@ module ActiveRecord if check_existing_record && (record = send(association_name)) && (options[:update_only] || record.id.to_s == attributes['id'].to_s) - assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) + assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes) elsif attributes['id'] existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id']) @@ -376,11 +376,11 @@ module ActiveRecord elsif existing_records.count == 0 #Existing record but not yet associated existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id']) - association.send(:add_record_to_target_with_callbacks, existing_record) unless association.loaded? + association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes) assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s } - association.send(:add_record_to_target_with_callbacks, existing_record) unless association.loaded? + association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes) assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) end diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index e35f30b..a4dea4c 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -122,6 +122,22 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase man.update_attributes({:interests_attributes => {:topic => 'gardening', :id => interest.id}}) assert_equal 'gardening', interest.reload.topic end + +def test_reject_if_with_a_proc_which_returns_true_always_for_has_one + Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| true } + pirate = Pirate.new(:catchphrase => "Stop wastin' me time") + ship = pirate.create_ship(:name => 's1') + pirate.update_attributes({:ship_attributes => { :name => 's2', :id => ship.id } }) + assert_equal 's1', ship.reload.name + end + + def test_reject_if_with_a_proc_which_returns_true_always_for_has_many + Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true } + man = Man.create(:name => "John") + interest = man.interests.create(:topic => 'photography') + man.update_attributes({:interests_attributes => { :topic => 'gardening', :id => interest.id } }) + assert_equal 'photography', interest.reload.topic + end end -- 1.7.2