Skip to content

Commit

Permalink
supporting instance level inspection for states (including permissibl…
Browse files Browse the repository at this point in the history
…e state, see issue #54)
  • Loading branch information
alto committed Apr 28, 2013
1 parent e969ef6 commit 991c446
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

## 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))

## 3.0.16
Expand Down
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -304,7 +304,7 @@ class AddJobState < ActiveRecord::Migration
end
```

## Inspection ##
## <a id="inspection">Inspection

AASM supports a couple of methods to find out which states or events are provided or permissible.

Expand All @@ -316,6 +316,12 @@ job = Job.new
job.states
=> [:sleeping, :running, :cleaning]

job.states(:permissible => true)
=> [:running]
job.run
job.states(:permissible => true)
=> [:cleaning, :sleeping]

job.events
=> [:run, :clean, :sleep]
```
Expand Down
11 changes: 9 additions & 2 deletions lib/aasm/instance_base.rb
Expand Up @@ -30,8 +30,15 @@ def human_state
AASM::Localizer.new.human_state_name(@instance.class, current_state)
end

def states
@instance.class.aasm.states
def states(options={})
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

# QUESTION: shouldn't events and permissible_events be the same thing?
Expand Down
4 changes: 2 additions & 2 deletions spec/models/foo.rb
Expand Up @@ -5,11 +5,11 @@ class Foo
state :closed, :enter => :enter

event :close, :success => :success_callback do
transitions :to => :closed, :from => [:open]
transitions :from => [:open], :to => [:closed]
end

event :null do
transitions :to => :closed, :from => [:open], :guard => :always_false
transitions :from => [:open], :to => :closed, :guard => :always_false
end
end

Expand Down
26 changes: 24 additions & 2 deletions spec/unit/inspection_spec.rb
Expand Up @@ -28,15 +28,37 @@
end

context "instance level inspection" do
let(:foo) { Foo.new }
let(:two) { FooTwo.new }

it "delivers all states" do
foo = Foo.new
states = foo.aasm.states
states.should include(:open)
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

it "delivers all events" do
foo = Foo.new
events = foo.aasm.events
events.should include(:close)
events.should include(:null)
Expand Down

0 comments on commit 991c446

Please sign in to comment.