如何在一个 Rails 模型中处理多个工作流程?
How to handle multiple workflow in one Rails model?
我们在 Rails 应用程序中使用 geekq 的工作流程 gem。现在我们需要处理工作流可能以不同方式结束的情况。这是一个例子:
purchase order
的正常工作流程可能是 issue PO=>receive delivery at front desk=>receive into production warehouse
。对于生产项目和非生产项目,有 2 个工作流程:
production item#: issue PO=>receive delivery at front desk=>receive into production warehouse
non-production item#: issue PO=>receive delivery at front desk
我们的问题是:
- 工作流是否能够处理生产和非生产项目的工作流?
- 如果答案是肯定的,如何很好地处理不同的工作流程?
在 geekq/workflow spec 上,这里有一个关于条件事件转换的部分,它在此处提供了解决方案:
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.
我们在 Rails 应用程序中使用 geekq 的工作流程 gem。现在我们需要处理工作流可能以不同方式结束的情况。这是一个例子:
purchase order
的正常工作流程可能是 issue PO=>receive delivery at front desk=>receive into production warehouse
。对于生产项目和非生产项目,有 2 个工作流程:
production item#: issue PO=>receive delivery at front desk=>receive into production warehouse
non-production item#: issue PO=>receive delivery at front desk
我们的问题是:
- 工作流是否能够处理生产和非生产项目的工作流?
- 如果答案是肯定的,如何很好地处理不同的工作流程?
在 geekq/workflow spec 上,这里有一个关于条件事件转换的部分,它在此处提供了解决方案:
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.