AASMはclassの状態遷移を実装するgemです。
インストール
# Gemfile
gem 'aasm'
定義例(Model)
class Hoge < ActiveRecord::Base
include AASM
aasm column: :status do
state :deactivated, initial: true
state :activated
# activated -> deactivated
event :deactivate do
transitions from: :activated, to: :deactivated
end
# deactivated -> activated
event :activate do
transitions from: :deactivated, to: :activated
end
end
定義したステータスの一覧は下記命令で取得できます。
pry(main)> Hoge.aasm.states.map(&:name)
=> [:deactivated, :activated]