Class: PaperTrailDiff::PreparedAssociationReifier

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

Overview

Resolves indexed associations and delegates unsupported reflections to PT-AT.

Instance Method Summary collapse

Constructor Details

#initialize(history, boundary, habtm_boundary:, fallback:) ⇒ PreparedAssociationReifier

: (PreparedHistory, untyped, habtm_boundary: untyped, fallback: HistoricalAssociationReifier) -> void

Parameters:



8
9
10
11
12
13
14
15
16
17
# File 'lib/paper_trail_diff/prepared_association_reifier.rb', line 8

def initialize(history, boundary, habtm_boundary:, fallback:)
  @history = history
  @boundary = boundary
  @habtm_boundary = habtm_boundary
  @fallback = fallback
  # Keyed on record identity rather than `object_id`, which Ruby may reuse
  # once an earlier record in the same pass has been collected.
  reified = {} #: Hash[untyped, Hash[untyped, bool]]
  @reified = reified.compare_by_identity
end

Instance Method Details

#reify(record, reflections) ⇒ void

This method returns an undefined value.

: (untyped, Array) -> void

Parameters:

  • (Object)
  • (Array[untyped])


20
21
22
# File 'lib/paper_trail_diff/prepared_association_reifier.rb', line 20

def reify(record, reflections)
  reflections.each { |reflection| reify_association(record, reflection) }
end

#reify_association(record, reflection) ⇒ void

This method returns an undefined value.

: (untyped, untyped) -> void

Parameters:

  • (Object)
  • (Object)


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

def reify_association(record, reflection)
  reified = @reified[record] ||= {} #: Hash[untyped, bool]
  return if reified[reflection.name]

  reified[reflection.name] = true
  handled, records = @history.resolve(
    record,
    reflection,
    @boundary,
    habtm_boundary: @habtm_boundary
  )
  return @fallback.reify(record, [reflection]) unless handled

  association = record.association(reflection.name)
  association.target = reflection.collection? ? records : records.first
  association.loaded!
end