Module: Request::Statemachine

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

Overview

rubocop:todo Style/Documentation

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)

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

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)

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

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)

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

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

#failed_downstream!Object


200
201
202
# File 'app/models/request/statemachine.rb', line 200

def failed_downstream!
  # Do nothing by default
end

#failed_upstream!Object


196
197
198
# File 'app/models/request/statemachine.rb', line 196

def failed_upstream!
  fail_from_upstream! unless failed?
end

#finished?Boolean

Returns:

  • (Boolean)

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

def finished?
  passed? || failed?
end

#on_blockedObject


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

def on_blocked
end

#on_cancelledObject


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

def on_cancelled
end

#on_failedObject


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

def on_failed
end

#on_holdObject


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

def on_hold
end

#on_passedObject


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

def on_passed
end

#on_startedObject

Default behaviour on started is to do nothing.


151
152
153
# File 'app/models/request/statemachine.rb', line 151

def on_started
  # Some subclasses may call transfer_aliquots below.
end

#open?Boolean

Returns:

  • (Boolean)

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

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

#terminated?Boolean

Returns:

  • (Boolean)

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

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.


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

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