Module: Qcable::Statemachine

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

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
54
# 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: [: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: [: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 :available,   -> { where(state: :available) }
    scope :unavailable, -> { where(state: [:created, :pending, :failed, :passed, :destroyed, :qc_in_progress, :exhausted]) }
  end
end

Instance Method Details

#default_stateObject


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

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


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

def on_destroyed; end

#on_failedObject


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

def on_failed; end

#on_passedObject


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

def on_passed; end

#on_qcObject


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

def on_qc; end

#on_releasedObject


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

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. ++


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

def on_stamp
  lot.template.stamp_to(asset)
end

#on_usedObject


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

def on_used; end

#transition_to(target_state) ⇒ Object


84
85
86
# File 'app/models/qcable/statemachine.rb', line 84

def transition_to(target_state)
  aasm.fire!(suggested_transition_to(target_state))
end