Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_states_for_event and to_states_for_event methods #792

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/aasm/base.rb
Expand Up @@ -187,11 +187,18 @@ def states_for_select
states.map { |state| state.for_select }
end

def from_states_for_event(event)
@state_machine.events[event].transitions.map { |t| t.from }.flatten.uniq
end

def to_states_for_event(event)
@state_machine.events[event].transitions.map { |t| t.to }.flatten.uniq
end

def from_states_for_state(state, options={})
if options[:transition]
@state_machine.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
else

events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/unit/inspection_spec.rb
Expand Up @@ -125,6 +125,33 @@
end
end

describe "aasm.from_states_for_event" do
it "should be callable" do
expect(SimpleExample.aasm).to respond_to(:from_states_for_event)
end

it "should return all from states (flattened)" do
froms = ComplexExample.aasm.from_states_for_event(:suspend)
[:passive, :pending, :active].each {|from| expect(froms).to include(from)}
end

it "should return all from states for any transitions on an event" do
froms = ParametrisedEvent.aasm.from_states_for_event(:dress)
[:sleeping, :showering].each {|from| expect(froms).to include(from)}
end
end

describe "aasm.to_states_for_event" do
it "should be callable" do
expect(SimpleExample.aasm).to respond_to(:to_states_for_event)
end

it "should return all to states for any transitions on an event" do
to_states = ParametrisedEvent.aasm.to_states_for_event(:dress)
[:working, :dating, :prettying_up].each {|to| expect(to_states).to include(to)}
end
end

describe 'permitted events' do
let(:foo) {Foo.new}

Expand Down