Skip to content

Commit

Permalink
Fix mongoize update all array operators 8.1 (#5824)
Browse files Browse the repository at this point in the history
* Cover all mongo array update operators with tests. Include  and  in special handling conditions for Mongoize

* fix spacing issues

---------

Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
JohnMaguir and jamis authored Aug 1, 2024
1 parent 260d3f2 commit feaa47d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/extensions/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def to_criteria

# 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 @@ -198,7 +198,7 @@ def to_criteria
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 @@ -3746,6 +3746,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 feaa47d

Please sign in to comment.