Module: Qcable::Statemachine

Included in:
Qcable
Defined in:
app/models/qcable/statemachine.rb

Overview

rubocop:todo Style/Documentation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/qcable/statemachine.rb', line 2

def self.included(base)
  base.class_eval do
    ## State machine
    ## namespace: true as destroyed clashes with rails, but we can't easily rename the state
    aasm column: :state, whiny_persistence: true, namespace: true, name: 'qc_state' do
      state :created
      state :pending,        enter: :on_stamp
      state :failed,         enter: :on_failed
      state :passed,         enter: :on_passed
      state :available,      enter: :on_released
      state :destroyed,      enter: :on_destroyed
      state :qc_in_progress, enter: :on_qc
      state :exhausted,      enter: :on_used

      initial_state Proc.new { |qcable| qcable.default_state }

      # State Machine events
      event :do_stamp do
        transitions to: :pending, from: [:created]
      end

      event :destroy_labware, allow_automated?: true do
        transitions to: :destroyed, from: %i[pending available]
      end

      event :qc, allow_automated?: true do
        transitions to: :qc_in_progress, from: [:pending]
      end

      event :release do
        transitions to: :available, from: [:pending]
      end

      event :pass do
        transitions to: :passed, from: [:qc_in_progress]
      end

      event :fail do
        transitions to: :failed, from: %i[qc_in_progress pending]
      end

      event :use, allow_automated?: true do
        transitions to: :exhausted, from: [:available]
      end
    end

    # new version of combinable named_scope
    scope :for_state, ->(state) { where(state: state) }

    scope :unavailable, -> { where(state: %i[created pending failed passed destroyed qc_in_progress exhausted]) }
  end
end

Instance Method Details

#default_stateObject


64
65
66
67
68
69
# File 'app/models/qcable/statemachine.rb', line 64

def default_state
  # We validate the presence of lot, however initial state gets called BEFORE we reach validation
  return :created if lot.nil?

  asset_purpose.default_state.to_sym || :created
end

#on_destroyedObject


77
# File 'app/models/qcable/statemachine.rb', line 77

def on_destroyed; end

#on_failedObject


71
# File 'app/models/qcable/statemachine.rb', line 71

def on_failed; end

#on_passedObject


73
# File 'app/models/qcable/statemachine.rb', line 73

def on_passed; end

#on_qcObject


79
# File 'app/models/qcable/statemachine.rb', line 79

def on_qc; end

#on_releasedObject


75
# File 'app/models/qcable/statemachine.rb', line 75

def on_released; end

#on_stampObject

– These are the callbacks that will be made on entry to a given state. This allows derived classes to override these and add custom behaviour. You are advised to call super in any method that you override so that they can be stacked. ++


60
61
62
# File 'app/models/qcable/statemachine.rb', line 60

def on_stamp
  lot.template.stamp_to(asset)
end

#on_usedObject


81
# File 'app/models/qcable/statemachine.rb', line 81

def on_used; end