Class: PaperTrail::RelatedChanges::GroupedByRequestId

Inherits:
Object
  • Object
show all
Defined in:
lib/paper_trail/related_changes/grouped_by_request_id.rb

Overview

Goal of class is to group versions rows into groups that represent a user event. When a user saves a resource that may have many associated resources and we want to see that as one event. Use ActiveRecord#reflections build a tree of downward relationships and query the versions.object and version.object_changes Group by the request_id to collect all actions into an event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_type: nil, type: nil, item_id: nil, id: nil, limit: nil) ⇒ GroupedByRequestId

Returns a new instance of GroupedByRequestId.



12
13
14
15
16
17
18
19
20
21
# File 'lib/paper_trail/related_changes/grouped_by_request_id.rb', line 12

def initialize(item_type: nil,
               type: nil,
               item_id: nil,
               id: nil,
               limit: nil)
  @item_type         = (item_type || type).underscore.classify
  @item_id           = item_id || id
  @limit             = Integer([limit, 1_000].reject(&:blank?).first)
  @append_root       = true
end

Instance Attribute Details

#item_idObject (readonly)

Returns the value of attribute item_id.



8
9
10
# File 'lib/paper_trail/related_changes/grouped_by_request_id.rb', line 8

def item_id
  @item_id
end

#item_typeObject (readonly)

Returns the value of attribute item_type.



8
9
10
# File 'lib/paper_trail/related_changes/grouped_by_request_id.rb', line 8

def item_type
  @item_type
end

#limitObject (readonly)

Returns the value of attribute limit.



8
9
10
# File 'lib/paper_trail/related_changes/grouped_by_request_id.rb', line 8

def limit
  @limit
end

Instance Method Details

#raw_recordsObject



32
33
34
35
36
37
# File 'lib/paper_trail/related_changes/grouped_by_request_id.rb', line 32

def raw_records
  results.map do |result|
    root_version, versions = build_versions(result)
    [root_version, group_versions(versions), result["request_id"]]
  end.concat(append_root_version)
end

#to_aObject



23
24
25
26
27
28
29
30
# File 'lib/paper_trail/related_changes/grouped_by_request_id.rb', line 23

def to_a
  BuildChanges.new(
    raw_records,
    hierarchy.model_type_children,
    model_name,
    item_id
  ).call.take(limit) # appending the last root version can cause limit+1 sized results.
end