From 610dd16ebffee7000c839596b86dbb115064ebc7 Mon Sep 17 00:00:00 2001 From: Michael Deryugin Date: Fri, 10 May 2024 12:02:16 +0100 Subject: [PATCH 1/5] update tests to highlight a problem with mongoize in $set operation --- spec/mongoid/contextual/mongo_spec.rb | 6 +++--- spec/support/models/band.rb | 1 + spec/support/models/lat_lng.rb | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/mongoid/contextual/mongo_spec.rb b/spec/mongoid/contextual/mongo_spec.rb index 3ee2382524..861a53e2d0 100644 --- a/spec/mongoid/contextual/mongo_spec.rb +++ b/spec/mongoid/contextual/mongo_spec.rb @@ -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 diff --git a/spec/support/models/band.rb b/spec/support/models/band.rb index d5745259bc..29ffe1cc31 100644 --- a/spec/support/models/band.rb +++ b/spec/support/models/band.rb @@ -26,6 +26,7 @@ class Band field :mojo, type: Object field :tags, type: Hash field :fans + field :location, type: LatLng alias_attribute :d, :deleted diff --git a/spec/support/models/lat_lng.rb b/spec/support/models/lat_lng.rb index f2eac19190..aa5a91b55c 100644 --- a/spec/support/models/lat_lng.rb +++ b/spec/support/models/lat_lng.rb @@ -15,4 +15,8 @@ def initialize(lat, lng) def mongoize [ lng, lat ] end + + def ==(other) + lat == other.lat && lng == other.lng + end end From f153f7a962a9c5ee1f3f9c03bf7ad9874e4e87de Mon Sep 17 00:00:00 2001 From: Michael Deryugin Date: Fri, 10 May 2024 12:21:43 +0100 Subject: [PATCH 2/5] fix params in mongoize_for method call and make $set operator to mongoize passed fields values --- lib/mongoid/atomic_update_preparer.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mongoid/atomic_update_preparer.rb b/lib/mongoid/atomic_update_preparer.rb index c4655eefd1..5d931f0042 100644 --- a/lib/mongoid/atomic_update_preparer.rb +++ b/lib/mongoid/atomic_update_preparer.rb @@ -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 @@ -56,11 +56,11 @@ def prepare_operation(klass, key, value) # @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 From 8adfb994219855f47158a027688c36061a8bb30a Mon Sep 17 00:00:00 2001 From: Michael Deryugin Date: Fri, 10 May 2024 12:35:09 +0100 Subject: [PATCH 3/5] update method documentation --- lib/mongoid/atomic_update_preparer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mongoid/atomic_update_preparer.rb b/lib/mongoid/atomic_update_preparer.rb index 5d931f0042..948a091426 100644 --- a/lib/mongoid/atomic_update_preparer.rb +++ b/lib/mongoid/atomic_update_preparer.rb @@ -53,6 +53,7 @@ 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. From 46ab85636be4379606eead2ccb74c325fd09e980 Mon Sep 17 00:00:00 2001 From: Michael Deryugin Date: Fri, 10 May 2024 12:43:02 +0100 Subject: [PATCH 4/5] align with the mongoize_for method annotation --- lib/mongoid/atomic_update_preparer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongoid/atomic_update_preparer.rb b/lib/mongoid/atomic_update_preparer.rb index 948a091426..fcea5efbb2 100644 --- a/lib/mongoid/atomic_update_preparer.rb +++ b/lib/mongoid/atomic_update_preparer.rb @@ -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 From 5288a2ad32d3b1bb1f70be01a16c86cbf18c39d5 Mon Sep 17 00:00:00 2001 From: Michael Deryugin Date: Fri, 10 May 2024 15:25:31 +0100 Subject: [PATCH 5/5] fix failing test --- spec/support/models/lat_lng.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/support/models/lat_lng.rb b/spec/support/models/lat_lng.rb index aa5a91b55c..b7a64443d2 100644 --- a/spec/support/models/lat_lng.rb +++ b/spec/support/models/lat_lng.rb @@ -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