Skip to content

Commit

Permalink
Implement find and modify related operations
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed May 26, 2015
1 parent 11b5e19 commit fdfbdb6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 34 deletions.
52 changes: 45 additions & 7 deletions lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,59 @@ def explain
# $findAndModify.
#
# @example Execute the command.
# context.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
# context.find_one_and_update({ "$inc" => { likes: 1 }})
#
# @param [ Hash ] update The updates.
# @param [ Hash ] options The command options.
#
# @option options [ true, false ] :new Return the updated document.
# @option options [ true, false ] :remove Delete the first document.
# @option options [ :before, :after ] :return_document Return the updated document
# from before or after update.
# @option options [ true, false ] :upsert Create the document if it doesn't exist.
#
# @return [ Document ] The result of the command.
#
# @since 3.0.0
def find_and_modify(update, options = {})
if doc = FindAndModify.new(collection, criteria, update, options).result
Factory.from_db(klass, doc) if doc.any?
# @since 5.0.0
def find_one_and_update(update, options = {})
if doc = query.find_one_and_update(update, options)
Factory.from_db(klass, doc)
end
end

# Execute the find and modify command, used for MongoDB's
# $findAndModify.
#
# @example Execute the command.
# context.find_one_and_update({ likes: 1 })
#
# @param [ Hash ] update The updates.
# @param [ Hash ] options The command options.
#
# @option options [ :before, :after ] :return_document Return the updated document
# from before or after update.
# @option options [ true, false ] :upsert Create the document if it doesn't exist.
#
# @return [ Document ] The result of the command.
#
# @since 5.0.0
def find_one_and_replace(replacement, options = {})
if doc = query.find_one_and_replace(replacement, options)
Factory.from_db(klass, doc)
end
end

# Execute the find and modify command, used for MongoDB's
# $findAndModify. This deletes the found document.
#
# @example Execute the command.
# context.find_one_and_delete
#
# @return [ Document ] The result of the command.
#
# @since 5.0.0
def find_one_and_delete
if doc = query.find_one_and_delete
Factory.from_db(klass, doc)
>>>>>>> Implement find and modify related operations
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/mongoid/findable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module Findable
:each,
:each_with_index,
:extras,
:find_and_modify,
:find_one_and_delete,
:find_one_and_replace,
:find_one_and_update,
:find_or_create_by,
:find_or_create_by!,
:find_or_initialize_by,
Expand Down
14 changes: 7 additions & 7 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@
end
end

pending "#find_and_modify" do
describe "#find_one_and_update" do

let!(:depeche) do
Band.create(name: "Depeche Mode")
Expand All @@ -580,7 +580,7 @@
end

let!(:result) do
context.find_and_modify("$inc" => { likes: 1 })
context.find_one_and_update("$inc" => { likes: 1 })
end

it "returns the first matching document" do
Expand All @@ -603,7 +603,7 @@
end

let!(:result) do
context.find_and_modify("$inc" => { likes: 1 })
context.find_one_and_update("$inc" => { likes: 1 })
end

it "returns the first matching document" do
Expand All @@ -626,7 +626,7 @@
end

let!(:result) do
context.find_and_modify("$inc" => { likes: 1 })
context.find_one_and_update("$inc" => { likes: 1 })
end

it "returns the first matching document" do
Expand All @@ -653,7 +653,7 @@
end

let!(:result) do
context.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
context.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
end

it "returns the first matching document" do
Expand All @@ -676,7 +676,7 @@
end

let!(:result) do
context.find_and_modify({}, remove: true)
context.find_one_and_delete
end

it "returns the first matching document" do
Expand All @@ -702,7 +702,7 @@
end

let(:result) do
context.find_and_modify("$inc" => { likes: 1 })
context.find_one_and_update("$inc" => { likes: 1 })
end

it "returns nil" do
Expand Down
27 changes: 10 additions & 17 deletions spec/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@
end
end

pending "#find_and_modify" do
describe "#find_one_and_update" do

let!(:depeche) do
Band.create(name: "Depeche Mode")
Expand All @@ -778,7 +778,7 @@
end

let(:result) do
criteria.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
criteria.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
end

it "returns the first matching document" do
Expand All @@ -793,7 +793,7 @@
end

let!(:result) do
criteria.find_and_modify("$inc" => { likes: 1 })
criteria.find_one_and_update("$inc" => { likes: 1 })
end

before do
Expand All @@ -813,7 +813,7 @@
end

let!(:result) do
criteria.find_and_modify("$inc" => { likes: 1 })
criteria.find_one_and_update("$inc" => { likes: 1 })
end

it "returns the first matching document" do
Expand All @@ -832,7 +832,7 @@
end

let!(:result) do
criteria.find_and_modify("$inc" => { likes: 1 })
criteria.find_one_and_update("$inc" => { likes: 1 })
end

it "returns the first matching document" do
Expand All @@ -851,7 +851,7 @@
end

let!(:result) do
criteria.find_and_modify("$inc" => { likes: 1 })
criteria.find_one_and_update("$inc" => { likes: 1 })
end

it "returns the first matching document" do
Expand All @@ -874,7 +874,7 @@
end

let!(:result) do
criteria.find_and_modify({ "$inc" => { likes: 1 }}, new: true)
criteria.find_one_and_update({ "$inc" => { likes: 1 }}, return_document: :after)
end

it "returns the first matching document" do
Expand All @@ -893,7 +893,7 @@
end

let!(:result) do
criteria.find_and_modify({}, remove: true)
criteria.find_one_and_delete
end

it "returns the first matching document" do
Expand All @@ -914,15 +914,8 @@
Band.where(name: "Placebo")
end

context "with upsert" do

let(:result) do
criteria.find_and_modify({"$inc" => { likes: 1 }}, upsert: true)
end

it 'returns nil' do
expect(result).to be_nil
end
let(:result) do
criteria.find_one_and_update("$inc" => { likes: 1 })
end

context "without upsert" do
Expand Down
4 changes: 2 additions & 2 deletions spec/mongoid/findable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
end
end

pending ".find_and_modify" do
describe ".find_one_and_update" do

let!(:person) do
Person.create(title: "Senior")
end

it "returns the document" do
expect(Person.find_and_modify(title: "Junior")).to eq(person)
expect(Person.find_one_and_update(title: "Junior")).to eq(person)
end
end

Expand Down

0 comments on commit fdfbdb6

Please sign in to comment.