Skip to content

Commit eec835a

Browse files
authored
Mongoize is not called on update_all, when $set operator is used (#5814)
* update tests to highlight a problem with mongoize in $set operation * fix params in mongoize_for method call and make $set operator to mongoize passed fields values * update method documentation * align with the mongoize_for method annotation * fix failing test
1 parent 88f4b1a commit eec835a

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

lib/mongoid/atomic_update_preparer.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def prepare(attributes, klass)
2525
if key.to_s.start_with?('$')
2626
(atomic_updates[key] ||= {}).update(prepare_operation(klass, key, value))
2727
else
28-
(atomic_updates['$set'] ||= {})[key] = mongoize_for(key, klass, key, value)
28+
(atomic_updates['$set'] ||= {})[key] = mongoize_for('$set', klass, key, value)
2929
end
3030
end
3131
end
@@ -43,7 +43,7 @@ def prepare(attributes, klass)
4343
def prepare_operation(klass, key, value)
4444
value.each_with_object({}) do |(key2, value2), hash|
4545
key2 = klass.database_field_name(key2)
46-
hash[key2] = value_for(key, klass, value2)
46+
hash[key2] = value_for(key, klass, key2, value2)
4747
end
4848
end
4949

@@ -53,14 +53,15 @@ def prepare_operation(klass, key, value)
5353
#
5454
# @param [ String ] operator The operator.
5555
# @param [ Class ] klass The model class.
56+
# @param [ String | Symbol ] key The field key.
5657
# @param [ Object ] value The original value.
5758
#
5859
# @return [ Object ] Value prepared for the provided operator.
59-
def value_for(operator, klass, value)
60+
def value_for(operator, klass, key, value)
6061
case operator
6162
when '$rename' then value.to_s
6263
when '$addToSet', '$push' then value.mongoize
63-
else mongoize_for(operator, klass, operator, value)
64+
else mongoize_for(operator, klass, key, value)
6465
end
6566
end
6667

spec/mongoid/contextual/mongo_spec.rb

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

36613661
before do
3662-
context.update_all("$set" => { member_count: "1" })
3662+
context.update_all("$set" => { location: LatLng.new(52.30, 13.25) })
36633663
end
36643664

36653665
it "updates the first matching document" do
3666-
expect(depeche_mode.reload.member_count).to eq(1)
3666+
expect(depeche_mode.reload.location).to eq(LatLng.new(52.30, 13.25))
36673667
end
36683668

36693669
it "updates the last matching document" do
3670-
expect(new_order.reload.member_count).to eq(1)
3670+
expect(new_order.reload.location).to eq(LatLng.new(52.30, 13.25))
36713671
end
36723672
end
36733673
end

spec/support/models/band.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Band
2626
field :mojo, type: Object
2727
field :tags, type: Hash
2828
field :fans
29+
field :location, type: LatLng
2930

3031
alias_attribute :d, :deleted
3132

spec/support/models/lat_lng.rb

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

77
def self.demongoize(object)
8+
return if object.nil?
9+
810
LatLng.new(object[1], object[0])
911
end
1012

@@ -15,4 +17,8 @@ def initialize(lat, lng)
1517
def mongoize
1618
[ lng, lat ]
1719
end
20+
21+
def ==(other)
22+
lat == other.lat && lng == other.lng
23+
end
1824
end

0 commit comments

Comments
 (0)