Module: Request::Statemachine

Extended by:
ActiveSupport::Concern
Included in:
Request
Defined in:
app/models/request/statemachine.rb

Overview

rubocop:todo Metrics/ModuleLength

Constant Summary collapse

COMPLETED_STATE =
%w[passed failed].freeze
OPENED_STATE =
%w[pending blocked started].freeze
ACTIVE =
%w[passed pending blocked started].freeze
INACTIVE =
%w[failed cancelled].freeze
SORT_ORDER =
%w[pending blocked hold started passed failed cancelled].freeze

Instance Method Summary collapse

Instance Method Details

#cancellable?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'app/models/request/statemachine.rb', line 225

def cancellable?
  %w[pending cancelled].include?(state)
end

#change_decision!void

Deprecated.

Favour retrospective_pass and retrospective_fail! instead. It is incredibly unlikely that you wish to arbitrarily toggle the state of a request And instead you probably have an explicit target state in mind. Use that instead.

This method returns an undefined value.

Toggles passed request to failed, and failed requests to pass.

Raises:

  • (StandardError)


171
172
173
174
175
176
# File 'app/models/request/statemachine.rb', line 171

def change_decision!
  return retrospective_fail! if passed?
  return retrospective_pass! if failed?

  raise StandardError, 'Can only use change decision on passed or failed requests'
end

#closed?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'app/models/request/statemachine.rb', line 217

def closed?
  %w[passed failed cancelled aborted].include?(state)
end

#failed_downstream!Object



205
206
207
# File 'app/models/request/statemachine.rb', line 205

def failed_downstream!
  # Do nothing by default
end

#failed_upstream!Object



195
196
197
198
199
200
201
202
203
# File 'app/models/request/statemachine.rb', line 195

def failed_upstream!
  # Don't transition it again if it's already reached an end state
  return if terminated?

  # Only transition it if *all* upstream requests are failed or cancelled, not just the one we came from.
  return unless upstream_requests.all?(&:terminated?)

  fail_from_upstream!
end

#finished?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'app/models/request/statemachine.rb', line 209

def finished?
  passed? || failed?
end

#on_blockedObject



189
190
# File 'app/models/request/statemachine.rb', line 189

def on_blocked
end

#on_cancelledObject



186
187
# File 'app/models/request/statemachine.rb', line 186

def on_cancelled
end

#on_failedObject



180
181
# File 'app/models/request/statemachine.rb', line 180

def on_failed
end

#on_holdObject



192
193
# File 'app/models/request/statemachine.rb', line 192

def on_hold
end

#on_passedObject



183
184
# File 'app/models/request/statemachine.rb', line 183

def on_passed
end

#on_startedObject

Default behaviour on started is to do nothing.



149
150
151
# File 'app/models/request/statemachine.rb', line 149

def on_started
  # Some subclasses may call transfer_aliquots below.
end

#open?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'app/models/request/statemachine.rb', line 221

def open?
  %w[pending started].include?(state)
end

#terminated?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'app/models/request/statemachine.rb', line 213

def terminated?
  failed? || cancelled?
end

#transfer_aliquotsObject

Aliquots are copied from the source asset to the target and updated with the project and study information from the request itself.



155
156
157
158
159
160
161
162
# File 'app/models/request/statemachine.rb', line 155

def transfer_aliquots
  target_asset.aliquots << asset.aliquots.map do |aliquot|
    aliquot.dup.tap do |clone|
      clone.study_id = initial_study_id || aliquot.study_id
      clone.project_id = initial_project_id || aliquot.project_id
    end
  end
end