Class: CurationConcerns::Workflow::StateMachineGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/curation_concerns/workflow/state_machine_generator.rb

Overview

Imports a single action for a workflow, including all of the adjacent states, permissions and notifications

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow:, action_name:, config:, email_generator_method_name: :schema_based_email_generator_method) ⇒ StateMachineGenerator

Returns a new instance of StateMachineGenerator.



13
14
15
16
17
18
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 13

def initialize(workflow:, action_name:, config:, email_generator_method_name: :schema_based_email_generator_method)
  self.workflow = workflow
  self.action_name = action_name
  self.config = config
  self.email_generator_method_name = email_generator_method_name
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



30
31
32
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 30

def action
  @action
end

Class Method Details

.generate_from_schema(workflow:, name:, **keywords) ⇒ Object



6
7
8
9
10
11
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 6

def self.generate_from_schema(workflow:, name:, **keywords)
  new(
    workflow: workflow, action_name: name, config: keywords,
    email_generator_method_name: :schema_based_email_generator_method
  ).call
end

Instance Method Details

#build_attributes(action_attributes) ⇒ Object



57
58
59
60
61
62
63
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 57

def build_attributes(action_attributes)
  action_attributes.delete('presentation_sequence') # TODO: remove this line when we want to support presentation_sequence
  existing_action_attributes = action.attributes.slice(*action_attributes.keys)
  unless action_attributes == existing_action_attributes
    action.update_attributes!(action_attributes)
  end
end

#build_from_state(state_names, state_roles) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 75

def build_from_state(state_names, state_roles)
  Array.wrap(state_names).each do |state_name|
    workflow_state = Sipity::WorkflowState.find_or_create_by!(workflow: workflow, name: state_name.to_s)
    PermissionGenerator.call(
      actors: [],
      roles: state_roles,
      workflow_state: workflow_state,
      action_names: action_name,
      workflow: workflow
    )
  end
end

#build_methods(method_list) ⇒ Object



88
89
90
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 88

def build_methods(method_list)
  MethodGenerator.call(action: action, list: method_list)
end

#build_transitions(name) ⇒ Object



65
66
67
68
69
70
71
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 65

def build_transitions(name)
  transition_to_state = Sipity::WorkflowState.find_or_create_by!(workflow: workflow, name: name)
  if action.resulting_workflow_state != transition_to_state
    action.resulting_workflow_state = transition_to_state
    action.save!
  end
end

#callObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 32

def call
  create_workflow_action!

  if config.key?(:attributes)
    build_attributes(config.fetch(:attributes).stringify_keys)
  end

  # Strategy State
  config.fetch(:from_states, []).each do |entry|
    build_from_state(entry.fetch(:names), entry.fetch(:roles))
  end

  # Transitions
  if config.key?(:transition_to)
    build_transitions(config.fetch(:transition_to).to_s)
  end

  send(email_generator_method_name, workflow: workflow, config: config)

  # Methods
  if config.key?(:methods)
    build_methods(Array.wrap(config.fetch(:methods)))
  end
end

#schema_based_email_generator_method(workflow:, config:) ⇒ Object



92
93
94
95
96
97
98
99
# File 'app/services/curation_concerns/workflow/state_machine_generator.rb', line 92

def schema_based_email_generator_method(workflow:, config:)
  Array.wrap(config.fetch(:notifications, [])).each do |configuration|
    notification_configuration = NotificationConfigurationParameter.build_from_workflow_action_configuration(
      workflow_action: action_name, config: configuration
    )
    NotificationGenerator.call(workflow: workflow, notification_configuration: notification_configuration)
  end
end