Class: Sipity::StateMachines::EtdStudentSubmission

Inherits:
Object
  • Object
show all
Defined in:
app/state_machines/sipity/state_machines/etd_student_submission.rb

Overview

Responsible for overseeing the life cycle of the ETD Submisssion process.

TODO: Magic Strings Everywhere! Deal with them.

REVIEW: How is this different from crafting a handful of runners? Perhaps

These should be codified as runners? Is there a symmetry of moving these
to runners? Is symmetry worth pursuing?

Constant Summary collapse

STATE_POLICY_QUESTION_ROLE_MAP =

TODO: Extract policy questions into separate class; There is a relationship, but is this necessary.

{ state => { policy_question => roles } }

{
  'new' => {
    update?: ['creating_user', 'advisor'], show?: ['creating_user', 'advisor'],
    delete?: ['creating_user'], submit_for_review?: ['creating_user']
  },
  'under_review' => {
    update?: ['etd_reviewer'], show?: ['creating_user', 'advisor', 'etd_reviewer'],
    request_revisions?: ['etd_reviewer'], approve_for_ingest?: ['etd_reviewer']
  },
  'ready_for_ingest' => { show?: ['creating_user', 'advisor', 'etd_reviewer'] },
  'ingested' => { show?: ['creating_user', 'advisor', 'etd_reviewer'] },
  'ready_for_cataloging' => { show?: ['creating_user', 'advisor', 'etd_reviewer', 'cataloger'], finish_cataloging?: ['cataloger'] },
  'cataloged' => { show?: ['creating_user', 'advisor', 'etd_reviewer', 'cataloger'] },
  'done' => { show?: ['creating_user', 'advisor', 'etd_reviewer', 'cataloger'] }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity:, user:, repository: nil) ⇒ EtdStudentSubmission

Returns a new instance of EtdStudentSubmission


32
33
34
35
36
37
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 32

def initialize(entity:, user:, repository: nil)
  @entity, @user = entity, user
  @repository = repository || default_repository
  # @TODO - Catch unexpected states.
  @state_machine = build_state_machine
end

Instance Attribute Details

#entityObject (readonly, private)

Returns the value of attribute entity


38
39
40
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 38

def entity
  @entity
end

#repositoryObject (readonly, private)

Returns the value of attribute repository


38
39
40
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 38

def repository
  @repository
end

#state_machineObject (readonly, private)

Returns the value of attribute state_machine


38
39
40
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 38

def state_machine
  @state_machine
end

#userObject (readonly, private)

Returns the value of attribute user


38
39
40
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 38

def user
  @user
end

Instance Method Details

#after_trigger_approve_for_ingest(_options) ⇒ Object (private)


77
78
79
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 77

def after_trigger_approve_for_ingest(_options)
  repository.submit_etd_student_submission_trigger!(entity: entity, trigger: :ingest, user: user)
end

#after_trigger_finish_cataloging(_options) ⇒ Object (private)


95
96
97
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 95

def after_trigger_finish_cataloging(_options)
  repository.submit_etd_student_submission_trigger!(entity: entity, trigger: :finish, user: user)
end

#after_trigger_ingest(_options) ⇒ Object (private)


81
82
83
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 81

def after_trigger_ingest(_options)
  repository.submit_ingest_etd(entity: entity)
end

#after_trigger_ingest_completed(options) ⇒ Object (private)


85
86
87
88
89
90
91
92
93
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 85

def after_trigger_ingest_completed(options)
  repository.grant_groups_permission_to_entity_for_role!(entity: entity, roles: 'cataloger')
  additional_emails = options.fetch(:additional_emails)
  repository.send_notification_for_entity_trigger(notification: "entity_ready_for_cataloging", entity: entity, to_roles: 'cataloger')
  repository.send_notification_for_entity_trigger(
    notification: "confirmation_of_entity_approved_for_ingest", entity: entity,
    to_roles: ['creating_user', 'advisor', 'etd_reviewer'], additional_emails: additional_emails
  )
end

#after_trigger_request_revisions(options) ⇒ Object (private)


70
71
72
73
74
75
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 70

def after_trigger_request_revisions(options)
  comments = options.fetch(:comments)
  repository.send_notification_for_entity_trigger(
    notification: "request_revisions_from_creator", entity: entity, to_roles: 'creating_user', comments: comments
  )
end

#after_trigger_submit_for_review(_options) ⇒ Object (private)


62
63
64
65
66
67
68
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 62

def after_trigger_submit_for_review(_options)
  repository.grant_groups_permission_to_entity_for_role!(entity: entity, roles: 'etd_reviewer')
  repository.send_notification_for_entity_trigger(
    notification: "confirmation_of_entity_submitted_for_review", entity: entity, to_roles: 'creating_user'
  )
  repository.send_notification_for_entity_trigger(notification: "entity_ready_for_review", entity: entity, to_roles: 'etd_reviewer')
end

#after_trigger_successful!(event, options = {}) ⇒ Object (private)


55
56
57
58
59
60
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 55

def after_trigger_successful!(event, options = {})
  repository.update_processing_state!(entity: entity, from: entity.processing_state, to: state_machine.state)
  repository.log_event!(entity: entity, user: user, event_name: convert_to_logged_name(event))
  include_private_methods = true
  send("after_trigger_#{event}", options) if respond_to?("after_trigger_#{event}", include_private_methods)
end

#build_state_machineObject (private)


99
100
101
102
103
104
105
106
107
108
109
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 99

def build_state_machine
  state_machine = MicroMachine.new(entity.processing_state)
  state_machine.when(:submit_for_review, 'new' => 'under_review')
  state_machine.when(:request_revisions, 'under_review' => 'under_review')
  state_machine.when(:approve_for_ingest, 'under_review' => 'ready_for_ingest')
  state_machine.when(:ingest, 'ready_for_ingest' => 'ingested')
  state_machine.when(:ingest_completed, 'ingested' => 'ready_for_cataloging')
  state_machine.when(:finish_cataloging, 'ready_for_cataloging' => 'cataloged')
  state_machine.when(:finish, 'cataloged' => 'done')
  state_machine
end

#convert_to_logged_name(event_name) ⇒ Object (private)

REVIEW: Will this be the convention? In other locations I'm using the Runner.


113
114
115
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 113

def convert_to_logged_name(event_name)
  "#{self.class.to_s.demodulize.underscore}/#{event_name}"
end

#default_repositoryObject (private)

REVIEW: Given that I need a repository, should this be teased into a runner.


119
120
121
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 119

def default_repository
  Repository.new
end

#roles_for_policy_question(policy_question) ⇒ Object


41
42
43
44
45
46
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 41

def roles_for_policy_question(policy_question)
  # @TODO - Catch invalid state look up
  STATE_POLICY_QUESTION_ROLE_MAP.fetch(entity.processing_state.to_s).fetch(policy_question, [])
rescue KeyError
  raise Exceptions::StatePolicyQuestionRoleMapError, state: entity.processing_state, context: self
end

#trigger!(event, options = {}) ⇒ Object


48
49
50
51
# File 'app/state_machines/sipity/state_machines/etd_student_submission.rb', line 48

def trigger!(event, options = {})
  state_machine.trigger!(event)
  after_trigger_successful!(event, options)
end