我有一个使用两个state machines
的模型class MyModel < ActiveRecord::Base
state_machine :call_state, :initial => :pending do
event :start do
transition :pending => :started
end
event :restart do
transition :started => :pending
end
event :finish do
transition :started => :finished
end
end
state_machine :payment_state, :initial => :unpaid do
event :pay do
transition :unpaid => :paid
end
event :pay_back do
transition :paid => :paid_back
end
event :reject_payment do
transition :unpaid => :rejected
end
end
#tons of methods related with call_state
#tons of methods related with payment_state
end
我想将此ActiveRecord
模型拆分为三个:
call_state
相关的定义和方法的模块/类payment_state
相关的定义和方法的模块/类同时使用的模块/类。第三个模块/类可以有像
这样的方法def can_call?
xxx.paid? and yyy.can_start?
end
其中xxx
/ yyy
可以是MyModel
实例,也可以是包装类中的方法。我不知道......
问题是:这样做的最佳方式是什么?
答案 0 :(得分:1)
简短回答:使用ActiveSupport::Concern将状态机分解为模块。
更长的版本:您可以查看How to define a state_machine in a Concern?,海报回答相同的问题。
希望这有帮助