diff --git a/CHANGELOG.md b/CHANGELOG.md index a0c40a5..4d4263e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/aasm/aasm.rb b/lib/aasm/aasm.rb index 6572bfb..f52977a 100644 --- a/lib/aasm/aasm.rb +++ b/lib/aasm/aasm.rb @@ -31,6 +31,10 @@ module AASM 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 diff --git a/lib/aasm/supporting_classes/event.rb b/lib/aasm/supporting_classes/event.rb index 0ae4acf..127ca77 100644 --- a/lib/aasm/supporting_classes/event.rb +++ b/lib/aasm/supporting_classes/event.rb @@ -28,6 +28,10 @@ module AASM @transitions.select { |t| t.from == state } end + def transitions_to_state(state) + @transitions.select { |t| t.to == state } + end + def all_transitions @transitions end diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index ee4d921..b630af9 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -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 @@ -61,6 +46,13 @@ describe AASM, '- aasm_states_for_select' do 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