diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-09-08 11:09:27 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-24 12:46:24 +0200 |
commit | 097240f60215b866d24aebd02cc4159bdc6e7451 (patch) | |
tree | b412557e9eaa94a1cd9639c7cba16e07f0a51819 /activerecord | |
parent | f48c56008e76e125d96a8d6cf4decc53e28414fa (diff) | |
download | rails-097240f60215b866d24aebd02cc4159bdc6e7451.tar.gz rails-097240f60215b866d24aebd02cc4159bdc6e7451.tar.bz2 rails-097240f60215b866d24aebd02cc4159bdc6e7451.zip |
reject_id option should be respected while using nested_attributes
[#5579 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/nested_attributes.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/nested_attributes_test.rb | 9 |
2 files changed, 12 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 18519a712b..7372ab3278 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -321,7 +321,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']) @@ -399,11 +399,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 9823d7aa0e..62e073ba8c 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -114,6 +114,15 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase pirate.ship_attributes = { :name => 'Hello Pearl' } assert_difference('Ship.count') { pirate.save! } end + + def test_reject_if_with_a_proc_which_returns_true_always + 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 + end class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase |