diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8b95d8ee4..ab14c4b95 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -815,14 +815,6 @@ Style/ArgumentsForwarding: - 'lib/mongoid/association/referenced/has_many/enumerable.rb' - 'lib/mongoid/association/referenced/has_many/proxy.rb' -# Offense count: 5 -Style/MultilineBlockChain: - Exclude: - - 'lib/mongoid/association/eager.rb' - - 'lib/mongoid/contextual/memory.rb' - - 'spec/mongoid/changeable_spec.rb' - - 'spec/mongoid/document_spec.rb' - # Offense count: 4 Style/OpenStructUse: Exclude: diff --git a/lib/mongoid/association/eager.rb b/lib/mongoid/association/eager.rb index e29d7f039..ba568ac05 100644 --- a/lib/mongoid/association/eager.rb +++ b/lib/mongoid/association/eager.rb @@ -80,9 +80,13 @@ def set_on_parent(id, element) # # @return [ Hash ] hash with grouped documents. def grouped_docs - @grouped_docs[@association.name] ||= @docs.group_by do |doc| + return @grouped_docs[@association.name] if @grouped_docs.key?(@association.name) + + docs_regroup = @docs.group_by do |doc| doc.send(group_by_key) if doc.respond_to?(group_by_key) - end.reject { |k, _| k.nil? } + end + + @grouped_docs[@association.name] = docs_regroup.reject { |k, _| k.nil? } end # Group the documents and return the keys. diff --git a/lib/mongoid/association/referenced/has_many/proxy.rb b/lib/mongoid/association/referenced/has_many/proxy.rb index 2f9407682..854629e60 100644 --- a/lib/mongoid/association/referenced/has_many/proxy.rb +++ b/lib/mongoid/association/referenced/has_many/proxy.rb @@ -147,7 +147,7 @@ def build(attributes = {}, type = nil) # @return [ Mongoid::Document ] The matching document. def delete(document) execute_callbacks_around(:remove, document) do - result = _target.delete(document) do |doc| + deleted_doc = _target.delete(document) do |doc| if doc unbind_one(doc) cascade!(doc) unless _assigning? @@ -155,7 +155,7 @@ def delete(document) end reset_unloaded - result + deleted_doc end end @@ -377,6 +377,15 @@ def append(document) end end + def delete_document_from_target(document) + _target.delete(document) do |doc| + if doc + unbind_one(doc) + cascade!(doc) unless _assigning? + end + end + end + # Execute before/after add callbacks around the block unless the objects # already have a persisted association. # diff --git a/spec/mongoid/changeable_spec.rb b/spec/mongoid/changeable_spec.rb index 6a461e0d1..a94c2f423 100644 --- a/spec/mongoid/changeable_spec.rb +++ b/spec/mongoid/changeable_spec.rb @@ -2042,11 +2042,8 @@ after do callback_kinds = %i[before after].freeze - Acolyte._save_callbacks.select do |callback| - callback_kinds.include?(callback.kind) - end.each do |callback| - Acolyte._save_callbacks.delete(callback) - end + callbacks_to_delete = Acolyte._save_callbacks.select { |callback| callback_kinds.include?(callback.kind) } + callbacks_to_delete.each { |callback| Acolyte._save_callbacks.delete(callback) } end it 'does not retain the changes until after all callbacks' do @@ -2074,11 +2071,8 @@ after do callback_kinds = %i[before after].freeze - Acolyte._save_callbacks.select do |callback| - callback_kinds.include?(callback.kind) - end.each do |callback| - Acolyte._save_callbacks.delete(callback) - end + callbacks_to_delete = Acolyte._save_callbacks.select { |callback| callback_kinds.include?(callback.kind) } + callbacks_to_delete.each { |callback| Acolyte._save_callbacks.delete(callback) } end it 'does not retain the changes until after all callbacks' do diff --git a/spec/mongoid/document_spec.rb b/spec/mongoid/document_spec.rb index e2ec62d75..5c860f308 100644 --- a/spec/mongoid/document_spec.rb +++ b/spec/mongoid/document_spec.rb @@ -34,21 +34,21 @@ end let(:new_klass) do - Class.new do + new_klass = Class.new do class << self; attr_accessor :name; end - end.tap { |new_klass| new_klass.name = new_klass_name } + end + new_klass.name = new_klass_name + new_klass end let(:new_model) do - new_klass.tap do - new_klass.send(:include, described_class) - end + new_klass.send(:include, described_class) + new_klass end let(:twice_a_new_model) do - new_klass.tap do - 2.times { new_klass.send(:include, described_class) } - end + 2.times { new_klass.send(:include, described_class) } + new_klass end context 'when Document has been included in a model' do