From c7dfffdb4407b74e099193bfc0e01bd5eab914a1 Mon Sep 17 00:00:00 2001 From: Dmitry Polushkin Date: Tue, 2 Jun 2009 20:14:33 +0100 Subject: [PATCH] Autosave should not validate association when it marked_for_destruction --- .../lib/active_record/autosave_association.rb | 12 +++++------- .../test/cases/autosave_association_test.rb | 7 ++++++- .../test/connections/native_mysql/connection.rb | 5 +++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index a540570..c4fbdfc 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -245,19 +245,17 @@ module ActiveRecord # the parent, self, if it wasn't. Skips any :autosave # enabled records if they're marked_for_destruction?. def association_valid?(reflection, association) - unless valid = association.valid? + unless association.marked_for_destruction? || valid = association.valid? if reflection.options[:autosave] - unless association.marked_for_destruction? - association.errors.each do |attribute, message| - attribute = "#{reflection.name}_#{attribute}" - errors[attribute] << message if errors[attribute].empty? - end + association.errors.each do |attribute, message| + attribute = "#{reflection.name}_#{attribute}" + errors[attribute] << message if errors[attribute].empty? end else errors.add(reflection.name) end end - valid + valid || true end # Is used as a before_save callback to check while saving a collection diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index ddca5e9..9f7558d 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -478,6 +478,7 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase assert !@pirate.valid? @pirate.ship.mark_for_destruction + @pirate.ship.expects(:valid?).never assert_difference('Ship.count', -1) { @pirate.save! } end @@ -515,6 +516,7 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase assert !@ship.valid? @ship.pirate.mark_for_destruction + @ship.pirate.expects(:valid?).never assert_difference('Pirate.count', -1) { @ship.save! } end @@ -558,7 +560,10 @@ class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase 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 diff --git a/activerecord/test/connections/native_mysql/connection.rb b/activerecord/test/connections/native_mysql/connection.rb index 140e06d..180ca00 100644 --- a/activerecord/test/connections/native_mysql/connection.rb +++ b/activerecord/test/connections/native_mysql/connection.rb @@ -10,13 +10,14 @@ ActiveRecord::Base.logger = Logger.new("debug.log") ActiveRecord::Base.configurations = { 'arunit' => { :adapter => 'mysql', - :username => 'rails', + :username => 'root', + :password => '', :encoding => 'utf8', :database => 'activerecord_unittest', }, 'arunit2' => { :adapter => 'mysql', - :username => 'rails', + :username => 'root', :database => 'activerecord_unittest2' } } -- 1.6.0.4