Skip to content

Commit

Permalink
Fix mongoize update all array operators master (#5823)
Browse files Browse the repository at this point in the history
* Change Mongoid::Criteria::Queryable::Selector#merge! to correctly merge $in and $nin when provided as a symbol as well as a string

* Mongoid::Criteria::Queryable::Selector#merge! convert to strings before matching

* cleanup whitespace

* Cover all mongo array update operators with tests. Include $pull and $pop in special handling conditions for Mongoize

* cleanup whitespace

* prepare multiple entries in the data to be pulled from the array for $pullAll tests

* fix spacing issue
  • Loading branch information
JohnMaguir committed May 20, 2024
1 parent eec835a commit 47c844c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/atomic_update_preparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def prepare_operation(klass, key, value)

# Get the value for the provided operator, klass, key and value.
#
# This is necessary for special cases like $rename, $addToSet and $push.
# This is necessary for special cases like $rename, $addToSet, $push, $pull and $pop.
#
# @param [ String ] operator The operator.
# @param [ Class ] klass The model class.
Expand All @@ -60,7 +60,7 @@ def prepare_operation(klass, key, value)
def value_for(operator, klass, key, value)
case operator
when '$rename' then value.to_s
when '$addToSet', '$push' then value.mongoize
when '$addToSet', '$push', '$pull', '$pop' then value.mongoize
else mongoize_for(operator, klass, key, value)
end
end
Expand Down
69 changes: 69 additions & 0 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3640,6 +3640,75 @@
expect(new_order.reload.genres).to eq(["electronic"])
end
end

context "when operation is $pull" do
context "when pulling single element" do

before do
depeche_mode.update_attribute(:genres, ["electronic", "pop"])
new_order.update_attribute(:genres, ["electronic", "pop"])
context.update_all("$pull" => { genres: "electronic" })
end

it "updates the first matching document" do
expect(depeche_mode.reload.genres).to eq(["pop"])
end

it "updates the last matching document" do
expect(new_order.reload.genres).to eq(["pop"])
end
end

context "when pulling based on condition" do
before do
depeche_mode.update_attribute(:genres, ["electronic", "pop", "dance"])
new_order.update_attribute(:genres, ["electronic", "pop", "dance"])
context.update_all("$pull" => { genres: { '$in' => ["electronic", "pop"] } })
end

it "updates the first matching document" do
expect(depeche_mode.reload.genres).to eq(["dance"])
end

it "updates the last matching document" do
expect(new_order.reload.genres).to eq(["dance"])
end
end
end

context "when operation is $pop" do

before do
depeche_mode.update_attribute(:genres, ["pop", "electronic"])
end

it "removes first element in array" do
context.update_all("$pop" => { genres: -1 })
expect(depeche_mode.reload.genres).to eq(["electronic"])
end

it "removes last element in array" do
context.update_all("$pop" => { genres: 1 })
expect(depeche_mode.reload.genres).to eq(["pop"])
end
end

context "when operation is $pullAll" do

before do
depeche_mode.update_attribute(:genres, ["pop", "electronic", "dance", "pop" ])
new_order.update_attribute(:genres, ["electronic", "pop", "electronic", "dance"])
context.update_all("$pullAll" => { genres: ["pop", "electronic"] })
end

it "updates the first matching document" do
expect(depeche_mode.reload.genres).to eq(["dance"])
end

it "updates the last matching document" do
expect(new_order.reload.genres).to eq(["dance"])
end
end
end

context 'when using aliased field names' do
Expand Down

0 comments on commit 47c844c

Please sign in to comment.