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

Rage against the state machine

Rage against the state machine

From LRUG March 2014 - A story about the problems we faced modelling state and recording state changes at GoCardless and how we generalised our solution to those problems into a new gem, Statesman.

Andy Appleton

March 10, 2014
Tweet

More Decks by Andy Appleton

Other Decks in Programming

Transcript

  1. Payment failure submitted DAY 5 DAY 4 Merchant receives failure

    notification DAY 3 Payment to merchant reversed DAY 0 DAY 1 Payer's bank receives request DAY 2 Merchant credited with payment Payment request submitted
  2. DAY 0 Mandate created DAY 1 Payer's bank receives mandate

    DAY 2 Mandate approved (DAY 3) Merchant notified of failure
  3. class Payment < ActiveRecord::Base
 has_many :payment_actions
 
 def gbp_mark_as_submitted
 self.payment_actions.create(..)


    end
 
 def eur_mark_as_submitted
 self.payment_actions.create(..)
 end
 end

  4. class PaymentStateMachine
 include Statesman::Machine
 
 state :pending, initial: true
 state

    :submitted
 
 transition from: :pending,
 to: :submitted
 end
  5. class Payment < ActiveRecord::Base
 has_many :payment_actions
 # ...
 def state_machine


    @state_machine ||= PaymentStateMachine.new(self,
 transition_class: PaymentAction)
 end
 end
  6. ?