diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8f1bd42e1..3227b8588 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -25,7 +25,7 @@ Metrics/BlockNesting: # Offense count: 17 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 327 + Max: 351 # Offense count: 50 # Configuration parameters: AllowedMethods, AllowedPatterns. diff --git a/lib/mongoid/contextual/mongo.rb b/lib/mongoid/contextual/mongo.rb index 7333f059a..0ba997e2b 100644 --- a/lib/mongoid/contextual/mongo.rb +++ b/lib/mongoid/contextual/mongo.rb @@ -803,8 +803,7 @@ def documents_loader def update_documents(attributes, method = :update_one, opts = {}) return false unless attributes - attributes = attributes.transform_keys { |k| klass.database_field_name(k.to_s) } - view.send(method, attributes.__consolidate__(klass), opts) + view.send(method, prepare_atomic_updates(klass, attributes), opts) end # Apply the field limitations. @@ -992,6 +991,49 @@ def retrieve_nth_to_last_with_limit(n, limit) process_raw_docs(raw_docs, limit) end # rubocop:enable Naming/MethodParameterName + + # Convert the key/values in the attributes into a hash of atomic updates. + # Non-operator keys are assumed to use $set operation. + # + # @param [ Class ] klass The model class. + # @param [ Hash ] The attributes to convert. + # + # @return [ Hash ] The prepared atomic updates. + def prepare_atomic_updates(klass, attributes) + attributes.each_pair.with_object({}) do |(key, value), atomic_updates| + key = klass.database_field_name(key) + if key.to_s.start_with?('$') + value = value.each_with_object({}) do |(key2, value2), hash| + key2 = klass.database_field_name(key2) + hash[key2] = key == '$rename' ? value2.to_s : mongoize_for_atomic_update(klass, key, key2, value2) + end + atomic_updates[key] ||= {} + atomic_updates[key].update(value) + else + atomic_updates['$set'] ||= {} + atomic_updates['$set'][key] = mongoize_for_atomic_update(klass, key, key, value) + end + end + end + + # Mongoize a value for an atomic update for the given klass, operator, and field. + # + # @param [ Class ] klass The model class. + # @param [ String ] operator The operator. + # @param [ String | Symbol ] field_name The field key. + # @param [ Object ] value The value to mongoize. + # + # @return [ Object ] The mongoized value. + def mongoize_for_atomic_update(klass, operator, field_name, value) + field = klass.fields[field_name.to_s] + return value unless field + + mongoized = field.mongoize(value) + if Mongoid::Persistable::LIST_OPERATIONS.include?(operator) && field.resizable? && !value.is_a?(Array) + mongoized = mongoized.first + end + mongoized + end end end end diff --git a/lib/mongoid/extensions/hash.rb b/lib/mongoid/extensions/hash.rb index ea0862381..9f866bac7 100644 --- a/lib/mongoid/extensions/hash.rb +++ b/lib/mongoid/extensions/hash.rb @@ -30,28 +30,6 @@ def __mongoize_object_id__ end end - # Consolidate the key/values in the hash under an atomic $set. - # - # @example Consolidate the hash. - # { name: "Placebo" }.__consolidate__ - # - # @return [ Hash ] A new consolidated hash. - def __consolidate__(klass) - each_pair.with_object({}) do |(key, value), consolidated| - if key.start_with?('$') - value = value.each_with_object({}) do |(key2, value2), hash| - key2 = klass.database_field_name(key2) - hash[key2] = key == '$rename' ? value2.to_s : mongoize_for(key, klass, key2, value2) - end - consolidated[key] ||= {} - consolidated[key].update(value) - else - consolidated['$set'] ||= {} - consolidated['$set'].update(key => mongoize_for(key, klass, key, value)) - end - end - end - # Checks whether conditions given in this hash are known to be # unsatisfiable, i.e., querying with this hash will always return no # documents. @@ -182,34 +160,6 @@ def to_criteria criteria end - private - - # Mongoize for the klass, key and value. - # - # @api private - # - # @example Mongoize for the klass, field and value. - # {}.mongoize_for("$push", Band, "name", "test") - # - # @param [ String ] operator The operator. - # @param [ Class ] klass The model class. - # @param [ String | Symbol ] key The field key. - # @param [ Object ] value The value to mongoize. - # - # @return [ Object ] The mongoized value. - def mongoize_for(operator, klass, key, value) - field = klass.fields[key.to_s] - if field - val = field.mongoize(value) - if Mongoid::Persistable::LIST_OPERATIONS.include?(operator) && field.resizable? && !value.is_a?(Array) - val = val.first - end - val - else - value - end - end - module ClassMethods # Turn the object from the ruby type we deal with to a Mongo friendly diff --git a/spec/mongoid/contextual/mongo_spec.rb b/spec/mongoid/contextual/mongo_spec.rb index fb9e7a949..2ff6a74c1 100644 --- a/spec/mongoid/contextual/mongo_spec.rb +++ b/spec/mongoid/contextual/mongo_spec.rb @@ -4640,4 +4640,52 @@ end end end + + describe '#prepare_atomic_updates' do + subject(:updates) { instance.send(:prepare_atomic_updates, Band, hash) } + + let(:instance) { described_class.new(Band.where(name: 'Depeche Mode')) } + + context 'when the hash already contains the key' do + + context 'when the $set is first' do + let(:hash) do + { '$set' => { name: 'Tool' }, likes: 10, '$inc' => { plays: 1 } } + end + + it 'moves the non hash values under the provided key' do + expect(updates).to eq({ + '$set' => { 'name' => 'Tool', 'likes' => 10 }, + '$inc' => { 'plays' => 1 } + }) + end + end + + context 'when the $set is not first' do + let(:hash) do + { likes: 10, '$inc' => { plays: 1 }, '$set' => { name: 'Tool' } } + end + + it 'moves the non hash values under the provided key' do + expect(updates).to eq({ + '$set' => { 'likes' => 10, 'name' => 'Tool' }, + '$inc' => { 'plays' => 1 } + }) + end + end + end + + context 'when the hash does not contain the key' do + let(:hash) do + { likes: 10, '$inc' => { plays: 1 }, name: 'Tool' } + end + + it 'moves the non hash values under the provided key' do + expect(updates).to eq({ + '$set' => { 'likes' => 10, 'name' => 'Tool' }, + '$inc' => { 'plays' => 1 } + }) + end + end + end end diff --git a/spec/mongoid/extensions/hash_spec.rb b/spec/mongoid/extensions/hash_spec.rb index 0f2d91ee4..311061c1e 100644 --- a/spec/mongoid/extensions/hash_spec.rb +++ b/spec/mongoid/extensions/hash_spec.rb @@ -162,63 +162,6 @@ end end - describe '#__consolidate__' do - - context 'when the hash already contains the key' do - - context 'when the $set is first' do - - let(:hash) do - { '$set' => { name: 'Tool' }, likes: 10, '$inc' => { plays: 1 } } - end - - let(:consolidated) do - hash.__consolidate__(Band) - end - - it 'moves the non hash values under the provided key' do - expect(consolidated).to eq({ - '$set' => { 'name' => 'Tool', likes: 10 }, '$inc' => { 'plays' => 1 } - }) - end - end - - context 'when the $set is not first' do - - let(:hash) do - { likes: 10, '$inc' => { plays: 1 }, '$set' => { name: 'Tool' } } - end - - let(:consolidated) do - hash.__consolidate__(Band) - end - - it 'moves the non hash values under the provided key' do - expect(consolidated).to eq({ - '$set' => { likes: 10, 'name' => 'Tool' }, '$inc' => { 'plays' => 1 } - }) - end - end - end - - context 'when the hash does not contain the key' do - - let(:hash) do - { likes: 10, '$inc' => { plays: 1 }, name: 'Tool' } - end - - let(:consolidated) do - hash.__consolidate__(Band) - end - - it 'moves the non hash values under the provided key' do - expect(consolidated).to eq({ - '$set' => { likes: 10, name: 'Tool' }, '$inc' => { 'plays' => 1 } - }) - end - end - end - context 'when the hash key is a string' do let(:hash) do