Skip to content

Success callbacks for transitions #236

Closed
@brega

Description

Do you think success callbacks

  • defined on transitions
  • trigger after the state is persisted

is something worth considering and sending a patch for?

Use case is something like

class Order < ActiveRecord::Base
  include AASM

  aasm do
    state :unconfirmed 
    state :cash_on_delivery
    state :paid

    event :confirm do
      transitions to: :cash_on_delivery, 
                     from: unconfirmed, 
                     success: proc { cash_on_delivery_callbacks },
                     guard: { payment_type == 'Cash on Delivery' }

      transitions to: :paid,
                     from: :unconfirmed,
                     success: proc { other_payment_type_callbacks },
                     guard: { payment_type != 'Cash on Delivery' }
    end
  end
end

Success callbacks would perform work specific to the transition path and would have to run only after state transition is successful.

New order of calling would be something like

begin
  event           before
  event           guards
  transition      guards
  old_state       before_exit
  old_state       exit
  transition      after
  new_state       before_enter
  new_state       enter
  ...update state...
  transition      success             # if persist successful
  event           success
  old_state       after_exit
  new_state       after_enter
  event           after
rescue
  event           error
end

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions