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

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



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

def self.included(base) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  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: %i[started pending completed]
      end

      event :release do
        transitions to: :released, from: %i[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



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

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

Returns:

  • (Boolean)


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

def editable?
  pending? or started?
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  completed? or released?
end

#release_with_user!(user) ⇒ Object



74
75
76
77
78
79
# File 'app/models/batch/state_machine_behaviour.rb', line 74

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



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

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