[gem] AASM 狀態機
# Gemfile
# AASM is a continuation of the acts-as-state-machine rails plugin, built for plain Ruby objects.
gem 'aasm', '~> 4.12', '>= 4.12.0'
instance method
##
# check status
##
job.running? # => true
job.may_run? # => false
job.aasm.current_state # stage3
job.aasm_state # => 'sleeping'
job.aasm.human_state # => 'sleeping'
job.aasm.states(:permitted => true).map(&:name) # show all permitted states (from initial state)
job.aasm.states(:permitted => false).map(&:name) # show all non permitted states
job.aasm.events.map(&:name) # show all possible (triggerable) events from the current state
##
# change status 的方法要根據在 AASM 中 event 所定義的名稱
##
job.sleep # => triggered :sleep
job.sleep! # => triggered :sleep!
class method
Job.aasm.states_for_select # 產生可以直接套用到 select 的 options
erb 中帶入 select options
<%= select_tag :transaction_state, options_for_select(Transaction.aasm.states_for_select), class: 'form-control' %>
目錄
[TOC]
Automatic Scope
在 AASM 中內建了 Model Scope 讓我們使用:
# 假設 Order 有 paid 的狀態,那麼可以直接使用
Order.paid
i18n
AASM 支援在 i18n 定義:
zh-TW:
activerecord:
attributes:
order:
status/waiting_for_payment: 等待付款
status/paid: 已付款
status/failed: 已出貨
status/canceled: 已逾期
status/expired: 已退款
status/refunded: 已退款
使用範例
# ./OnePageShop/app/models/order.rb
class Order < ApplicationRecord
# Concerns macros
include AASM
# Attributes related macros
aasm column: 'status', no_direct_assignment: true, requires_lock: true do
# 等待付款
state :waiting_for_payment, initial: true
# 已付款
state :paid
# 已出貨
state :shipped
# 訂單逾期
state :expired
# 已退款
state :refunded
after_all_transitions :generate_status_transition_log!
event :mark_as_paid do
transitions from: %i(placed waiting_for_payment), to: :paid
end
event :mark_as_shipped do
transitions from: :paid, to: :shipped
end
event :mark_as_expired do
transitions from: :waiting_for_payment, to: :expired
end
event :mark_as_refunded do
transitions from: :paid, to: :refunded
end
end
# ...
private
# callback methods
def can_not_be_destroyed
throw :abort
end
def generate_status_transition_log!
status_logs.create!(status: aasm.to_state)
end
end
# ./OnePageShop/app/models/transaction.rb
class Transaction < ApplicationRecord
# Concerns macros
include AASM
# Attributes related macros
aasm column: 'status', no_direct_assignment: true, requires_lock: true do
# 等待繳款
state :waiting_for_payment, initial: true
# 已付款
state :paid
# 交易失敗
state :failed
# 取消
state :canceled
# 逾期
state :expired
# 已退款
state :refunded
after_all_transitions :generate_status_transition_log!
event :mark_as_paid do
transitions from: :waiting_for_payment, to: :paid
end
event :mark_as_paid do
transitions from: :waiting_for_payment, to: :failed
end
event :mark_as_canceled do
transitions from: :waiting_for_payment, to: :canceled
end
event :mark_as_expired do
transitions from: :waiting_for_payment, to: :expired
end
event :mark_as_refunded, after: :check_all_transactions_refunded! do
transitions from: :paid, to: :refunded
end
end
# callbacks
after_initialize :generate_trade_number
after_create :generate_status_change_log!
after_update :update_order_status!
private
# callback methods
def generate_status_change_log!
status_logs.create!(status: aasm.current_state)
end
def generate_status_transition_log!
status_logs.create!(status: aasm.to_state)
end
def update_order_status!
return if payment_info.nil?
if self.paid?
# set all waiting for payment transaction as canceled
self.order.transactions.waiting_for_payment.each do |transaction|
transaction.mark_as_canceled! if transaction.may_mark_as_canceled?
end
self.order.mark_as_paid! if self.order.may_mark_as_paid?
end
end
def check_all_transactions_refunded!
# 若沒有其餘等待付款或已付款的交易,則將訂單狀態改為已退款
if !Transaction.where(order_id: self.order_id, status: %i(waiting_for_payment paid)).exists?
self.order.mark_as_refunded!
end
end
end
參考
- AASM @ github