ROR:工作流gem:我想从数据库实现动态状态
ROR : Workflow gem : I want to implement dynamic states from database
我目前正在从事一个必须实施动态工作流的项目。
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
为此,我正在尝试使用 workflow gem。您可以在 gem 的 github 页面中看到它如何初始化状态和相应的事件。
我的代码:
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
会 return 我的 SpsWorkflow
的 instance/collection。但是 self
关键字 returns #<Workflow::Specification:0x000000063e23e8 @meta={}, @states={}>
当我在 workflow
方法块中使用 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
需要你的帮助,谢谢
编辑:
我还尝试将实例存储在一个变量中,并在传递给 workflow
方法调用的块中使用该变量。
class SpsWorkflow < ActiveRecord::Base
include Workflow
sps_instance = self
但是我得到了 class 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">
您使用过:
workflow do
self.something
end
self
在此块的上下文中将引用 WorkflowSpecification
。如果你真的想访问 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
我目前正在从事一个必须实施动态工作流的项目。
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
为此,我正在尝试使用 workflow gem。您可以在 gem 的 github 页面中看到它如何初始化状态和相应的事件。
我的代码:
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
会 return 我的 SpsWorkflow
的 instance/collection。但是 self
关键字 returns #<Workflow::Specification:0x000000063e23e8 @meta={}, @states={}>
当我在 workflow
方法块中使用 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
需要你的帮助,谢谢
编辑:
我还尝试将实例存储在一个变量中,并在传递给 workflow
方法调用的块中使用该变量。
class SpsWorkflow < ActiveRecord::Base
include Workflow
sps_instance = self
但是我得到了 class 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">
您使用过:
workflow do
self.something
end
self
在此块的上下文中将引用 WorkflowSpecification
。如果你真的想访问 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