supporting instance level inspection for states (including permissible state, see issue #54)
This commit is contained in:
parent
e969ef6767
commit
991c446ca2
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
* supporting instance level inspection for states
|
* supporting instance level inspection for states (including permissible state, see issue #54)
|
||||||
* added autocreation of constants for each state ([@jherdman](https://github.com/jherdman))
|
* added autocreation of constants for each state ([@jherdman](https://github.com/jherdman))
|
||||||
|
|
||||||
## 3.0.16
|
## 3.0.16
|
||||||
|
|
|
@ -304,7 +304,7 @@ class AddJobState < ActiveRecord::Migration
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
## Inspection ##
|
## <a id="inspection">Inspection
|
||||||
|
|
||||||
AASM supports a couple of methods to find out which states or events are provided or permissible.
|
AASM supports a couple of methods to find out which states or events are provided or permissible.
|
||||||
|
|
||||||
|
@ -316,6 +316,12 @@ job = Job.new
|
||||||
job.states
|
job.states
|
||||||
=> [:sleeping, :running, :cleaning]
|
=> [:sleeping, :running, :cleaning]
|
||||||
|
|
||||||
|
job.states(:permissible => true)
|
||||||
|
=> [:running]
|
||||||
|
job.run
|
||||||
|
job.states(:permissible => true)
|
||||||
|
=> [:cleaning, :sleeping]
|
||||||
|
|
||||||
job.events
|
job.events
|
||||||
=> [:run, :clean, :sleep]
|
=> [:run, :clean, :sleep]
|
||||||
```
|
```
|
||||||
|
|
|
@ -30,8 +30,15 @@ module AASM
|
||||||
AASM::Localizer.new.human_state_name(@instance.class, current_state)
|
AASM::Localizer.new.human_state_name(@instance.class, current_state)
|
||||||
end
|
end
|
||||||
|
|
||||||
def states
|
def states(options={})
|
||||||
@instance.class.aasm.states
|
if options[:permissible]
|
||||||
|
# ugliness level 1000
|
||||||
|
transitions = @instance.class.aasm.events.values.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
|
||||||
|
@instance.class.aasm.states
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# QUESTION: shouldn't events and permissible_events be the same thing?
|
# QUESTION: shouldn't events and permissible_events be the same thing?
|
||||||
|
|
|
@ -5,11 +5,11 @@ class Foo
|
||||||
state :closed, :enter => :enter
|
state :closed, :enter => :enter
|
||||||
|
|
||||||
event :close, :success => :success_callback do
|
event :close, :success => :success_callback do
|
||||||
transitions :to => :closed, :from => [:open]
|
transitions :from => [:open], :to => [:closed]
|
||||||
end
|
end
|
||||||
|
|
||||||
event :null do
|
event :null do
|
||||||
transitions :to => :closed, :from => [:open], :guard => :always_false
|
transitions :from => [:open], :to => :closed, :guard => :always_false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,15 +28,37 @@ describe 'inspection for common cases' do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "instance level inspection" do
|
context "instance level inspection" do
|
||||||
|
let(:foo) { Foo.new }
|
||||||
|
let(:two) { FooTwo.new }
|
||||||
|
|
||||||
it "delivers all states" do
|
it "delivers all states" do
|
||||||
foo = Foo.new
|
|
||||||
states = foo.aasm.states
|
states = foo.aasm.states
|
||||||
states.should include(:open)
|
states.should include(:open)
|
||||||
states.should include(:closed)
|
states.should include(:closed)
|
||||||
|
|
||||||
|
states = foo.aasm.states(:permissible => true)
|
||||||
|
states.should include(:closed)
|
||||||
|
states.should_not include(:open)
|
||||||
|
|
||||||
|
foo.close
|
||||||
|
foo.aasm.states(:permissible => true).should be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
it "delivers all states for subclasses" do
|
||||||
|
states = two.aasm.states
|
||||||
|
states.should include(:open)
|
||||||
|
states.should include(:closed)
|
||||||
|
states.should include(:foo)
|
||||||
|
|
||||||
|
states = two.aasm.states(:permissible => true)
|
||||||
|
states.should include(:closed)
|
||||||
|
states.should_not include(:open)
|
||||||
|
|
||||||
|
two.close
|
||||||
|
two.aasm.states(:permissible => true).should be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
it "delivers all events" do
|
it "delivers all events" do
|
||||||
foo = Foo.new
|
|
||||||
events = foo.aasm.events
|
events = foo.aasm.events
|
||||||
events.should include(:close)
|
events.should include(:close)
|
||||||
events.should include(:null)
|
events.should include(:null)
|
||||||
|
|
Loading…
Reference in New Issue