aasm/aasm — pull request #170 by hjdivad (2014-09-15 21:28:33+00:00)
Title: Add :after_commit callbacks to events.

Event-specific :after_commit callbacks are called in the order that the events
were called. They are called even when the record is manually persisted. For
the sake of consistency, the same is now true of the state :after_commit
callback. As in that case, this feature is specific to active record.

This is really useful when you a) have implicit state transitions; b) have
state-specific validations and c) have event hooks that you only want to fire
once the transition is committed: for example because they can't be rolled back
(like notifications via a third party).

Imagine if you have something like the following:

```rb
class Invoice
aasm do
state :due
state :paid, after_commit: :notify_of_payment!

event :pay do
transitions from: :due, to: :paid
end
end

with_options if: :paid? do
validates_presence_of :transaction_id
end

def payment_number= n
write_attribute :payment_number, n
pay if payment_number_changed?
end
end
```

You want `notify_of_payment!` to happen on the `pay` transition but only if the
invoice could actually be saved. When there's state-specific validation it gets
tricky because you want to actually change state *before* validation so you get
the state-specific validatino, but you want the hook to fire *after* validation
(indeed after a succesful persistence).