Module: Woulda::ActsAsStateMachine::Macros

Defined in:
lib/woulda/acts_as_state_machine/macros_new.rb,
lib/woulda/acts_as_state_machine/macros_old.rb

Instance Method Summary collapse

Instance Method Details

#should_act_as_state_machine(opts = {}) ⇒ Object

Example:

class Order < ActiveRecord::Base

acts_as_state_machine :initial => :open

state :open
state :closed

event :close_order do
  transitions :to => :closed, :from => :open
end

end

class OrderTest < Test::Unit::TestCase

# check the inital state
should_act_as_state_machine :initial => :open

# check states in addition to :initial
should_act_as_state_machine :initial => :open, :states => [:closed]

# check events and transitions
should_act_as_state_machine :events => {:close_order => {:to => :closed, :from :open}}

should_act_as_state_machine :initial => :open, :states => [:closed], :events => {:close_order => {:to => :closed, :from :open}}

end



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/woulda/acts_as_state_machine/macros_new.rb', line 31

def should_act_as_state_machine(opts={})
  klass = described_type

  initial_state, states, events, db_column = get_options!([opts], :initial, :states, :events, :column)

  states ||= []
  events ||= {}
  db_column ||= :state

  context "A #{klass.name}" do

    should_have_db_column db_column

    should "include the ActsAsStateMachine module" do
      assert klass.included_modules.include?(AASM)
    end

    should "define ActsAsStateMachine class methods" do
       assert klass.extended_by.include?(AASM::ClassMethods), "#{klass} doesn't define ActsAsStateMachine class methods"
    end

    should "define ActsAsStateMachine instance methods" do
      assert klass.include?(AASM::InstanceMethods), "#{klass} doesn't define ActsAsStateMachine instance methods"
    end

    should "have an intital state of #{initial_state}" do
      assert_equal initial_state, klass.aasm_initial_state, "#{klass} does not have an initial state of #{initial_state}"
    end

    states.each do |state|
      should "include state #{state}" do
        assert klass.aasm_states.map { |state| state.name}.include?(state), "#{klass} does not include state #{state}"
      end
    end

    events.each do |event, transition|

      should "define an event #{event}" do
        assert klass.aasm_events.has_key?(event), "#{klass} does not define event #{event}"
      end

      to   = transition[:to]
      from = transition[:from].is_a?(Symbol) ? [transition[:from]] : transition[:from]

      from.each do |from_state|
        should "transition to #{to} from #{from_state} on event #{event}" do
          assert_not_nil klass.aasm_events[event].instance_variable_get("@transitions").detect { |t| t.to == to && t.from == from_state }, "#{event} does not transition to #{to} from #{from_state}"
        end
      end

    end
  end   

end