Permalink
Browse files

Merge branch 'query_methods_match_correct_docs' of https://github.com…

…/balexand/mongomapper into balexand-query_methods_match_correct_docs
  • Loading branch information...
2 parents 9a86e4f + 8ae7ce0 commit 57112746033d07445db7d8360abdb96fd2776ad6 @jnunemaker jnunemaker committed Dec 28, 2012
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
25 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
@@ -27,8 +43,11 @@ def find!(*ids)
def method_missing(method, *args, &block)
return super unless model.respond_to?(method)
result = model.send(method, *args, &block)
- return super unless result.is_a?(Plucky::Query)
- merge(result)
+ if result.is_a?(Plucky::Query)
+ merge(result)
+ else
+ result
+ end
end
end
end
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
View
2 test/functional/test_scopes.rb
@@ -130,7 +130,7 @@ def self.young
should "not work if method does not return a query" do
@document.class_eval { def self.age; 20 end }
- lambda { @document.by_name('John').age }.should raise_error(NoMethodError)
+ @document.by_name('John').age.should == 20
end
end
end

0 comments on commit 5711274

Please sign in to comment.