@@ -451,19 +451,38 @@ def id_of(doc)
451
451
452
452
# Optimized version of #append that handles multiple documents
453
453
# in a more efficient way.
454
+ #
455
+ # @param [ Array<Document> ] documents The documents to append.
456
+ #
457
+ # @return [ EmbedsMany::Proxy ] This proxy instance.
454
458
def append_many ( documents , &block )
455
- unique_set = get_unique_new_docs ( documents , &block )
459
+ unique_set = process_incoming_docs ( documents , &block )
456
460
457
461
_unscoped . concat ( unique_set )
458
462
_target . push ( *scope ( unique_set ) )
459
463
update_attributes_hash
460
464
461
465
unique_set . each { |doc | execute_callback :after_add , doc }
466
+
467
+ self
462
468
end
463
469
464
- # Return a list of unique new documents that do not yet exist
465
- # in the association.
466
- def get_unique_new_docs ( documents , &block )
470
+ # Processes the list of documents, building a list of those
471
+ # that are not already in the association, and preparing
472
+ # each unique document to be integrated into the association.
473
+ #
474
+ # The :before_add callback is executed for each unique document
475
+ # as part of this step.
476
+ #
477
+ # @param [ Array<Document> ] documents The incoming documents to
478
+ # process.
479
+ #
480
+ # @yield [ Document ] Optional block to call for each unique
481
+ # document.
482
+ #
483
+ # @return [ Array<Document> ] The list of unique documents that
484
+ # do not yet exist in the association.
485
+ def process_incoming_docs ( documents , &block )
467
486
visited_docs = Set . new ( _target . map { |doc | id_of ( doc ) } )
468
487
next_index = _unscoped . size
469
488
@@ -481,7 +500,7 @@ def get_unique_new_docs(documents, &block)
481
500
doc . _index = next_index
482
501
next_index += 1
483
502
484
- block &.call ( doc )
503
+ block &.call ( doc ) || true
485
504
end
486
505
end
487
506
0 commit comments