Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
super simple state machine for activerecord
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
MIT-LICENSE
README
Rakefile
super_state.gemspec

README

SuperState
==========

Super Simple state machine for Ruby and Rails
(designed for ActiveRecord, but hopefully easily extended to any ActiveModel)

Example
=======

a simple multi-state model

  class MyModel < ActiveRecord::Base

    include SuperState

    super_state :pending, :initial => true
    super_state :processing
    super_state :completed
    super_state :failed

    super_state_group :outstanding, ["pending", "processing"]

    # first part of a two stage transition
    # eg.
    #   def process
    #     start_processing!
    #       do_the_stuff
    #     complete_processing!
    #   end
    #
    state_transition :start_processing, :pending => :processing

    # second part of a two stage transition
    state_transition :complete_processing, :processing => :completed

    # transition direct from pending to complete
    state_transition :complete, :pending => :completed

    # failed to process
    state_transition :fail, :processing => :failed

    # back to processing
    state_transition :restart, :failed => :processing
  
  end

this is actually already shipped as a magic mixin

  class MyClass < ActiveRecord::Base
    include SuperState::CommonStates
  end
  
we can also take arguments in our transitions

  class Loan < ActiveRecord::Base
  
    include SuperState
    
    super_state :requested, :initial => true
    super_state :disbursed
    
    state_transition :disburse, :requested => :disbursed do |params|
      self.disbursed_amount = params[:amount]
      self.disbursed_date   = params[:disbursed_date]
    end
    
  end


Copyright (c) 2010 [Matthew Rudy Jacobs], released under the MIT license
Something went wrong with that request. Please try again.