Skip to content

Instantly share code, notes, and snippets.

@electronicbites
Created June 15, 2012 15:45
Show Gist options
  • Save electronicbites/2937134 to your computer and use it in GitHub Desktop.
Save electronicbites/2937134 to your computer and use it in GitHub Desktop.
play with state_machine gem
class TestState
include Mongoid::Document
include Mongoid::Timestamps
state_machine :state, initial: :authorized do
event :capture_money do
transition :authorized => :captured
end
event :error_with_card do
transition :authorized => :error
end
event :retry do
transition :error => :foo
transition :authorized => :bang
transition :bang => :error
end
before_transition :authorized => :captured, :do => :charge
after_transition :authorized => :captured, :do => :did_capture
after_transition :authorized => :error, :do => :show_error
end
def try_capture_money
capture_money
error_with_card if state == "authorized"
end
def show_error
puts "show error: state: #{state}"
end
def charge
puts "charge"
false
end
def did_capture
puts "did_capture"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment