geekq / workflow

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

Can I define an event that is for all states? #201

Closed timeon closed 5 years ago

timeon commented 7 years ago

For example, at any state, I would like to be reset the workflow to it's first state.

geekq commented 5 years ago

The question is, what would such event do beyond changing the state? Is it the same or different for all "from" states?

If it is different for every "from" state, you will need to name events differently to provide a separate handler for every transition.

If it is the same, then you can just define a plane method

class Article
    include Workflow

    def reset!
        self[:workflow_state] = :my_first_state
        save!
    end
    ...

If it is almost the same, but you would like to take advantage of on_exit transition hooks, you could run it directly:

run_on_exit(current_state, spec.states[:my_first_state], nil)