Class: Sipity::Workflow

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/sipity/workflow.rb

Overview

A named workflow for processing an entity. Originally I had thought of calling this a Type, but once I extracted the Processing submodule, type felt to much of a noun, not conveying potentiality. Workflow conveys “things will happen” because of this.

Defined Under Namespace

Classes: NoActiveWorkflowError

Constant Summary collapse

DEFAULT_INITIAL_WORKFLOW_STATE =
'new'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activate!(permission_template:, workflow_id: nil, workflow_name: nil) ⇒ Sipity::Workflow

Within the given permission_template scope:

* Activate the specified workflow_id or workflow_name
* Deactivate the other workflows

Parameters:

  • permission_template (Hyrax::PermissionTemplate)

    The scope for activation of the workflow id

  • workflow_id (Integer) (defaults to: nil)

    The workflow_id within the given permission_template that should be activated

  • workflow_name (String) (defaults to: nil)

    The name of the workflow within the given permission template that should be activated

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    When we have a mismatch on permission template and workflow id or workflow name

  • (ArgumentError)

    When you don't specify a workflow_id or workflow_name


52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/sipity/workflow.rb', line 52

def self.activate!(permission_template:, workflow_id: nil, workflow_name: nil)
  raise(ArgumentError, "You must specify a workflow_id or workflow_name to activate!") if
    workflow_id.blank? && workflow_name.blank?
  workflow_to_activate = Sipity::Workflow.find_by!({ permission_template: permission_template, id: workflow_id, name: workflow_name }.compact)
  active_workflow = Sipity::Workflow.where(permission_template: permission_template, active: true)
  return workflow_to_activate if workflow_to_activate == active_workflow.first
  workflow_to_activate.tap do |workflow|
    active_workflow.update(active: nil)
    workflow.update!(active: true)
  end
end

.find_active_workflow_for(admin_set_id:) ⇒ Sipity::Workflow

Returns that is active for the given administrative set`.

Parameters:

  • admin_set_id (#to_s)

    the admin set to which we will scope our query.

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    when we don't have an active admin set for the given administrative set's ID


29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/sipity/workflow.rb', line 29

def self.find_active_workflow_for(admin_set_id:)
  templates = Hyrax::PermissionTemplate.arel_table
  workflows = Sipity::Workflow.arel_table
  Sipity::Workflow.where(active: true).where(
    workflows[:permission_template_id].in(
      templates.project(templates[:id]).where(templates[:source_id].eq(admin_set_id.to_s))
    )
  ).first!
rescue ActiveRecord::RecordNotFound => err
  raise NoActiveWorkflowError, err.message
end

Instance Method Details

#find_deposit_actionSipity::WorkflowAction

Find an action in this workflow that has no starting state. This is the deposit action.


70
71
72
73
74
75
76
77
# File 'app/models/sipity/workflow.rb', line 70

def find_deposit_action
  actions_that_lead_to_states = Sipity::WorkflowStateAction.all.pluck(:workflow_action_id)
  relation = Sipity::WorkflowAction.where(workflow: self)
  relation = relation.where('id NOT IN (?)', actions_that_lead_to_states) if actions_that_lead_to_states.any?
  relation.first!
rescue ActiveRecord::RecordNotFound => err
  raise NoActiveWorkflowError, err.message
end

#initial_workflow_stateObject


21
22
23
# File 'app/models/sipity/workflow.rb', line 21

def initial_workflow_state
  workflow_states.find_or_create_by!(name: DEFAULT_INITIAL_WORKFLOW_STATE)
end

#update_responsibilities(role:, agents:) ⇒ Object

Grant a workflow responsibility to a set of agents and remove it from any agents who currently have the workflow responsibility, but are not in the provided list

Parameters:


84
85
86
87
# File 'app/models/sipity/workflow.rb', line 84

def update_responsibilities(role:, agents:)
  add_workflow_responsibilities(role, agents)
  remove_workflow_responsibilities(role, agents)
end