更新後にモデルが新しい状態を返すようにしたかったのですが、これは、コントローラーに多くの「脂肪」を使用せずにこれを行うと考えられる最も簡単な方法であり、ワークフローが変更された場合に先に進むのが簡単になります。
class Article < ActiveRecord::Base
include Workflow
attr_accessible :workflow_state, :workflow_event # etc
validates_inclusion_of :workflow_event, in: %w(submit approve reject), allow_nil: true
after_validation :send_workflow_event
def workflow_event
@workflow_event
end
def workflow_event=(workflow_event)
@workflow_event = workflow_event
end
# this method should be private, normally, but I wanted to
# group the meaningful code together for this example
def send_workflow_event
if @workflow_event && self.send("can_#{@workflow_event}?")
self.send("#{@worklow_event}!")
end
end
# I pulled this from the workflow website, to use that example instead.
workflow do
state :new do
event :submit, :transitions_to => :awaiting_review
end
state :awaiting_review do
event :review, :transitions_to => :being_reviewed
end
state :being_reviewed do
event :accept, :transitions_to => :accepted
event :reject, :transitions_to => :rejected
end
state :accepted
state :rejected
end
end