Skip to content

Instantly share code, notes, and snippets.

@BDQ
Created June 29, 2012 08:49
Show Gist options
  • Save BDQ/3016726 to your computer and use it in GitHub Desktop.
Save BDQ/3016726 to your computer and use it in GitHub Desktop.
Example of editing existing State Machine
# Adding a new state
Order.state_machine.states << skrill_state = StateMachine::State.new(Order.state_machine, 'skrill')
# Redefining an event
Order.state_machine.events << next_event = StateMachine::Event.new(Order.state_machine, :next)
next_event.transition :from => 'cart', :to => 'address'
next_event.transition :from => 'address', :to => 'delivery'
next_event.transition :from => 'delivery', :to => 'payment', :if => :payment_required?
next_event.transition :from => 'payment', :to => 'skrill', :if => lambda {true}
next_event.transition :from => 'skrill', :to => 'complete'
next_event.transition :from => 'payment', :to => 'confirm', :if => Proc.new { Gateway.current && Gateway.current.payment_profiles_supported? }
next_event.transition :from => 'payment', :to => 'complete'
# Adding a tranisition
Order.state_machine.before_transition :to => 'skrill', :do => Proc.new{ |order|
skrill_account = SkrillAccount.find_or_create_by_email(order.email)
payment = order.payments.create(
:amount => order.total,
:source => skrill_account,
:payment_method => order.payment_method)
payment.started_processing!
payment.pend!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment