From 4e75074069e1b10652f4a4f2f18aee55ee40d30b Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Mon, 10 Aug 2009 00:18:22 -0400 Subject: [PATCH] destroy_all and delete_all scoped to association now take conditions. --- .../associations/many_documents_proxy.rb | 9 +++++---- .../functional/associations/test_many_proxy.rb | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/mongomapper/associations/many_documents_proxy.rb b/lib/mongomapper/associations/many_documents_proxy.rb index df2b8bfb1..81d07c02e 100644 --- a/lib/mongomapper/associations/many_documents_proxy.rb +++ b/lib/mongomapper/associations/many_documents_proxy.rb @@ -54,12 +54,13 @@ def create(attrs={}) doc end - def destroy_all - @target.map(&:destroy) if load_target + def destroy_all(conditions={}) + all(:conditions => conditions).map(&:destroy) + reset end - def delete_all - klass.delete_all(scoped_conditions) + def delete_all(conditions={}) + klass.delete_all(conditions.deep_merge(scoped_conditions)) reset end diff --git a/test/functional/associations/test_many_proxy.rb b/test/functional/associations/test_many_proxy.rb index fa304bed4..e84d8c3fc 100644 --- a/test/functional/associations/test_many_proxy.rb +++ b/test/functional/associations/test_many_proxy.rb @@ -118,6 +118,15 @@ def setup Status.count.should == 2 end + should "work with destroy all and conditions" do + @project.statuses.count.should == 2 + @project.statuses.destroy_all(:name => '1') + @project.statuses.count.should == 1 + + @project2.statuses.count.should == 2 + Status.count.should == 3 + end + should "work with delete all" do @project.statuses.count.should == 2 @project.statuses.delete_all @@ -127,6 +136,15 @@ def setup Status.count.should == 2 end + should "work with delete all and conditions" do + @project.statuses.count.should == 2 + @project.statuses.delete_all(:name => '1') + @project.statuses.count.should == 1 + + @project2.statuses.count.should == 2 + Status.count.should == 3 + end + should "work with nullify" do @project.statuses.count.should == 2 @project.statuses.nullify