所以我在我的项目中有多个模型,其中许多模型具有激活和停用功能,我使用AASM管理
aasm column: 'status' do
state :active, :initial => true
state :inactive
event :deactivate do
transitions :from => :active, :to => :inactive
end
event :activate do
transitions :from => :inactive, :to => :active
end
end
我想避免重复此代码,它有4个不同的模型,我可能不会再添加任何状态。
提前致谢
答案 0 :(得分:4)
您可以使用此部件创建模块并将其放入models/concerns/
或lib/
文件夹(或其他地方,只需确保已将其上传):
module ActivateDeactivateStatuses
extend ActiveSupport::Concern
included do
aasm column: 'status' do
state :active, :initial => true
state :inactive
event :deactivate do
transitions :from => :active, :to => :inactive
end
event :activate do
transitions :from => :inactive, :to => :active
end
end
end
end
并在你的模特中:
include ActivateDeactivateStatuses