Search code examples
ruby-on-railsworkflow

How to handle multiple workflow in one Rails model?


We use geekq's Workflow gem in our Rails app. Now we need to deal with a situation which a workflow may end up differently. Here is an example:

A normal workflow for a purchase order may be issue PO=>receive delivery at front desk=>receive into production warehouse. For production item and non-production item, there are 2 workflows:

production item#: issue PO=>receive delivery at front desk=>receive into production warehouse
non-production item#: issue PO=>receive delivery at front desk

Our questions are:

  1. is workflow capable to handle workflow for production and non-production items?
  2. If the answer is yes, how to nicely handle different workflows?

Solution

  • On geekq/workflow spec, here is a section about conditional event transitions which provides a solution here:

    Conditions are procs or lambdas added to events, like so:
    
    state :off
      event :turn_on, :transition_to => :on,
                      :if => proc { |device| device.battery_level > 0 }
      event :turn_on, :transition_to => :low_battery,
                      :if => proc { |device| device.battery_level > 10 }
    end
    
    When calling a device.can_<fire_event>? check, or attempting a device.<event>!, each event is checked in turn:
    
        With no :if check, proceed as usual.
        If an :if check is present, proceed if it evaluates to true, or drop to the next event.
        If you've run out of events to check (eg. battery_level == 0), then the transition isn't possible.