我正在使用带有rails的AASM,我一直在寻找解决我的问题的方法,我需要建立一个由用户配置的状态机系统。
想象一下这种情况:
使用AASM有一种简单的方法来实现这些案例吗?
我想象过这样的事情:
class Project < ActiveRecord::Base
include AASM
aasm do
State.where(model_name: 'Project').each do |database_state|
state database_state[:name], database_state[:initial]
end
Event.where(model_name: 'Project').each do |database_event|
...
end
end
end
答案 0 :(得分:1)
这是最终的模块文件,它的工作方式与我的计划方式相同:
module WorkflowModule
def self.included(base)
base.send(:include, AASM)
base.send(:aasm, column: 'aasm_state', no_direct_assignment: false) do
start_aasm(base)
end
end
def start_aasm(base)
State.where(entity: base.name.downcase).each_with_index do |state_database, index|
if index == 0
state state_database.name.to_sym, :initial => true
else
state state_database.name.to_sym
end
end
Transition
.joins(:workflow)
.where(workflows: { entity: base.name.downcase})
.each do |transition_database|
event transition_database.name.to_sym do
transitions :from => State.where(state_from_id: transition_database.state_from_id).name.to_sym,
:to => State.where(state_to_id: transition_database.state_to_id).name.to_sym
end
end
end
end