Skip to content

Commit

Permalink
reject_id option should be respected while using nested_attributes
Browse files Browse the repository at this point in the history
[#5579 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Sep 24, 2010
1 parent f48c560 commit 097240f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/nested_attributes.rb
Expand Up @@ -321,7 +321,7 @@ def assign_nested_attributes_for_one_to_one_association(association_name, attrib

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'])
Expand Down Expand Up @@ -399,11 +399,11 @@ def assign_nested_attributes_for_collection_association(association_name, attrib

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
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/nested_attributes_test.rb
Expand Up @@ -114,6 +114,15 @@ def test_reject_if_with_indifferent_keys
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
Expand Down

0 comments on commit 097240f

Please sign in to comment.