Skip to content

Commit 8c296ca

Browse files
committed
rubocop appeasement
1 parent f52a4a7 commit 8c296ca

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Layout/SpaceInsidePercentLiteralDelimiters:
6464
Enabled: false
6565

6666
Metrics/ClassLength:
67-
Max: 200
67+
Enabled: false
6868

6969
Metrics/ModuleLength:
7070
Enabled: false

lib/mongoid/association/embedded/embeds_many/proxy.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -443,34 +443,46 @@ def append(document)
443443
execute_callback :after_add, document
444444
end
445445

446+
# Returns a unique id for the document, which is either
447+
# its _id or its object_id.
448+
def id_of(doc)
449+
doc._id || doc.object_id
450+
end
451+
446452
# Optimized version of #append that handles multiple documents
447453
# in a more efficient way.
448454
def append_many(documents, &block)
449-
id_of = ->(doc){ doc._id || doc.object_id }
455+
visited_docs = Set.new(_target.map { |doc| id_of(doc) })
456+
unique_set = get_unique_new_docs(documents, visited_docs, &block)
457+
458+
_unscoped.concat(unique_set)
459+
_target.push(*scope(unique_set))
460+
update_attributes_hash
461+
462+
unique_set.each { |doc| execute_callback :after_add, doc }
463+
end
450464

451-
visited_docs = Set.new(_target.map(&id_of))
465+
# Return a list of unique new documents that do not yet exist
466+
# in the association, and which have not previously been seen.
467+
def get_unique_new_docs(documents, visited_docs, &block)
452468
next_index = _unscoped.size
453469

454-
unique_set = documents.select do |doc|
470+
documents.select do |doc|
455471
next unless doc
456-
next if visited_docs.include?(id_of[doc])
472+
473+
id = id_of(doc)
474+
next if visited_docs.include?(id)
457475

458476
execute_callback :before_add, doc
459477

460-
visited_docs.add(id_of[doc])
478+
visited_docs.add(id)
461479
integrate(doc)
462480

463481
doc._index = next_index
464482
next_index += 1
465483

466-
block.call(doc) if block
484+
block&.call(doc)
467485
end
468-
469-
_unscoped.concat(unique_set)
470-
_target.push(*scope(unique_set))
471-
update_attributes_hash
472-
473-
unique_set.each { |doc| execute_callback :after_add, doc }
474486
end
475487

476488
# Instantiate the binding associated with this association.

lib/mongoid/association/referenced/has_many/proxy.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# frozen_string_literal: true
22

3-
# TODO: consider refactoring this Proxy class, to satisfy the following
4-
# cops...
5-
# rubocop:disable Metrics/ClassLength
63
module Mongoid
74
module Association
85
module Referenced
@@ -588,4 +585,3 @@ def save_or_delay(doc, docs, inserts)
588585
end
589586
end
590587
end
591-
# rubocop:enable Metrics/ClassLength

0 commit comments

Comments
 (0)