Skip to content

Commit

Permalink
added support for from state retrieval (for any given state)
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Oct 19, 2012
1 parent de80fea commit 8f974ee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 3.0.11

* added class method aasm_from_states_for_state to retrieve all from states (regarding transitions) for a given state

## 3.0.10

* added support for transitions from all other states (thanks to Stefan 'swrobel' Wrobel)
Expand Down
4 changes: 4 additions & 0 deletions lib/aasm/aasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def aasm_initial_state(set_state=nil)
end
end

def aasm_from_states_for_state(state)
aasm.events.map {|k,v| v.transitions_to_state(:active)}.flatten.map(&:from).flatten
end

# deprecated
def aasm_initial_state=(state)
AASM::StateMachine[self].initial_state = state
Expand Down
4 changes: 4 additions & 0 deletions lib/aasm/supporting_classes/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def transitions_from_state(state)
@transitions.select { |t| t.from == state }
end

def transitions_to_state(state)
@transitions.select { |t| t.to == state }
end

def all_transitions
@transitions
end
Expand Down
26 changes: 9 additions & 17 deletions spec/unit/aasm_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))

describe AASM, '- class level definitions' do
it 'should define a class level aasm_initial_state() method on its including class' do
it 'should define a class level methods on its including class' do
Foo.should respond_to(:aasm_initial_state)
end

it 'should define a class level aasm_state() method on its including class' do
Foo.should respond_to(:aasm_state)
end

it 'should define a class level aasm_event() method on its including class' do
Foo.should respond_to(:aasm_event)
end

it 'should define a class level aasm_states() method on its including class' do
Foo.should respond_to(:aasm_states)
end

it 'should define a class level aasm_states_for_select() method on its including class' do
Foo.should respond_to(:aasm_states_for_select)
end

it 'should define a class level aasm_events() method on its including class' do
Foo.should respond_to(:aasm_events)
Foo.should respond_to(:aasm_from_states_for_state)
end

end

describe "naming" do
Expand Down Expand Up @@ -61,6 +46,13 @@
end
end

describe "aasm_from_states_for_state" do
it "should return all from states for a state" do
froms = AuthMachine.aasm_from_states_for_state(:active)
[:pending, :passive, :suspended].each {|from| froms.should include(:pending)}
end
end

describe AASM, '- instance level definitions' do
before(:each) do
@foo = Foo.new
Expand Down

0 comments on commit 8f974ee

Please sign in to comment.