Class: PaperTrailDiff::HistoricalAssociationReifier

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

Overview

Reconstructs selected nested edges at one root PaperTrail endpoint.

Instance Method Summary collapse

Constructor Details

#initialize(version, habtm_version: version) ⇒ HistoricalAssociationReifier

: (untyped, ?habtm_version: untyped) -> void

Parameters:

  • (Object)
  • habtm_version: (Object) (defaults to: version)


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

def initialize(version, habtm_version: version)
  @transaction_id = version.transaction_id if version.respond_to?(:transaction_id)
  if habtm_version.respond_to?(:transaction_id)
    @habtm_transaction_id = habtm_version.transaction_id
  end
  @version_at = version.created_at
  # 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_associations = reified.compare_by_identity
end

Instance Method Details

#optionsHash[Symbol, untyped]

: () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


87
88
89
90
91
92
93
94
# File 'lib/paper_trail_diff/historical_association_reifier.rb', line 87

def options
  {
    dup: true,
    mark_for_destruction: false,
    unversioned_attributes: :nil,
    version_at: @version_at
  }
end

#reifier(name) ⇒ Object

: (Symbol) -> untyped

Parameters:

  • (Symbol)

Returns:

  • (Object)


97
98
99
100
101
102
103
104
# File 'lib/paper_trail_diff/historical_association_reifier.rb', line 97

def reifier(name)
  tracking = Object.const_get(:PaperTrailAssociationTracking) #: untyped
  reifiers = tracking.const_get(:Reifiers) #: untyped
  reifiers.const_get(name)
rescue NameError => e
  message = 'loaded association tracking does not support nested reconstruction'
  raise AssociationTrackingUnavailableError, message, cause: e
end

#reify(record, reflections) ⇒ void

This method returns an undefined value.

: (untyped, Array) -> void

Parameters:

  • (Object)
  • (Array[untyped])


21
22
23
# File 'lib/paper_trail_diff/historical_association_reifier.rb', line 21

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
# File 'lib/paper_trail_diff/historical_association_reifier.rb', line 33

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

  reified[reflection.name] = true
  case reflection.macro
  when :belongs_to
    reifier(:BelongsTo).reify(reflection, record, options, @transaction_id)
  when :has_one
    reifier(:HasOne).reify(reflection, record, options, @transaction_id)
  when :has_many
    reify_has_many(record, reflection)
  when :has_and_belongs_to_many
    reify_habtm(record, reflection)
  end
end

#reify_habtm(record, reflection) ⇒ void

This method returns an undefined value.

: (untyped, untyped) -> void

Parameters:

  • (Object)
  • (Object)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/paper_trail_diff/historical_association_reifier.rb', line 69

def reify_habtm(record, reflection)
  unless @habtm_transaction_id
    message = 'HABTM reconstruction requires a transaction-backed endpoint version'
    raise IncompleteAssociationHistoryError, message
  end

  paper_trail = Object.const_get(:PaperTrail) #: untyped
  target_versioned = paper_trail.request.enabled_for_model?(reflection.klass)
  reifier(:HasAndBelongsToMany).reify(
    target_versioned,
    reflection,
    record,
    options,
    @habtm_transaction_id
  )
end

#reify_has_many(record, reflection) ⇒ void

This method returns an undefined value.

: (untyped, untyped) -> void

Parameters:

  • (Object)
  • (Object)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/paper_trail_diff/historical_association_reifier.rb', line 51

def reify_has_many(record, reflection)
  if reflection.options[:through]
    reify_association(record, reflection.through_reflection)
    reifier(:HasManyThrough).reify(reflection, record, options, @transaction_id)
    return
  end

  version_table = record.class.paper_trail.version_class.table_name
  reifier(:HasMany).reify(
    reflection,
    record,
    options,
    @transaction_id,
    version_table
  )
end