Skip to content

Commit feaa47d

Browse files
JohnMaguirjamis
andauthored
Fix mongoize update all array operators 8.1 (#5824)
* 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]>
1 parent 260d3f2 commit feaa47d

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lib/mongoid/extensions/hash.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def to_criteria
187187

188188
# Get the value for the provided operator, klass, key and value.
189189
#
190-
# This is necessary for special cases like $rename, $addToSet and $push.
190+
# This is necessary for special cases like $rename, $addToSet, $push, $pull and $pop.
191191
#
192192
# @param [ String ] operator The operator.
193193
# @param [ Class ] klass The model class.
@@ -198,7 +198,7 @@ def to_criteria
198198
def value_for(operator, klass, key, value)
199199
case operator
200200
when "$rename" then value.to_s
201-
when "$addToSet", "$push" then value.mongoize
201+
when "$addToSet", "$push", '$pull', '$pop' then value.mongoize
202202
else mongoize_for(operator, klass, key, value)
203203
end
204204
end

spec/mongoid/contextual/mongo_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3746,6 +3746,75 @@
37463746
expect(new_order.reload.genres).to eq(["electronic"])
37473747
end
37483748
end
3749+
3750+
context "when operation is $pull" do
3751+
context "when pulling single element" do
3752+
3753+
before do
3754+
depeche_mode.update_attribute(:genres, ["electronic", "pop"])
3755+
new_order.update_attribute(:genres, ["electronic", "pop"])
3756+
context.update_all("$pull" => { genres: "electronic" })
3757+
end
3758+
3759+
it "updates the first matching document" do
3760+
expect(depeche_mode.reload.genres).to eq(["pop"])
3761+
end
3762+
3763+
it "updates the last matching document" do
3764+
expect(new_order.reload.genres).to eq(["pop"])
3765+
end
3766+
end
3767+
3768+
context "when pulling based on condition" do
3769+
before do
3770+
depeche_mode.update_attribute(:genres, ["electronic", "pop", "dance"])
3771+
new_order.update_attribute(:genres, ["electronic", "pop", "dance"])
3772+
context.update_all("$pull" => { genres: { '$in' => ["electronic", "pop"] } })
3773+
end
3774+
3775+
it "updates the first matching document" do
3776+
expect(depeche_mode.reload.genres).to eq(["dance"])
3777+
end
3778+
3779+
it "updates the last matching document" do
3780+
expect(new_order.reload.genres).to eq(["dance"])
3781+
end
3782+
end
3783+
end
3784+
3785+
context "when operation is $pop" do
3786+
3787+
before do
3788+
depeche_mode.update_attribute(:genres, ["pop", "electronic"])
3789+
end
3790+
3791+
it "removes first element in array" do
3792+
context.update_all("$pop" => { genres: -1 })
3793+
expect(depeche_mode.reload.genres).to eq(["electronic"])
3794+
end
3795+
3796+
it "removes last element in array" do
3797+
context.update_all("$pop" => { genres: 1 })
3798+
expect(depeche_mode.reload.genres).to eq(["pop"])
3799+
end
3800+
end
3801+
3802+
context "when operation is $pullAll" do
3803+
3804+
before do
3805+
depeche_mode.update_attribute(:genres, ["pop", "electronic", "dance", "pop" ])
3806+
new_order.update_attribute(:genres, ["electronic", "pop", "electronic", "dance"])
3807+
context.update_all("$pullAll" => { genres: ["pop", "electronic"] })
3808+
end
3809+
3810+
it "updates the first matching document" do
3811+
expect(depeche_mode.reload.genres).to eq(["dance"])
3812+
end
3813+
3814+
it "updates the last matching document" do
3815+
expect(new_order.reload.genres).to eq(["dance"])
3816+
end
3817+
end
37493818
end
37503819

37513820
context 'when using aliased field names' do

0 commit comments

Comments
 (0)