Skip to content

Commit

Permalink
destroy_all and delete_all scoped to association now take conditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Aug 10, 2009
1 parent 9d981d1 commit 4e75074
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/mongomapper/associations/many_documents_proxy.rb
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions test/functional/associations/test_many_proxy.rb
Expand Up @@ -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
Expand 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
Expand Down

0 comments on commit 4e75074

Please sign in to comment.