Skip to content

Commit

Permalink
instance-based events inspection now returns event instances (instead…
Browse files Browse the repository at this point in the history
… of the event names as symbol)
  • Loading branch information
alto committed Oct 12, 2014
1 parent d94bd3d commit 9346e6a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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]
```

Expand Down
22 changes: 22 additions & 0 deletions README_FROM_VERSION_3_TO_4.md
Expand Up @@ -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_
Expand Down
6 changes: 3 additions & 3 deletions lib/aasm/instance_base.rb
Expand Up @@ -35,7 +35,8 @@ def human_state
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
Expand All @@ -47,14 +48,13 @@ def states(options={})
# 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)
Expand Down

0 comments on commit 9346e6a

Please sign in to comment.