Module: StateMachinesTransactions

Defined in:
lib/state_machines_transactions.rb,
lib/state_machines_transactions/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#audit_transitionObject



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
# File 'lib/state_machines_transactions.rb', line 7

def audit_transition
  state_machine = self
  state_machine.after_transition(any => any) do |object, _transition|
    state_transaction = StateMachinesTransaction.create(
      object_type: object.class.name,
      object_id: object.id,
      state_field: state_machine.attribute,
      event: _transition.event,
      from_state: _transition.from,
      to_state: _transition.to,
      status: 'success'
    )
  end

  state_machine.after_failure do |object, _transition|
    state_transaction = StateMachinesTransaction.new(
      object_type: object.class.name,
      object_id: object.id,
      state_field: state_machine.attribute,
      event: _transition.event,
      from_state: _transition.from,
      to_state: _transition.to,
      status: 'failure'
    )
    CreateTransactionJob.perform_later(state_transaction.attributes)
  end
end