Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoize is not called on update_all, when $set operator is used #5814

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading