为 AASM 中的所有转换注册回调?
我想在每次状态转换后调用 2 个方法。现在我正在做:
aasm_event :nominate_for_publishing, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :under_review, :from => [:work_in_progress]
end
aasm_event :publish, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :published, :from => [:work_in_progress, :under_review], :guard => :is_publishable?
end
aasm_event :unpublish, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :work_in_progress, :from => [:published, :under_review]
end
显然这不是最好的方法。我正在复制代码,更根本的是,当回调真正应用于整个状态机时,我将回调与特定的转换相关联。有什么更好的方法来处理这个问题?
There are 2 methods I want to call after every state transition. Right now I'm doing:
aasm_event :nominate_for_publishing, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :under_review, :from => [:work_in_progress]
end
aasm_event :publish, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :published, :from => [:work_in_progress, :under_review], :guard => :is_publishable?
end
aasm_event :unpublish, :before => [:set_state_last_updated_by, :set_state_updated_at] do
transitions :to => :work_in_progress, :from => [:published, :under_review]
end
Obviously this isn't the best approach. I'm duplicating code, and more fundamentally, I'm associating callbacks with specific transitions when they really apply to the state machine as a whole. What's a better way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不直接使用脏属性来检查保存时状态是否已更改?
就像这样,
Why not just use dirty attributes to check if the state has been changed on save?
Like so,
查看 aasm 回调。您想使用
after_all_transitions
方法。Look aasm callbacks. You want to use
after_all_transitions
method.