instance-based events inspection now returns event instances (instead of the event names as symbol)

This commit is contained in:
Thorsten Böttger 2014-10-12 15:46:11 +02:00
parent d94bd3d623
commit 9346e6a859
4 changed files with 27 additions and 4 deletions

View File

@ -14,6 +14,7 @@
* **DSL change**: `:on_transition` renamed to `:after`
* **DSL change**: `:on_transition` renamed to `:after`
* **DSL change**: transition `:after` binding changed (see [issue #59](https://github.com/aasm/aasm/issues/59), thanks to [@stiff](https://github.com/stiff))
* **DSL change**: instance-based events inspection now returns event instances (instead of the event names as symbol)
## 3.9.0 (not yet released)

View File

@ -597,7 +597,7 @@ job.aasm.states(:permissible => true).map(&:name)
=> [:cleaning, :sleeping]
# show all possible (triggerable) events (allowed by transitions)
job.aasm.events
job.aasm.events.map(&:name)
=> [:sleep]
```

View File

@ -104,6 +104,28 @@ class Job < ActiveRecord::Base
end
```
### Instance-level inspection
Listing events for the current state now returns Event objects instead of event names (as symbols). So, change from
```ruby
job = Job.new
job.aasm.events
# => [:run]
```
to
```ruby
job = Job.new
job.aasm.events.map(&:name)
# => [:run]
```
## Could
### Triggering an event without _to_state_

View File

@ -35,7 +35,8 @@ module AASM
def states(options={})
if options[:permissible]
# ugliness level 1000
transitions = @instance.class.aasm.events.values_at(*permissible_events).compact.map {|e| e.transitions_from_state(current_state) }
permissible_event_names = permissible_events.map(&:name)
transitions = @instance.class.aasm.events.values_at(*permissible_event_names).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
@ -47,14 +48,13 @@ module AASM
# QUESTION: shouldn't events return objects instead of strings?
def events(state=current_state)
events = @instance.class.aasm.events.values.select {|e| e.transitions_from_state?(state) }
events.map {|e| e.name}
end
# filters the results of events_for_current_state so that only those that
# are really currently possible (given transition guards) are shown.
# QUESTION: what about events.permissible ?
def permissible_events
events.select{ |e| @instance.send(("may_" + e.to_s + "?").to_sym) }
events.select{ |e| @instance.send("may_#{e.name}?") }
end
def state_object_for_name(name)