Module: QcReport::StateMachine

Included in:
QcReport
Defined in:
app/models/qc_report.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

rubocop:todo Metrics/MethodLength



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/qc_report.rb', line 17

def self.included(base) # rubocop:todo Metrics/AbcSize
  base.class_eval do
    # When adding new states, please make sure you update the config/locals/en.yml file
    # with descriptions.

    # We disable the transactions here as otherwise our entire job gets wrapped in a transaction.
    # This causes problems for very large reports, as it causes objects with after transaction callbacks to
    # be persisted for the entire job. We'd already chunked our transaction into batches, but this transaction
    # was counteracting that.
    aasm column: :state, whiny_persistence: true, use_transactions: false do
      # A report has just been created and is awaiting processing. There is probably a corresponding delayed job
      state :queued, initial: true

      # A report has failed one or more times. Generally this means there is a problem.
      state :requeued

      # The report has been picked up by the delayed job. Entry into this state triggers building.
      state :generating, after_enter: :generate_report

      # The report has been generated and is awaiting customer feedback
      state :awaiting_proceed

      # Customer feedback has been uploaded. This is generally an end state, but a report can be re-uploaded
      # at a later date if necessary.
      state :complete

      # Triggered automatically on after_create. This event is handled via
      # schedule_report, which creates a delayed job. It can be called manually.
      event :generate do
        transitions from: %i[queued requeued], to: :generating
      end

      # Called on report failure. Generally the delayed job will cycle it through a few times
      # but most reports in this state will require manual intervention.
      event :requeue do
        transitions from: :generating, to: :requeued
      end

      # Called automatically when a report is generated
      event :generation_complete do
        transitions from: :generating, to: :awaiting_proceed
      end

      # A QC report might be uploaded multiple times
      event :proceed_decision do
        transitions from: %i[complete awaiting_proceed], to: :complete
      end
    end

    def available?
      awaiting_proceed? or complete?
    end

    extend ClassMethods
  end
end