使用状态机进行简单的订单处理,只想在每个事件后记录DB中的更改。 也许有人对如何重构这个有任何想法:所以我不需要很多记录方法和after_transition
def record
self.status_transition.update(:event => "placed", :from => "draft" , :to => "placed" )
end
def record_cancel
self.status_transition.update(:event => "canceled", :from => "placed" , :to => "draft" )
end
state_machine initial: :draft do
state :draft, value: 0
state :placed, value: 1
state :paid, value: 2
state :canceled, value: 3
after_transition :on => :place, :do => :record
after_transition :on => :cancel, :do => :record_cancel
event :place do
transition :draft => :placed
end
event :cancel do
transition :placed => :draft
end
event :pay do
transition :placed => :paid
end
end
答案 0 :(得分:1)
您可以定义一般转换回调,例如
after_transition any => any do |order, transition|
order.status_transition.update(event: transition.event,
from: transition.from,
to: transition.to)
end
有关可以在transition
块参数上调用的方法,请参阅StateMachine::Transition。