bugfix: permissible events will respect given guards #150

This commit is contained in:
Thorsten Böttger 2014-07-12 15:24:10 +02:00
parent be0a999b5c
commit 75c81b1195
4 changed files with 11 additions and 4 deletions

View File

@ -4,7 +4,11 @@
* deprecated old aasm_* class methods (old-style DSL), in preparation for AASM v4.0.0
## 3.3.0 (not yet released)
## 3.3.1 (not yet released)
* bugfix: permissible events will respect given `guards` (see [issue #150](https://github.com/aasm/aasm/issues/150))
## 3.3.0
* support for Rails 4.1 enum fields (see [issue #124](https://github.com/aasm/aasm/issues/124), thanks to [@bkon](https://github.com/bkon))
* bugfix: allow lazy-evaluation for Rails 3 scopes (see [issue #144](https://github.com/aasm/aasm/issues/144), thanks to [@laurens](https://github.com/laurens))

View File

@ -35,7 +35,7 @@ module AASM
def states(options={})
if options[:permissible]
# ugliness level 1000
transitions = @instance.class.aasm.events.values.map {|e| e.transitions_from_state(current_state) }
transitions = @instance.class.aasm.events.values_at(*permissible_events).compact.map {|e| e.transitions_from_state(current_state) }
tos = transitions.map {|t| t[0] ? t[0].to : nil}.flatten.compact.map(&:to_sym).uniq
@instance.class.aasm.states.select {|s| tos.include?(s.name.to_sym)}
else

View File

@ -3,13 +3,14 @@ class Foo
aasm do
state :open, :initial => true, :exit => :exit
state :closed, :enter => :enter
state :final
event :close, :success => :success_callback do
transitions :from => [:open], :to => [:closed]
end
event :null do
transitions :from => [:open], :to => :closed, :guard => :always_false
transitions :from => [:open], :to => [:closed, :final], :guard => :always_false
end
end

View File

@ -22,10 +22,12 @@ describe 'inspection for common cases' do
states = foo.aasm.states
expect(states).to include(:open)
expect(states).to include(:closed)
expect(states).to include(:final)
states = foo.aasm.states(:permissible => true)
expect(states).to include(:closed)
expect(states).not_to include(:open)
expect(states).not_to include(:final)
foo.close
expect(foo.aasm.states(:permissible => true)).to be_empty
@ -77,7 +79,7 @@ end
describe 'aasm.states_for_select' do
it "should return a select friendly array of states" do
expect(Foo.aasm).to respond_to(:states_for_select)
expect(Foo.aasm.states_for_select).to eq([['Open', 'open'], ['Closed', 'closed']])
expect(Foo.aasm.states_for_select).to eq([['Open', 'open'], ['Closed', 'closed'], ['Final', 'final']])
end
end