ROR:Workflow gem:我想从数据库实现动态状态
我正在研究一个必须实现动态工作流的项目。
动态 :我将工作流的状态存储在名为wf_steps的数据库表中,工作流gem必须从数据库为特定工作流创建
states
为此,我试图使用工作流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
方法的块中使用#
时, self
关键字返回#
(我在代码中注释)
# 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)
但我想要
#
你用过:
workflow do self.something end
self
在此块的上下文中将引用WorkflowSpecification
。 如果您真的想要访问SpsWorkflow
的实例, SpsWorkflow
可能必须将其传递到块中或将其分配给另一个变量并在那里使用它。
我终于使用activerecord回调解决了它
class SpsWorkflow < ActiveRecord::Base include Workflow after_initialize do sps_instance = self SpsWorkflow.workflow do # read wf-states as well as events from the database sps_instance.wf_steps.each do |step| state step.title.to_sym do event :assign, transitions_to: :step2 event :hire, transitions_to: :hire event :not_hire, transitions_to: :not_hiring end end end end belongs_to :user has_many :wf_steps end