ROR:工作流gem:我想从数据库实现动态状态 [英] ROR : Workflow gem : I want to implement dynamic states from database
问题描述
我目前正在从事一个必须实现动态工作流程的项目.
I am currrently working on a project that has to implement dynamic workflow.
动态:我将工作流的状态存储在名为 wf_steps 的数据库表中,工作流gem必须从数据库中为特定工作流创建
states
Dynamic: I store workflow's states in database table called wf_steps and the workflow gem has to create
states
for a particular workflow from the database
为此,我正在尝试使用工作流gem .您可以在gem的github页上查看它如何初始化状态和相应的事件.
For that I am trying to use the workflow gem. You can see how it initializes states and corresponding events in the gem's 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={}>
(在代码中已注释)
I expected in the code block below the term self.wf_steps
would return my SpsWorkflow
's instance/collection. However the self
keyword returns #<Workflow::Specification:0x000000063e23e8 @meta={}, @states={}>
when I use binding.pry
inside the workflow
method's block ( I commented in the code )
# 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
方法调用的块中使用变量.
I also tried storing the instance in a variable and using the variable inside the block passing to the workflow
method call.
class SpsWorkflow < ActiveRecord::Base
include Workflow
sps_instance = self
但是我得到了类SpsWorkflow
的实例,
But I got the instance of the class SpsWorkflow
like
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">
推荐答案
我终于使用activerecord回调解决了它
I finally solved it using a activerecord callback
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
这篇关于ROR:工作流gem:我想从数据库实现动态状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!