use aasm in concern

把原本寫在order model裡的aasm放在Concern裡的included block裡就好了,使用的方法跟原本一樣order.make_payment!

order.rb
class Order < ActiveRecord::Base
    include OrderAasm
 end
order_aasm.rb
module OrderAasm extend ActiveSupport::Concern
  included do
    include AASM
      aasm do
        state :order_placed,intial:true
        state :paid
        state :shipping
        state :shipped
        state :order_cancelled
        state :good_return
  
        event :make_payment , after_commit: :pay! do
          transitions form: :order_placed , to: :paid
        end
        event :ship do
          transitions form: :paid , to: :shipping
        end
        event :deliver do
          transitions form: :shipping , to: :shipped
        end
        event :return_good do
          transitions form: :shipped , to: :good_return
        end
        event :cancell_order do
          transitions form: [:order_placed , :paid] , to: :order_cancelled
        end
      end

  end
end

comments powered by Disqus