1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

supporting instance level inspection for states

This commit is contained in:
Thorsten Böttger 2013-04-28 16:42:48 +02:00
parent 66d4a61bd0
commit e969ef6767
4 changed files with 41 additions and 11 deletions

View file

@ -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

View file

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

View file

@ -30,6 +30,10 @@ module AASM
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)

View file

@ -27,6 +27,24 @@ describe 'inspection for common cases' do
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
@ -67,17 +85,6 @@ describe :aasm_from_states_for_state do
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}