Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Heteromorphic Fantasy: Obie's state_machine (2012)

Rob Howard
February 05, 2012

Heteromorphic Fantasy: Obie's state_machine (2012)

A walk-through of state machines with @obie's gem.

Code examples here: https://github.com/damncabbage/state_machine_example

Rob Howard

February 05, 2012
Tweet

More Decks by Rob Howard

Other Decks in Technology

Transcript

  1. state :details do validates :name, :presence => true validates :email,

    :presence => true end state :shipping do validates :address, :presence => true end
  2. state :on do def volume dial_rotation * 100 / max_rotation

    end end state :mute do def volume 0 end end
  3. state_machine :initial => 'cart', :use_transactions => false do event :next

    do transition :from => 'cart', :to => 'address' transition :from => 'address', :to => 'delivery' transition :from => 'delivery', :to => 'payment', :if => :payment_required? transition :from => 'delivery', :to => 'complete' transition :from => 'confirm', :to => 'complete' transition :from => 'payment', :to => 'confirm', :if => Proc.new { |order| order.payment_method && order.payment_method.payment_profiles_supported? } transition :from => 'payment', :to => 'complete' end event :cancel { transition :to => 'canceled', :if => :allow_cancel? } event :return { transition :to => 'returned', :from => 'awaiting_return' } event :resume { transition :to => 'resumed', :from => 'canceled', :if => :allow_resume? } event :authorize_return { transition :to => 'awaiting_return' } before_transition :to => 'complete' { |order| order.process_payments! } before_transition :to => ['delivery'] do |order| order.shipments.each { |s| s.destroy unless s.shipping_method.available_to_order?(order) } end after_transition :to => 'complete', :do => :finalize! after_transition :to => 'delivery', :do => :create_tax_charge! after_transition :to => 'payment', :do => :create_shipment! after_transition :to => 'resumed', :do => :after_resume after_transition :to => 'canceled', :do => :after_cancel end