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

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**: 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 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**: 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) ## 3.9.0 (not yet released)

View file

@ -143,6 +143,20 @@ job.aasm.events(:permissible => true)
# => [:run] # => [: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 ## Could

View file

@ -160,7 +160,7 @@ private
end end
def aasm_fire_event(event_name, options, *args, &block) 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 begin
old_state = aasm.state_object_for_name(aasm.current_state) old_state = aasm.state_object_for_name(aasm.current_state)

View file

@ -1,6 +1,8 @@
module AASM module AASM
class Base class Base
attr_reader :state_machine
def initialize(klass, options={}, &block) def initialize(klass, options={}, &block)
@klass = klass @klass = klass
@state_machine = AASM::StateMachine[@klass] @state_machine = AASM::StateMachine[@klass]
@ -80,7 +82,7 @@ module AASM
end end
def events def events
@state_machine.events @state_machine.events.values
end end
def states_for_select def states_for_select
@ -89,9 +91,9 @@ module AASM
def from_states_for_state(state, options={}) def from_states_for_state(state, options={})
if options[:transition] 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 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
end end

View file

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

View file

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