Skip to content

Commit 2281f0d

Browse files
authored
Mongoize is not called on update_all, when $set operator is used (#5815)
* update tests to highlight a problem with mongoize in $set operation * fix params in mongoize_for method call * align with the mongoize_for method annotation * fix failing test
1 parent dce18b8 commit 2281f0d

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

lib/mongoid/extensions/hash.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __consolidate__(klass)
4949
consolidated[key].update(value)
5050
else
5151
consolidated["$set"] ||= {}
52-
consolidated["$set"].update(key => mongoize_for(key, klass, key, value))
52+
consolidated["$set"].update(key => mongoize_for("$set", klass, key, value))
5353
end
5454
end
5555
consolidated
@@ -199,7 +199,7 @@ def value_for(operator, klass, key, value)
199199
case operator
200200
when "$rename" then value.to_s
201201
when "$addToSet", "$push" then value.mongoize
202-
else mongoize_for(operator, klass, operator, value)
202+
else mongoize_for(operator, klass, key, value)
203203
end
204204
end
205205

spec/mongoid/contextual/mongo_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3765,15 +3765,15 @@
37653765
context "when the attributes must be mongoized" do
37663766

37673767
before do
3768-
context.update_all("$set" => { member_count: "1" })
3768+
context.update_all("$set" => { location: LatLng.new(52.30, 13.25) })
37693769
end
37703770

37713771
it "updates the first matching document" do
3772-
expect(depeche_mode.reload.member_count).to eq(1)
3772+
expect(depeche_mode.reload.location).to eq(LatLng.new(52.30, 13.25))
37733773
end
37743774

37753775
it "updates the last matching document" do
3776-
expect(new_order.reload.member_count).to eq(1)
3776+
expect(new_order.reload.location).to eq(LatLng.new(52.30, 13.25))
37773777
end
37783778
end
37793779
end

spec/support/models/band.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Band
2323
field :mojo, type: Object
2424
field :tags, type: Hash
2525
field :fans
26+
field :location, type: LatLng
2627

2728
embeds_many :records, cascade_callbacks: true
2829
embeds_many :notes, as: :noteable, cascade_callbacks: true, validate: false

spec/support/models/lat_lng.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ class LatLng
44
attr_accessor :lat, :lng
55

66
def self.demongoize(object)
7+
return if object.nil?
8+
79
LatLng.new(object[1], object[0])
810
end
911

@@ -14,4 +16,8 @@ def initialize(lat, lng)
1416
def mongoize
1517
[ lng, lat ]
1618
end
19+
20+
def ==(other)
21+
lat == other.lat && lng == other.lng
22+
end
1723
end

0 commit comments

Comments
 (0)