Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

move delete and destroy methods to Querying::Decorator

  • Loading branch information...
commit 8ae7ce0a57ae67c97d04407a2959603f476951d8 1 parent 3a78f04
@balexand balexand authored
View
16 lib/mongo_mapper/plugins/querying.rb
@@ -40,22 +40,6 @@ def update(*args)
end
end
- def delete(*ids)
- query(:_id => ids.flatten).remove
- end
-
- def delete_all(options={})
- query(options).remove
- end
-
- def destroy(*ids)
- find_some!(ids.flatten).each { |doc| doc.destroy }
- end
-
- def destroy_all(options={})
- find_each(options) { |document| document.destroy }
- end
-
# @api private for now
def query(options={})
query = Plucky::Query.new(collection, :transformer => transformer)
View
18 lib/mongo_mapper/plugins/querying/decorator.rb
@@ -2,11 +2,27 @@
module MongoMapper
module Plugins
module Querying
- Methods = Plucky::Methods + [:find!]
+ Methods = Plucky::Methods + [:delete, :delete_all, :destroy, :destroy_all, :find!]
module Decorator
include DynamicQuerying::ClassMethods
+ def delete(*ids)
+ where(:_id => ids.flatten).remove
+ end
+
+ def delete_all(options = {})
+ where(options).remove
+ end
+
+ def destroy(*ids)
+ [find!(*ids.flatten.compact.uniq)].flatten.each { |doc| doc.destroy }
+ end
+
+ def destroy_all(options={})
+ find_each(options) { |document| document.destroy }
+ end
+
def model(model=nil)
return @model if model.nil?
@model = model
View
35 test/functional/test_querying.rb
@@ -560,6 +560,41 @@ def setup
should "be chainable" do
@query.sort(:age).first.should == @doc3
end
+
+ context "with methods from MongoMapper::Plugins::Querying" do
+ should "delete" do
+ lambda do
+ @document.where(:first_name => 'Steve').delete(@doc1.id, @doc2.id)
+ end.should change { @document.count }.by(-1)
+ @document.all.should == [@doc1, @doc3]
+ end
+
+ should "delete_all" do
+ lambda do
+ @document.where(:first_name => 'Steph').delete_all(:last_name => "Nunemaker")
+ end.should change { @document.count }.by(-1)
+ @document.all.should == [@doc1, @doc2]
+ end
+
+ should "destroy" do
+ lambda do
+ @document.where(:first_name => 'Steve').destroy(@doc1.id, @doc2.id)
+ end.should raise_error(MongoMapper::DocumentNotFound)
+ @document.count.should == 3
+
+ lambda do
+ @document.where(:last_name => 'Nunemaker').destroy(@doc1.id, @doc3.id)
+ end.should change { @document.count }.by(-2)
+ @document.all.should == [@doc2]
+ end
+
+ should "destroy_all" do
+ lambda do
+ @document.where(:first_name => 'Steph').destroy_all(:last_name => "Nunemaker")
+ end.should change { @document.count }.by(-1)
+ @document.all.should == [@doc1, @doc2]
+ end
+ end
end
context ".fields" do
Please sign in to comment.
Something went wrong with that request. Please try again.