Sha256: a573c0741d54e74e12c8f40be953ebc6c7cfa2087a7a15e172388ce09b5563d1

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require 'active_record'
require 'active_record/edge-state-machine'

ActiveRecord::Base.establish_connection(:adapter  => "sqlite3", :database => ":memory:")

class DoubleMachineActiveRecord < ActiveRecord::Base
  include ActiveRecord::EdgeStateMachine

  state_machine do
    # the machine is automatically named :default
    # the scopes are not created by default
    create_scopes true
    # the persistence instance variable is the default one (:state)
    state :first_state # first one is initial state
    state :second_state
    state :third_state # the user in this state can't sign in

    event :first_move do
      transition :from => :first_state, :to => :second_state, :on_transition => :do_move
    end

    event :second_move do
      transition :from => :second_state, :to => :third_state
    end
  end

  state_machine :second do
    # the scopes are not created by default
    create_scopes true
    # for the second machine we must specify the name of the persistence instance variable
    # so there are no conflicts between the state machines
    persisted_to :second_state
    initial_state :red
    state :blue
    state :green
    state :red

    event :go_blue do
      transition :from => [:red, :green], :to => :blue
    end

    event :go_red do
      transition :from => [:blue, :green], :to => :red, :on_transition => [:turn_off, :color_in_red]
    end

    event :go_green do
      transition :from => [:blue, :red], :to => :green
    end
  end

  def do_move
  end

  def turn_off
  end

  def color_in_red
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
edge-state-machine-1.0.1 spec/active_record/samples/double_machine.rb
edge-state-machine-1.0.0 spec/active_record/samples/double_machine.rb
edge-state-machine-0.9.1 spec/active_record/samples/double_machine.rb
edge-state-machine-0.9.0 spec/active_record/samples/double_machine.rb