summaryrefslogtreecommitdiff
path: root/app/models/group_import_state.rb
blob: 7773b8872496c19d99f9a91bd63dc04b6c41b8fa (plain)
1
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
# frozen_string_literal: true

class GroupImportState < ApplicationRecord
  self.primary_key = :group_id

  belongs_to :group, inverse_of: :import_state

  validates :group, :status, :jid, presence: true

  state_machine :status, initial: :created do
    state :created, value: 0
    state :started, value: 1
    state :finished, value: 2
    state :failed, value: -1

    event :start do
      transition created: :started
    end

    event :finish do
      transition started: :finished
    end

    event :fail_op do
      transition any => :failed
    end

    after_transition any => :failed do |state, transition|
      last_error = transition.args.first

      state.update_column(:last_error, last_error) if last_error
    end
  end
end