Module: Batch::StateMachineBehaviour

Included in:
Batch
Defined in:
app/models/batch/state_machine_behaviour.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


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
# File 'app/models/batch/state_machine_behaviour.rb', line 3

def self.included(base)
  base.class_eval do
    include AASM
    aasm column: :state, whiny_persistence: true do
      state :pending, initial: true
      state :started, enter: :start_requests
      state :completed
      state :released
      state :discarded

      # State Machine events
      event :start do
        transitions to: :started, from: [:pending]
      end

      event :complete do
        transitions to: :completed, from: [:started, :pending, :completed]
      end

      event :release do
        transitions to: :released, from: [:completed, :started, :pending, :released]
      end

      event :discard do
        transitions to: :discarded, from: [:pending]
      end
    end

    scope :failed, -> { where(production_state: 'fail') }

    # We override the behaviour of a couple of events because they require user details.
    alias_method(:start_without_user!, :start!)
    alias_method(:start!, :start_with_user!)
    alias_method(:complete_without_user!, :complete!)
    alias_method(:complete!, :complete_with_user!)
    alias_method(:release_without_user!, :release!)
    alias_method(:release!, :release_with_user!)
  end
end

Instance Method Details

#complete_with_user!(user) ⇒ Object


56
57
58
59
60
# File 'app/models/batch/state_machine_behaviour.rb', line 56

def complete_with_user!(user)
  complete_without_user! unless released?
  create_complete_batch_event_for(user)
  pipeline.post_finish_batch(self, user)
end

#editable?Boolean


47
48
49
# File 'app/models/batch/state_machine_behaviour.rb', line 47

def editable?
  pending? or started?
end

#finished?Boolean


43
44
45
# File 'app/models/batch/state_machine_behaviour.rb', line 43

def finished?
  completed? or released?
end

#release_with_user!(user) ⇒ Object


71
72
73
74
75
76
# File 'app/models/batch/state_machine_behaviour.rb', line 71

def release_with_user!(user)
  requests.each { |request| pipeline.completed_request_as_part_of_release_batch(request) }
  release_without_user!
  create_release_batch_event_for(user)
  pipeline.post_release_batch(self, user)
end

#start_with_user!(user) ⇒ Object


51
52
53
54
# File 'app/models/batch/state_machine_behaviour.rb', line 51

def start_with_user!(user)
  pipeline.on_start_batch(self, user)
  start_without_user!
end