Class: ApprovalEngine::History

Inherits:
Object
  • Object
show all
Defined in:
app/models/approval_engine/history.rb

Overview

A read-only view of everything a record has gone through: every approval it spawned, the track/step tree beneath each, and a flat chronological timeline of the actions taken (with actors and comments).

It assembles the data; who may see it and how it’s rendered is the host’s call — wrap it in your own authorization and UI.

history = invoice.approval_history
history.approvals   # => newest-first, tree preloaded (no N+1)
history.events      # => chronological audit entries across everything

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ History

Returns a new instance of History.



17
18
19
# File 'app/models/approval_engine/history.rb', line 17

def initialize(record)
  @record = record
end

Class Method Details

.for(record) ⇒ Object



13
14
15
# File 'app/models/approval_engine/history.rb', line 13

def self.for(record)
  new(record)
end

Instance Method Details

#approvalsObject

Every approval for the record, newest first, with tracks, steps and their assigned actors eager-loaded so traversal doesn’t fan out into N+1 queries.



24
25
26
27
28
29
# File 'app/models/approval_engine/history.rb', line 24

def approvals
  @approvals ||= Approval.where(target: @record)
                         .includes(tracks: { steps: :assigned_actor })
                         .order(created_at: :desc)
                         .to_a
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/approval_engine/history.rb', line 35

def empty?
  approvals.empty?
end

#events(limit: 500) ⇒ Object

The “what happened” narrative: every step action (approved / rejected / changes_requested) across all of this record’s approvals and iterations, oldest first. Queried (and ordered + capped) in the database rather than by walking the whole tree in Ruby, with the polymorphic actors preloaded. Each entry is an AuditLog, so the host can read its event, actors (intended vs actual), comment, timestamp and step context.



45
46
47
48
49
50
# File 'app/models/approval_engine/history.rb', line 45

def events(limit: 500)
  AuditLog.where(approval_engine_step_id: step_ids)
          .preload(:actual_actor, :intended_actor, step: :assigned_actor)
          .order(:created_at)
          .limit(limit)
end

#latestObject



31
32
33
# File 'app/models/approval_engine/history.rb', line 31

def latest
  approvals.first
end