Class: PaperTrailDiff::TraversalPreparer

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

Overview

Checks that a model can be traversed before any reconstruction starts, and that association tracking is actually available when history is involved.

Instance Method Summary collapse

Constructor Details

#initialize(tree:, traversal:) ⇒ TraversalPreparer

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

Parameters:



9
10
11
12
# File 'lib/paper_trail_diff/traversal_preparer.rb', line 9

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

Instance Method Details

#call(model_class, historical:) ⇒ void

This method returns an undefined value.

: (untyped, historical: bool) -> void

Parameters:

  • (Object)
  • historical: (Boolean)


15
16
17
18
19
20
21
# File 'lib/paper_trail_diff/traversal_preparer.rb', line 15

def call(model_class, historical:)
  return if @tree.empty?

  ensure_association_tracking! if historical
  @traversal.validate!(model_class)
  ensure_versioned_targets!(model_class) if historical
end

#ensure_association_tracking!void

This method returns an undefined value.

: () -> void



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/paper_trail_diff/traversal_preparer.rb', line 53

def ensure_association_tracking!
  paper_trail = Object.const_get(:PaperTrail) #: untyped
  config = paper_trail.config #: untyped
  available = defined?(::PaperTrailAssociationTracking) &&
              config.respond_to?(:track_associations?) &&
              config.track_associations?
  return if available

  message = 'association tracking must be loaded and enabled to compare historical associations'
  raise AssociationTrackingUnavailableError, message
end

#ensure_versioned_targets!(model_class) ⇒ void

This method returns an undefined value.

A model PaperTrail never versioned has no history to reconstruct, so a comparison over it can only ever answer "nothing changed" -- which is a wrong answer rather than an empty one. Live-to-live comparison reads current state and is unaffected, so this applies to historical work only. : (untyped) -> void

Parameters:

  • (Object)


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

def ensure_versioned_targets!(model_class)
  @traversal.selected_reflections(model_class).each do |path, reflection|
    # A polymorphic target is not known until a row names it; `diagnose`
    # reports that separately rather than guessing here.
    next if reflection.polymorphic?
    next if versioned?(reflection.klass)

    raise UnversionedAssociationError,
          "association #{path} cannot be compared historically: " \
          "#{reflection.klass.name} is not versioned. Add `has_paper_trail` to it, " \
          'or mirror the fields you need onto a model that has it.'
  end
end

#versioned?(model_class) ⇒ Boolean

: (untyped) -> bool

Parameters:

  • (Object)

Returns:

  • (Boolean)


48
49
50
# File 'lib/paper_trail_diff/traversal_preparer.rb', line 48

def versioned?(model_class)
  model_class.respond_to?(:paper_trail_options) && !model_class.paper_trail_options.nil?
end