如何查找工作流程中是否已触发事件?

时间:2015-12-02 17:56:41

标签: ruby-on-rails ruby workflow

我们在Rails应用中使用Geekq/workflow。这是一个例子:

class Article
  include Workflow
  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

很容易找到一个州,例如:awaiting_review是否经历过:

article.current_state < :awaiting_review

但是,我们没有找到检查事件是否已被触发的方法。是否使用工作流程的内置方法检查事件是否已被触发?或者如果不是这个工作流程,任何其他可以吗?

1 个答案:

答案 0 :(得分:0)

因此,您可以捕获on_create和on_update事件,因为ActiveRecord中存在回调,但对于工作流中的change_state,您需要通过以下方式覆盖工作流的转换:

class Article < BaseModel

include Workflow
  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

    on_transition do |from, to, event, *event_args|
       self.update_attribute(:status, to)
       # this triggers on_update callback on workflow transition
       # which you can use to log the event now
    end
  end
end

class BaseModel < ActiveRecord
  after_commit :log_create_event, on: [:create]
  after_commit :log_update_event, on: [:update]
  after_commit :log_destroy_event, on: [:destroy]

  def log_create_event
    #log_event('Create')
  end

  def log_update_event
    #log_event('Update')
  end

  def log_destroy_event
    #log_event('Destroy')
  end
end