Skip to content

Commit 47c844c

Browse files
authored
Fix mongoize update all array operators master (#5823)
* 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
1 parent eec835a commit 47c844c

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lib/mongoid/atomic_update_preparer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def prepare_operation(klass, key, value)
4949

5050
# Get the value for the provided operator, klass, key and value.
5151
#
52-
# This is necessary for special cases like $rename, $addToSet and $push.
52+
# This is necessary for special cases like $rename, $addToSet, $push, $pull and $pop.
5353
#
5454
# @param [ String ] operator The operator.
5555
# @param [ Class ] klass The model class.
@@ -60,7 +60,7 @@ def prepare_operation(klass, key, value)
6060
def value_for(operator, klass, key, value)
6161
case operator
6262
when '$rename' then value.to_s
63-
when '$addToSet', '$push' then value.mongoize
63+
when '$addToSet', '$push', '$pull', '$pop' then value.mongoize
6464
else mongoize_for(operator, klass, key, value)
6565
end
6666
end

spec/mongoid/contextual/mongo_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,6 +3640,75 @@
36403640
expect(new_order.reload.genres).to eq(["electronic"])
36413641
end
36423642
end
3643+
3644+
context "when operation is $pull" do
3645+
context "when pulling single element" do
3646+
3647+
before do
3648+
depeche_mode.update_attribute(:genres, ["electronic", "pop"])
3649+
new_order.update_attribute(:genres, ["electronic", "pop"])
3650+
context.update_all("$pull" => { genres: "electronic" })
3651+
end
3652+
3653+
it "updates the first matching document" do
3654+
expect(depeche_mode.reload.genres).to eq(["pop"])
3655+
end
3656+
3657+
it "updates the last matching document" do
3658+
expect(new_order.reload.genres).to eq(["pop"])
3659+
end
3660+
end
3661+
3662+
context "when pulling based on condition" do
3663+
before do
3664+
depeche_mode.update_attribute(:genres, ["electronic", "pop", "dance"])
3665+
new_order.update_attribute(:genres, ["electronic", "pop", "dance"])
3666+
context.update_all("$pull" => { genres: { '$in' => ["electronic", "pop"] } })
3667+
end
3668+
3669+
it "updates the first matching document" do
3670+
expect(depeche_mode.reload.genres).to eq(["dance"])
3671+
end
3672+
3673+
it "updates the last matching document" do
3674+
expect(new_order.reload.genres).to eq(["dance"])
3675+
end
3676+
end
3677+
end
3678+
3679+
context "when operation is $pop" do
3680+
3681+
before do
3682+
depeche_mode.update_attribute(:genres, ["pop", "electronic"])
3683+
end
3684+
3685+
it "removes first element in array" do
3686+
context.update_all("$pop" => { genres: -1 })
3687+
expect(depeche_mode.reload.genres).to eq(["electronic"])
3688+
end
3689+
3690+
it "removes last element in array" do
3691+
context.update_all("$pop" => { genres: 1 })
3692+
expect(depeche_mode.reload.genres).to eq(["pop"])
3693+
end
3694+
end
3695+
3696+
context "when operation is $pullAll" do
3697+
3698+
before do
3699+
depeche_mode.update_attribute(:genres, ["pop", "electronic", "dance", "pop" ])
3700+
new_order.update_attribute(:genres, ["electronic", "pop", "electronic", "dance"])
3701+
context.update_all("$pullAll" => { genres: ["pop", "electronic"] })
3702+
end
3703+
3704+
it "updates the first matching document" do
3705+
expect(depeche_mode.reload.genres).to eq(["dance"])
3706+
end
3707+
3708+
it "updates the last matching document" do
3709+
expect(new_order.reload.genres).to eq(["dance"])
3710+
end
3711+
end
36433712
end
36443713

36453714
context 'when using aliased field names' do

0 commit comments

Comments
 (0)