geekq / workflow

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

A way to create 'global' events? #147

Closed kevindewalt closed 9 years ago

kevindewalt commented 9 years ago

I'm trying to DRY up some code. Is there a way to create 'global' events?

For instance, suppose a customer can transition through multiple states and the customer could transition to :canceled from any one of them.

Do I have to create the :cancel event for every state? for instance ...

  workflow do

    state :new do
      event :cancel, :transitions_to => :canceled

      event :qualify, :transitions_to => :qual_completed
    end

    state :qual_completed do
      event :cancel, :transitions_to => :canceled

      event :contact, :transitions_to => :contacted
    end

  ...

  end
jondkinney commented 9 years ago

I'm used to being able to do this in AASM and am wondering this too.

geekq commented 9 years ago

In workflow DSL every transition has to be explicitly specified.

In your example I am wondering, if really every state needs a cancel event or all but one (cancelled). How we could specify such an exception - via some naming convention, via additional syntax like

workflow do
  event :cancel, transitions_to: :canceled, except: [:cancelled, :some_other]

This can quickly become messy... I mean both, in usage (DSL) and implementation ;-) (workflow library).