class-based events now returns a list of Event instances (instead of a hash with event names as keys)

This commit is contained in:
Thorsten Böttger 2014-10-12 16:16:01 +02:00
parent 6ab03be38b
commit 1352b9b215
6 changed files with 25 additions and 8 deletions

View File

@ -16,6 +16,7 @@
* **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)
* **DSL change**: instance-based permissible_events has been removed in favor or events(:permissible => true)
* **DSL change**: class-based events now returns a list of Event instances (instead of a hash with event names as keys)
## 3.9.0 (not yet released)

View File

@ -143,6 +143,20 @@ job.aasm.events(:permissible => true)
# => [:run]
```
Class-based events now return a list of `Event` instances. Change from
```ruby
Job.aasm.events.values.map(&:name)
# => [:run]
```
to
```ruby
Job.aasm.events.map(&:name)
# => [:run]
```
## Could

View File

@ -160,7 +160,7 @@ private
end
def aasm_fire_event(event_name, options, *args, &block)
event = self.class.aasm.events[event_name]
event = self.class.aasm.state_machine.events[event_name]
begin
old_state = aasm.state_object_for_name(aasm.current_state)

View File

@ -1,6 +1,8 @@
module AASM
class Base
attr_reader :state_machine
def initialize(klass, options={}, &block)
@klass = klass
@state_machine = AASM::StateMachine[@klass]
@ -80,7 +82,7 @@ module AASM
end
def events
@state_machine.events
@state_machine.events.values
end
def states_for_select
@ -89,9 +91,9 @@ module AASM
def from_states_for_state(state, options={})
if options[:transition]
events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
@state_machine.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
else
events.map {|k,v| v.transitions_to_state(state)}.flatten.map(&:from).flatten
events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten
end
end

View File

@ -36,7 +36,7 @@ module AASM
if options[:permissible]
# ugliness level 1000
permissible_event_names = events(:permissible => true).map(&:name)
transitions = @instance.class.aasm.events.values_at(*permissible_event_names).compact.map {|e| e.transitions_from_state(current_state) }
transitions = @instance.class.aasm.state_machine.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
@ -46,7 +46,7 @@ module AASM
def events(options={})
state = options[:state] || current_state
events = @instance.class.aasm.events.values.select {|e| e.transitions_from_state?(state) }
events = @instance.class.aasm.events.select {|e| e.transitions_from_state?(state) }
if options[:permissible]
# filters the results of events_for_current_state so that only those that
@ -75,7 +75,7 @@ module AASM
end
def may_fire_event?(name, *args)
if event = @instance.class.aasm.events[name]
if event = @instance.class.aasm.state_machine.events[name]
event.may_fire?(@instance, *args)
else
false # unknown event

View File

@ -174,7 +174,7 @@ module AASM
success = options[:persist] ? self.class.transaction(:requires_new => requires_new?) { super } : super
if success && options[:persist]
event = self.class.aasm.events[name]
event = self.class.aasm.state_machine.events[name]
event.fire_callbacks(:after_commit, self)
end