Sha256: bc16ee212c129f4881e2fb9f5b9e07223f4ff17a68bcbfe3c0f5ba4db025eb29

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module RailsWorkflow
  # Process stores service information including links to process template,
  # all operations, parent operation, context etc.
  class Process < ActiveRecord::Base
    include Status

    belongs_to :template, class_name: 'RailsWorkflow::ProcessTemplate'
    has_many :operations, class_name: 'RailsWorkflow::Operation'
    has_one :parent_operation,
            class_name: 'RailsWorkflow::Operation',
            foreign_key: :child_process_id

    alias parent parent_operation
    has_one :context, class_name: 'RailsWorkflow::Context', as: :parent
    has_many :workflow_errors, class_name: 'RailsWorkflow::Error', as: :parent

    delegate :data, to: :context
    scope :by_status, ->(status) { where(status: status) }

    def manager
      @manager ||= template.manager_class.new(self)
    end

    def self.count_by_statuses
      query = RailsWorkflow.config.sql_dialect::COUNT_STATUSES

      statuses = connection.select_all(query).rows

      statuses_array.map do |status|
        statuses.detect { |s| s.first.to_i == status }.try(:last).to_i
      end
    end

    def self.statuses_array
      (NOT_STARTED..ROLLBACK).to_a
    end

    def uncompleted?
      uncompleted_statuses.include?(status) &&
        uncompleted_operations.size.zero?
    end

    # Returns set or operation that not yet completed.
    # Operation complete in DONE, SKIPPED, CANCELED, etc many other statuses
    def uncompleted_operations
      operations.reject(&:completed?)
    end

    def can_start?
      [Status::NOT_STARTED, Status::IN_PROGRESS]
        .include?(status) && !operations.empty?
    end

    def unresolved_errors
      workflow_errors.unresolved.where.not(id: id)
    end

    def complete
      self.status = Status::DONE
      save
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_workflow-0.4.3 app/models/rails_workflow/process.rb
rails_workflow-0.4.2 app/models/rails_workflow/process.rb
rails_workflow-0.4.1 app/models/rails_workflow/process.rb
rails_workflow-0.4.0 app/models/rails_workflow/process.rb