geekq / workflow

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

Allow one event in any state with same transition #195

Closed MathieuDerelle closed 5 years ago

MathieuDerelle commented 7 years ago

Is there a way to define an event accessible from any state ?

I've tried

workflow do
  event :invalid, transitions_to: :reject
end 

but it doesn't seem to work

If it's not supported now, could it be done in the future please ?

geekq commented 5 years ago
  1. You can put this line into every state definition

    event :invalid, transitions_to: :reject

  2. Or, just define a plain method, so you can call it any time

def invalid!
  # do whatever is needed, e.g. depending on `current_state`
  ...

  # persist the rejected state
  persist_workflow_state :reject
end

...
article.invalid!

Note on style: I would give a noun name to a state, like rejected, not reject and give an event a verb name, e.g. invalidate.