Class: PaperTrailDiff::LiveGraphCollector

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

Overview

Flattens an already-preloaded, explicitly bounded live association graph.

Instance Method Summary collapse

Constructor Details

#initialize(tree:, traversal:) ⇒ LiveGraphCollector

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

Parameters:



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

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

Instance Method Details

#call(records) ⇒ Array[untyped]

: (Array) -> Array

Parameters:

  • (Array[untyped])

Returns:

  • (Array[untyped])


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

def call(records)
  collected = {} #: Hash[Array[untyped], untyped]
  collect(records, @tree, path: '', into: collected)
  collected.values
end

#children_for(records, reflection) ⇒ Array[untyped]

: (Array, untyped) -> Array

Parameters:

  • (Array[untyped])
  • (Object)

Returns:

  • (Array[untyped])


45
46
47
48
49
50
# File 'lib/paper_trail_diff/live_graph_collector.rb', line 45

def children_for(records, reflection)
  records.flat_map do |record|
    associated = record.public_send(reflection.name)
    reflection.collection? ? associated.to_a : Array(associated).compact
  end
end

#collect(records, tree, path:, into:) ⇒ void

This method returns an undefined value.

: (Array, AssociationTree, path: String, into: Hash[Array[untyped], untyped]) -> void

Parameters:

  • (Array[untyped])
  • (AssociationTree)
  • path: (String)
  • into: (Hash[Array[untyped], untyped])


26
27
28
29
30
31
# File 'lib/paper_trail_diff/live_graph_collector.rb', line 26

def collect(records, tree, path:, into:)
  records.each { |record| into[Endpoint.identity(record)] = record }
  records.group_by(&:class).each do |model_class, grouped|
    collect_group(model_class, grouped, tree, path: path, into: into)
  end
end

#collect_group(model_class, records, tree, path:, into:) ⇒ void

This method returns an undefined value.

: (untyped, Array, AssociationTree, path: String, into: Hash[Array[untyped], untyped]) -> void

Parameters:

  • (Object)
  • (Array[untyped])
  • (AssociationTree)
  • path: (String)
  • into: (Hash[Array[untyped], untyped])


34
35
36
37
38
39
40
41
42
# File 'lib/paper_trail_diff/live_graph_collector.rb', line 34

def collect_group(model_class, records, tree, path:, into:)
  @traversal.reflections_for(model_class, tree, path: path).each do |reflection|
    subtree = tree.child(reflection.name)
    next unless subtree

    child_path = Support.association_path(path, reflection.name.to_s)
    collect(children_for(records, reflection), subtree, path: child_path, into: into)
  end
end