Class: PaperTrail::RelatedChanges::Change
- Inherits:
-
Struct
- Object
- Struct
- PaperTrail::RelatedChanges::Change
- Defined in:
- lib/paper_trail/related_changes/change.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#description ⇒ Object
Returns the value of attribute description.
-
#diffs ⇒ Object
Returns the value of attribute diffs.
-
#event ⇒ Object
Returns the value of attribute event.
-
#merge_into_root ⇒ Object
Returns the value of attribute merge_into_root.
-
#requested_root ⇒ Object
Returns the value of attribute requested_root.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#resource_id ⇒ Object
Returns the value of attribute resource_id.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#user ⇒ Object
Returns the value of attribute user.
-
#version_id ⇒ Object
Returns the value of attribute version_id.
Instance Method Summary collapse
- #add_diff(args) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(diffs: [], merge_into_root: false, **args) ⇒ Change
constructor
A new instance of Change.
- #to_h ⇒ Object (also: #as_json)
Constructor Details
#initialize(diffs: [], merge_into_root: false, **args) ⇒ Change
Returns a new instance of Change.
14 15 16 |
# File 'lib/paper_trail/related_changes/change.rb', line 14 def initialize(diffs: [], merge_into_root: false, **args) super end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def children @children end |
#description ⇒ Object
Returns the value of attribute description
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def description @description end |
#diffs ⇒ Object
Returns the value of attribute diffs
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def diffs @diffs end |
#event ⇒ Object
Returns the value of attribute event
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def event @event end |
#merge_into_root ⇒ Object
Returns the value of attribute merge_into_root
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def merge_into_root @merge_into_root end |
#requested_root ⇒ Object
Returns the value of attribute requested_root
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def requested_root @requested_root end |
#resource ⇒ Object
Returns the value of attribute resource
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def resource @resource end |
#resource_id ⇒ Object
Returns the value of attribute resource_id
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def resource_id @resource_id end |
#timestamp ⇒ Object
Returns the value of attribute timestamp
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def @timestamp end |
#user ⇒ Object
Returns the value of attribute user
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def user @user end |
#version_id ⇒ Object
Returns the value of attribute version_id
2 3 4 |
# File 'lib/paper_trail/related_changes/change.rb', line 2 def version_id @version_id end |
Instance Method Details
#add_diff(args) ⇒ Object
36 37 38 |
# File 'lib/paper_trail/related_changes/change.rb', line 36 def add_diff(args) diffs << PaperTrail::RelatedChanges::Serializer::Diff.new(**args) end |
#empty? ⇒ Boolean
32 33 34 |
# File 'lib/paper_trail/related_changes/change.rb', line 32 def empty? diffs.count.zero? && children.count.zero? end |
#to_h ⇒ Object Also known as: as_json
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/paper_trail/related_changes/change.rb', line 18 def to_h(*) self.diffs = diffs .group_by(&:source) .map { |k, g| [k, g.sort_by(&:source_rank)] } # ie. segments can be in display order .sort_by { |_, g| g[0].rank } # Direct attributes shown first .flat_map(&:last) .uniq results = super().except(:merge_into_root).map { |k, v| [k, v.is_a?(Array) ? v.map(&:to_h) : v] }.to_h results.delete(:children) if results[:children].nil? results end |