Class: PaperTrailDiff::PreloadedEndpointBatchLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb,
sig/generated/paper_trail_diff/preloaded_endpoint_batch_loader.rbs

Overview

Validates and reuses caller-owned live records without issuing association queries.

Instance Method Summary collapse

Constructor Details

#initialize(tree:, traversal:) ⇒ PreloadedEndpointBatchLoader

: (tree: AssociationTree, traversal: AssociationTraversal) -> void

Parameters:



8
9
10
11
# File 'lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb', line 8

def initialize(tree:, traversal:)
  @tree = tree
  @traversal = traversal
end

Instance Method Details

#call(records) ⇒ Hash[identity, untyped]

: (Array) -> Hash[identity, untyped]

Parameters:

  • (Array[untyped])

Returns:

  • (Hash[identity, untyped])


14
15
16
17
# File 'lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb', line 14

def call(records)
  records.each { |record| validate_record(record, @tree, path: '') }
  records.to_h { |record| [Endpoint.identity(record), record] }
end

#validate_association(record, tree, path, reflection) ⇒ void

This method returns an undefined value.

: (untyped, AssociationTree, String, untyped) -> void

Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb', line 32

def validate_association(record, tree, path, reflection)
  association_path = Support.association_path(path, reflection.name.to_s)
  association = record.association(reflection.name)
  unless association.loaded?
    message = "current endpoint association is not preloaded: #{association_path}"
    raise UnloadedAssociationError, message
  end

  subtree = tree.child(reflection.name)
  return unless subtree

  Array(association.target).compact.each do |child|
    validate_record(child, subtree, path: association_path)
  end
end

#validate_record(record, tree, path:) ⇒ void

This method returns an undefined value.

: (untyped, AssociationTree, path: String) -> void

Parameters:



25
26
27
28
29
# File 'lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb', line 25

def validate_record(record, tree, path:)
  @traversal.reflections_for(record.class, tree, path: path).each do |reflection|
    validate_association(record, tree, path, reflection)
  end
end