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.

Constant Summary collapse

DEFAULT_INITIAL_WORKFLOW_STATE =
'new'.freeze

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

  • (RuntimeError)

    When you don’t specify a workflow_id or workflow_name



48
49
50
51
52
53
54
55
56
57
# File 'app/models/sipity/workflow.rb', line 48

def self.activate!(permission_template:, workflow_id: nil, workflow_name: nil)
  raise "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



27
28
29
30
31
32
33
34
35
# File 'app/models/sipity/workflow.rb', line 27

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))
    )
  ).first!
end

Instance Method Details

#initial_workflow_stateObject



19
20
21
# File 'app/models/sipity/workflow.rb', line 19

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:



64
65
66
67
# File 'app/models/sipity/workflow.rb', line 64

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