【发布时间】:2015-08-10 03:01:57
【问题描述】:
我目前正在从事一个必须实施动态工作流程的项目。
动态:我将工作流的状态存储在名为 wf_steps 的数据库表中,并且工作流 gem 必须为数据库中的特定工作流创建
states
为此,我尝试使用workflow gem。您可以在 gem 的 github-page 中看到它是如何初始化状态和相应事件的。
我的代码:
class SpsWorkflow < ActiveRecord::Base
belongs_to :user
has_many :wf_steps
include Workflow
workflow do
# binding.pry
# read wf-states from the database
# for now let event be same for all the states
self.wf_steps.each do |step|
state step.title.to_sym do
event :assign, transitions_to: :assigning
event :hire, transitions_to: :hiring
event :not_hire, transitions_to: :not_hiring
end
end
end
end
期望和遇到的问题:
我预计在术语 self.wf_steps 下方的代码块中将返回我的 SpsWorkflow 的实例/集合。但是,当我在 workflow 方法的块内使用 binding.pry 时,self 关键字返回 #<Workflow::Specification:0x000000063e23e8 @meta={}, @states={}>(我在代码中注释了)
# read wf-states from the database
# for now let event be same for all the states
self.wf_steps.each do |step|
state step.title.to_sym do
需要你帮忙,谢谢
编辑:
我还尝试将实例存储在一个变量中,并使用传递给workflow 方法调用的块内的变量。
class SpsWorkflow < ActiveRecord::Base
include Workflow
sps_instance = self
但是我得到了 SpsWorkflow 类的实例
SpsWorkflow(id: integer, workflow_state: string, assigned_to: integer, title: string, description: string, organization_id: integer, user_id: integer, created_at: datetime, updated_at: datetime)
但我想要
#<SpsWorkflow id: 1, workflow_state: "step1", assigned_to: nil, title: "Software Engineer", description: "Hire best engineer", organization_id: nil, user_id: 1, created_at: "2015-08-08 00:58:12", updated_at: "2015-08-08 00:58:12">
【问题讨论】:
标签: ruby-on-rails ruby workflow metaprogramming