diff --git a/lib/mongoid/extensions/hash.rb b/lib/mongoid/extensions/hash.rb index 4a187807b8..862ecf1ba4 100644 --- a/lib/mongoid/extensions/hash.rb +++ b/lib/mongoid/extensions/hash.rb @@ -49,7 +49,7 @@ def __consolidate__(klass) consolidated[key].update(value) else consolidated["$set"] ||= {} - consolidated["$set"].update(key => mongoize_for(key, klass, key, value)) + consolidated["$set"].update(key => mongoize_for("$set", klass, key, value)) end end consolidated @@ -199,7 +199,7 @@ 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 diff --git a/spec/mongoid/contextual/mongo_spec.rb b/spec/mongoid/contextual/mongo_spec.rb index c10a8aa939..c8d1be576d 100644 --- a/spec/mongoid/contextual/mongo_spec.rb +++ b/spec/mongoid/contextual/mongo_spec.rb @@ -3765,15 +3765,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 6723ffcbff..dde3fd18ce 100644 --- a/spec/support/models/band.rb +++ b/spec/support/models/band.rb @@ -23,6 +23,7 @@ class Band field :mojo, type: Object field :tags, type: Hash field :fans + field :location, type: LatLng embeds_many :records, cascade_callbacks: true embeds_many :notes, as: :noteable, cascade_callbacks: true, validate: false diff --git a/spec/support/models/lat_lng.rb b/spec/support/models/lat_lng.rb index d65110a92b..c384e19f37 100644 --- a/spec/support/models/lat_lng.rb +++ b/spec/support/models/lat_lng.rb @@ -4,6 +4,8 @@ class LatLng attr_accessor :lat, :lng def self.demongoize(object) + return if object.nil? + LatLng.new(object[1], object[0]) end @@ -14,4 +16,8 @@ def initialize(lat, lng) def mongoize [ lng, lat ] end + + def ==(other) + lat == other.lat && lng == other.lng + end end