geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.75k stars 207 forks source link

support for active record callbacks #182

Closed dhwanitgupta closed 2 years ago

dhwanitgupta commented 8 years ago

Hi

Problem: To log changes in any model I am using after_commit callback if active record, but when I change state using event then I am not able to use after_commit callback. Is there any way to handle this ?

Basically I want to log any change in model, as after_commit only runs after transaction block is successfully executed therefore I am using after_commit callback

Any help ?

asanger commented 8 years ago

What I had to do was add the following to my Workflow model:

workflow do

  ...

  on_transition do |from, to, event, *event_args|
    self.update_attribute(:status, to)
  end
end

It's a bit of a hack, but it ensures that the callbacks get triggered on every state transition. Been using it for quite a while now and haven't had any issues with it. Make sure that :status is the name of your state attribute.

kartikv11 commented 7 years ago

@geekq This issue can be closed, as the solution provided by @asanger is perfect.