Skip to content

Commit

Permalink
supporting instance level inspection for states
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Apr 28, 2013
1 parent 66d4a61 commit e969ef6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* supporting instance level inspection for states
* added autocreation of constants for each state ([@jherdman](https://github.com/jherdman))

## 3.0.16
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,24 @@ class AddJobState < ActiveRecord::Migration
end
```

## Inspection ##

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

Given the `Job` class from above:

```ruby
job = Job.new

job.states
=> [:sleeping, :running, :cleaning]

job.events
=> [:run, :clean, :sleep]
```



## Installation ##

### Manually from RubyGems.org ###
Expand Down
4 changes: 4 additions & 0 deletions lib/aasm/instance_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def human_state
AASM::Localizer.new.human_state_name(@instance.class, current_state)
end

def states
@instance.class.aasm.states
end

# QUESTION: shouldn't events and permissible_events be the same thing?
# QUESTION: shouldn't events return objects instead of strings?
def events(state=current_state)
Expand Down
29 changes: 18 additions & 11 deletions spec/unit/inspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@
Foo.aasm.events.should include(:null)
end

context "instance level inspection" do
it "delivers all states" do
foo = Foo.new
states = foo.aasm.states
states.should include(:open)
states.should include(:closed)
end

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

it 'should list states in the order they have been defined' do
Conversation.aasm.states.should == [:needs_attention, :read, :closed, :awaiting_response, :junk]
end
Expand Down Expand Up @@ -67,17 +85,6 @@
end
end

describe :aasm_events_for_current_state do
let(:foo) {Foo.new}

it 'work' do
foo.aasm.events.should include(:close)
foo.aasm.events.should include(:null)
foo.close
foo.aasm.events.should be_empty
end
end

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

Expand Down

0 comments on commit e969ef6

Please sign in to comment.