Class: PaperTrailDiff::ActivitySnapshotDelta
- Inherits:
-
Object
- Object
- PaperTrailDiff::ActivitySnapshotDelta
- 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
-
#after_value(name) ⇒ Object
: (untyped) -> untyped.
-
#apply(snapshot) ⇒ RecordSnapshot
: (RecordSnapshot) -> RecordSnapshot.
-
#before_value(name) ⇒ Object
: (untyped) -> untyped.
-
#initialize(before_attributes:, after_attributes:) ⇒ ActivitySnapshotDelta
constructor
: (before_attributes: Hash[untyped, untyped], after_attributes: Hash[untyped, untyped]) -> void.
-
#normalize_hash(hash) ⇒ Hash[String, untyped]
: (Hash[untyped, untyped]) -> Hash[String, untyped].
-
#relationship_changed?(reflection) ⇒ Boolean
: (untyped) -> bool.
-
#relationship_keys(reflection) ⇒ Array[untyped]
: (untyped) -> Array.
Constructor Details
#initialize(before_attributes:, after_attributes:) ⇒ ActivitySnapshotDelta
: (before_attributes: Hash[untyped, untyped], after_attributes: Hash[untyped, untyped]) -> void
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
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
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
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]
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
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
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.[:as] keys end |