Skip to content

Instantly share code, notes, and snippets.

@Martin91
Last active August 29, 2015 14:04
Show Gist options
  • Save Martin91/29b65130df0ce0ac87af to your computer and use it in GitHub Desktop.
Save Martin91/29b65130df0ce0ac87af to your computer and use it in GitHub Desktop.
implement orders' common state machine through state_machine or workflow
# state_machine: https://github.com/pluginaweek/state_machine
class Order < ActiveRecord::Base
state_machine :state, initial: :cart do
before_transition on: :checkout, do: :reduce_buyer_balance
after_transition on: :dispatch_goods, do: :create_shipping_log
after_transition on: :confirm, do: :charge_to_seller
event :checkout do
transition :cart => :paid
end
event :dispatch_goods do
transition :paid => :shipping
end
event :confirm do
transition :shipping => :finished
end
end
end
# workflow: https://github.com/geekq/workflow
class Order < ActiveRecord::Base
include Workflow
workflow do
state :cart do
event :checkout, transition_to: :paid
end
state :paid do
event :dispatch_goods, transition_to: :shipping
end
state :shipping do
event :confirm, transition_to: :finished
end
state :finished # this state declaration is required, even that no transition
end
def on_cart_exit
reduce_buyer_balance
end
def on_shipping_entry
create_shipping_log
end
def on_finished_entry
charge_to_seller
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment