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. [rails#2064 state:resolved]

Signed-off-by: Eloy Duran <eloy.de.enige@gmail.com>
  • Loading branch information
Dmitry Polushkin authored and alloy committed Jul 11, 2009
1 parent 295a20b commit dbc56d6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 5 additions & 7 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -244,17 +244,15 @@ 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 do |attribute, message|
attribute = "#{reflection.name}_#{attribute}"
errors.add(attribute, message) unless errors.on(attribute)
end
association.errors.each do |attribute, message|
attribute = "#{reflection.name}_#{attribute}"
errors.add(attribute, message) unless errors.on(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 dbc56d6

Please sign in to comment.