Skip to content

Commit

Permalink
Fix autosave association to skip validation if it is marked for destr…
Browse files Browse the repository at this point in the history
…uction. [#2064 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information
Dmitry Polushkin authored and alloy committed Sep 12, 2009
1 parent 2420d62 commit c52a50e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 6 additions & 8 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -244,18 +244,16 @@ def validate_collection_association(reflection)

# Returns whether or not the association is valid and applies any errors to
# the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
# enabled records if they're marked_for_destruction?.
# enabled records if they're marked_for_destruction? or destroyed.
def association_valid?(reflection, association)
return true if association.destroyed?
return true if association.destroyed? || association.marked_for_destruction?

unless valid = association.valid?
if reflection.options[:autosave]
unless association.marked_for_destruction?
association.errors.each_error do |attribute, error|
error = error.dup
error.attribute = "#{reflection.name}_#{attribute}"
errors.add(error) unless errors.on(error.attribute)
end
association.errors.each_error do |attribute, error|
error = error.dup
error.attribute = "#{reflection.name}_#{attribute}"
errors.add(error) unless errors.on(error.attribute)
end
else
errors.add(reflection.name)
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -537,6 +537,7 @@ def test_should_skip_validation_on_a_child_association_if_marked_for_destruction
assert !@pirate.valid?

@pirate.ship.mark_for_destruction
@pirate.ship.expects(:valid?).never
assert_difference('Ship.count', -1) { @pirate.save! }
end

Expand Down Expand Up @@ -574,6 +575,7 @@ def test_should_skip_validation_on_a_parent_association_if_marked_for_destructio
assert !@ship.valid?

@ship.pirate.mark_for_destruction
@ship.pirate.expects(:valid?).never
assert_difference('Pirate.count', -1) { @ship.save! }
end

Expand Down Expand Up @@ -617,7 +619,10 @@ def save(*args)
children.each { |child| child.name = '' }
assert !@pirate.valid?

children.each { |child| child.mark_for_destruction }
children.each do |child|
child.mark_for_destruction
child.expects(:valid?).never
end
assert_difference("#{association_name.classify}.count", -2) { @pirate.save! }
end

Expand Down

0 comments on commit c52a50e

Please sign in to comment.