Skip to content
This repository
Browse code

backport of #5706 and #5579 to 3-0-stable

[#5706 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 2525bfc5dd255a865ae3fae77b5daff833396758 1 parent e0411f3
Neeraj Singh neerajdotname authored josevalim committed
6 activerecord/lib/active_record/nested_attributes.rb
@@ -293,7 +293,7 @@ def assign_nested_attributes_for_one_to_one_association(association_name, attrib
293 293
294 294 if check_existing_record && (record = send(association_name)) &&
295 295 (options[:update_only] || record.id.to_s == attributes['id'].to_s)
296   - assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy])
  296 + assign_to_or_mark_for_destruction(record, attributes, options[:allow_destroy]) unless call_reject_if(association_name, attributes)
297 297
298 298 elsif attributes['id']
299 299 existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id'])
@@ -376,11 +376,11 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
376 376
377 377 elsif existing_records.count == 0 #Existing record but not yet associated
378 378 existing_record = self.class.reflect_on_association(association_name).klass.find(attributes['id'])
379   - association.send(:add_record_to_target_with_callbacks, existing_record) unless association.loaded?
  379 + association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes)
380 380 assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
381 381
382 382 elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
383   - association.send(:add_record_to_target_with_callbacks, existing_record) unless association.loaded?
  383 + association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes)
384 384 assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
385 385
386 386 end
16 activerecord/test/cases/nested_attributes_test.rb
@@ -122,6 +122,22 @@ def test_has_many_association_updating_a_single_record
122 122 man.update_attributes({:interests_attributes => {:topic => 'gardening', :id => interest.id}})
123 123 assert_equal 'gardening', interest.reload.topic
124 124 end
  125 +
  126 +def test_reject_if_with_a_proc_which_returns_true_always_for_has_one
  127 + Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| true }
  128 + pirate = Pirate.new(:catchphrase => "Stop wastin' me time")
  129 + ship = pirate.create_ship(:name => 's1')
  130 + pirate.update_attributes({:ship_attributes => { :name => 's2', :id => ship.id } })
  131 + assert_equal 's1', ship.reload.name
  132 + end
  133 +
  134 + def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
  135 + Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }
  136 + man = Man.create(:name => "John")
  137 + interest = man.interests.create(:topic => 'photography')
  138 + man.update_attributes({:interests_attributes => { :topic => 'gardening', :id => interest.id } })
  139 + assert_equal 'photography', interest.reload.topic
  140 + end
125 141
126 142 end
127 143

0 comments on commit 2525bfc

Please sign in to comment.
Something went wrong with that request. Please try again.