forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finishing up the commands with delete all and destroy all
- Loading branch information
Showing
9 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
require "mongoid/commands/create" | ||
require "mongoid/commands/delete" | ||
require "mongoid/commands/delete_all" | ||
require "mongoid/commands/destroy" | ||
require "mongoid/commands/destroy_all" | ||
require "mongoid/commands/save" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Mongoid #:nodoc: | ||
module Commands #:nodoc: | ||
class Delete #:nodoc: | ||
# Performs a delete of the supplied +Document+ without any callbacks. | ||
# | ||
# Options: | ||
# | ||
# doc: A new +Document+ that is going to be deleted. | ||
def self.execute(doc) | ||
doc.collection.remove(:_id => doc.id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Mongoid #:nodoc: | ||
module Commands #:nodoc: | ||
class DeleteAll #:nodoc: | ||
# Performs a delete of the all the +Documents+ that match the criteria | ||
# supplied. | ||
# | ||
# Options: | ||
# | ||
# params: A set of conditions to find the +Documents+ by. | ||
# klass: The class of the +Document+ to execute the find on. | ||
# | ||
# Example: | ||
# | ||
# <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt> | ||
def self.execute(klass, params) | ||
klass.find(:all, params).each { |doc| Delete.execute(doc) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Mongoid #:nodoc: | ||
module Commands #:nodoc: | ||
class DestroyAll #:nodoc: | ||
# Performs a destroy of the all the +Documents+ that match the criteria | ||
# supplied. Will execute all the destroy callbacks for each +Document+. | ||
# | ||
# Options: | ||
# | ||
# params: A set of conditions to find the +Documents+ by. | ||
# klass: The class of the +Document+ to execute the find on. | ||
# | ||
# Example: | ||
# | ||
# <tt>DestroyAll.execute(Person, :conditions => { :field => "value" })</tt> | ||
def self.execute(klass, params) | ||
klass.find(:all, params).each { |doc| Destroy.execute(doc) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb") | ||
|
||
describe Mongoid::Commands::DeleteAll do | ||
|
||
describe "#execute" do | ||
|
||
before do | ||
@doc = mock | ||
@docs = [@doc] | ||
@klass = mock | ||
@conditions = { :conditions => { :title => "Sir" } } | ||
end | ||
|
||
it "destroys each document that the criteria finds" do | ||
@klass.expects(:find).with(:all, @conditions).returns(@docs) | ||
Mongoid::Commands::Delete.expects(:execute).with(@doc) | ||
Mongoid::Commands::DeleteAll.execute(@klass, @conditions) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb") | ||
|
||
describe Mongoid::Commands::Delete do | ||
|
||
describe "#execute" do | ||
|
||
before do | ||
@collection = mock | ||
@document = stub(:collection => @collection, :id => "1") | ||
end | ||
|
||
it "removes the document from its collection" do | ||
@collection.expects(:remove).with({ :_id => @document.id }) | ||
Mongoid::Commands::Delete.execute(@document) | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb") | ||
|
||
describe Mongoid::Commands::DestroyAll do | ||
|
||
describe "#execute" do | ||
|
||
before do | ||
@doc = mock | ||
@docs = [@doc] | ||
@klass = mock | ||
@conditions = { :conditions => { :title => "Sir" } } | ||
end | ||
|
||
it "destroys each document that the criteria finds" do | ||
@klass.expects(:find).with(:all, @conditions).returns(@docs) | ||
Mongoid::Commands::Destroy.expects(:execute).with(@doc) | ||
Mongoid::Commands::DestroyAll.execute(@klass, @conditions) | ||
end | ||
|
||
end | ||
|
||
end |