Skip to content

Commit

Permalink
Mongoize is not called on update_all, when $set operator is used (#5814)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dem committed May 10, 2024
1 parent 88f4b1a commit eec835a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
9 changes: 5 additions & 4 deletions lib/mongoid/atomic_update_preparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def prepare(attributes, klass)
if key.to_s.start_with?('$')
(atomic_updates[key] ||= {}).update(prepare_operation(klass, key, value))
else
(atomic_updates['$set'] ||= {})[key] = mongoize_for(key, klass, key, value)
(atomic_updates['$set'] ||= {})[key] = mongoize_for('$set', klass, key, value)
end
end
end
Expand All @@ -43,7 +43,7 @@ def prepare(attributes, klass)
def prepare_operation(klass, key, value)
value.each_with_object({}) do |(key2, value2), hash|
key2 = klass.database_field_name(key2)
hash[key2] = value_for(key, klass, value2)
hash[key2] = value_for(key, klass, key2, value2)
end
end

Expand All @@ -53,14 +53,15 @@ def prepare_operation(klass, key, value)
#
# @param [ String ] operator The operator.
# @param [ Class ] klass The model class.
# @param [ String | Symbol ] key The field key.
# @param [ Object ] value The original value.
#
# @return [ Object ] Value prepared for the provided operator.
def value_for(operator, klass, value)
def value_for(operator, klass, key, value)
case operator
when '$rename' then value.to_s
when '$addToSet', '$push' then value.mongoize
else mongoize_for(operator, klass, operator, value)
else mongoize_for(operator, klass, key, value)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3659,15 +3659,15 @@
context "when the attributes must be mongoized" do

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

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

it "updates the last matching document" do
expect(new_order.reload.member_count).to eq(1)
expect(new_order.reload.location).to eq(LatLng.new(52.30, 13.25))
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/models/band.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Band
field :mojo, type: Object
field :tags, type: Hash
field :fans
field :location, type: LatLng

alias_attribute :d, :deleted

Expand Down
6 changes: 6 additions & 0 deletions spec/support/models/lat_lng.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class LatLng
attr_accessor :lat, :lng

def self.demongoize(object)
return if object.nil?

LatLng.new(object[1], object[0])
end

Expand All @@ -15,4 +17,8 @@ def initialize(lat, lng)
def mongoize
[ lng, lat ]
end

def ==(other)
lat == other.lat && lng == other.lng
end
end

0 comments on commit eec835a

Please sign in to comment.