Class: LcpRuby::Workflow::Approval::DataBuilder
- Inherits:
-
Object
- Object
- LcpRuby::Workflow::Approval::DataBuilder
- Defined in:
- lib/lcp_ruby/workflow/approval/data_builder.rb
Class Method Summary collapse
-
.build(record:, user:) ⇒ Object
Builds approval status data for a record.
Class Method Details
.build(record:, user:) ⇒ Object
Builds approval status data for a record. Returns nil if no active request exists, or a hash with:
{ request:, steps:, tasks_by_step:, user_tasks:, approval_definition:, delegation_allowed:, record: }
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lcp_ruby/workflow/approval/data_builder.rb', line 8 def self.build(record:, user:) return nil unless Registry.available? request = record.active_approval_request return nil unless request step_model = Registry.step_model task_model = Registry.task_model steps = step_model.where(approval_request_id: request.id).order(position: :asc).to_a all_tasks = task_model.where(approval_step_id: steps.map(&:id)).to_a tasks_by_step = all_tasks.group_by(&:approval_step_id) user_tasks = if user active_step_ids = Set.new(steps.select { |s| s.status == "active" }.map(&:id)) all_tasks.select { |t| active_step_ids.include?(t.approval_step_id) && t.approver_id == user.id && %w[pending claimed].include?(t.status) } else [] end approval_definition = begin Registry.workflow_for_request(request)&.approval_for(request.state_name) rescue MetadataError => e Rails.logger.warn("[LcpRuby] Failed to load approval definition for request #{request.id}: #{e.}") nil end { request: request, steps: steps, steps_by_id: steps.index_by(&:id), tasks_by_step: tasks_by_step, user_tasks: user_tasks, approval_definition: approval_definition, delegation_allowed: approval_definition&.allow_delegation == true, record: record } end |