Class: PaperTrailDiff::ActivitySnapshotDelta

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

Overview

Internal scalar transition for one isolated PaperTrail update event.

Instance Method Summary collapse

Constructor Details

#initialize(before_attributes:, after_attributes:) ⇒ ActivitySnapshotDelta

: (before_attributes: Hash[untyped, untyped], after_attributes: Hash[untyped, untyped]) -> void

Parameters:

  • before_attributes: (Hash[untyped, untyped])
  • after_attributes: (Hash[untyped, untyped])


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

def initialize(before_attributes:, after_attributes:)
  @before_attributes = normalize_hash(before_attributes)
  @after_attributes = normalize_hash(after_attributes)
  freeze
end

Instance Method Details

#after_value(name) ⇒ Object

: (untyped) -> untyped

Parameters:

  • (Object)

Returns:

  • (Object)


36
37
38
# File 'lib/paper_trail_diff/activity_snapshot_delta.rb', line 36

def after_value(name)
  @after_attributes[name.to_s]
end

#apply(snapshot) ⇒ RecordSnapshot

: (RecordSnapshot) -> RecordSnapshot

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/paper_trail_diff/activity_snapshot_delta.rb', line 15

def apply(snapshot)
  attributes = snapshot.attributes.dup
  attributes.each_key do |name|
    attributes[name] = @after_attributes[name] if @after_attributes.key?(name)
  end
  return snapshot if attributes == snapshot.attributes

  RecordSnapshot.new(
    type: snapshot.type,
    id: snapshot.id,
    attributes: attributes,
    associations: snapshot.associations
  )
end

#before_value(name) ⇒ Object

: (untyped) -> untyped

Parameters:

  • (Object)

Returns:

  • (Object)


31
32
33
# File 'lib/paper_trail_diff/activity_snapshot_delta.rb', line 31

def before_value(name)
  @before_attributes[name.to_s]
end

#normalize_hash(hash) ⇒ Hash[String, untyped]

: (Hash[untyped, untyped]) -> Hash[String, untyped]

Parameters:

  • (Hash[untyped, untyped])

Returns:

  • (Hash[String, untyped])


53
54
55
# File 'lib/paper_trail_diff/activity_snapshot_delta.rb', line 53

def normalize_hash(hash)
  hash.to_h { |name, value| [name.to_s, value] }.freeze
end

#relationship_changed?(reflection) ⇒ Boolean

: (untyped) -> bool

Parameters:

  • (Object)

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/paper_trail_diff/activity_snapshot_delta.rb', line 41

def relationship_changed?(reflection)
  relationship_keys(reflection).any? do |name|
    before_value(name).to_s != after_value(name).to_s
  end
end

#relationship_keys(reflection) ⇒ Array[untyped]

: (untyped) -> Array

Parameters:

  • (Object)

Returns:

  • (Array[untyped])


58
59
60
61
62
# File 'lib/paper_trail_diff/activity_snapshot_delta.rb', line 58

def relationship_keys(reflection)
  keys = Array(reflection.foreign_key)
  keys << reflection.type if reflection.options[:as]
  keys
end