Skip to content

Commit

Permalink
List all states of a state machine.
Browse files Browse the repository at this point in the history
  • Loading branch information
troessner committed Nov 25, 2011
1 parent b775388 commit 3c5cb9d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ the name of the timestamp column.
transiions :from => :exploded, :to => :rebuilt
end

== Listing all the available states

You can easily get a listing of all available states:

Order.available_states # Uses the `default` state machine
# => [:pick_line_items, :picking_line_items]

In case you have multiple state machines you can also pass the state machine name:

Order.available_states(:your_machine)

= Documentation, Guides & Examples

- {Online API Documentation}[http://rdoc.info/github/troessner/transitions/master/Transitions]
Expand Down
4 changes: 4 additions & 0 deletions lib/transitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def state_machine(name = nil, options = {}, &block)
block ? state_machines[name].update(options, &block) : state_machines[name]
end

def available_states name = :default
state_machines[name].states.map(&:name).sort
end

def define_state_query_method(state_name)
name = "#{state_name}?"
undef_method(name) if method_defined?(name)
Expand Down
27 changes: 27 additions & 0 deletions test/test_available_states_listing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "helper"

class Bender
include Transitions

state_machine :default do
state :drinking
state :smoking
state :gambling
end

state_machine :maintenance do
state :wip
state :finished
end
end

class TestAvailableStatesListing < Test::Unit::TestCase
test 'available_states should return the states for the default state machine if no state machine is specified' do
assert_equal [:drinking, :gambling, :smoking], Bender.available_states
end

test 'available_states should return the states for a given state' do
assert_equal [:finished, :wip], Bender.available_states(:maintenance)
assert_equal [:drinking, :gambling, :smoking], Bender.available_states(:default)
end
end

0 comments on commit 3c5cb9d

Please sign in to comment.