Added new callbacks with slightly more obvious semantics:
state - before_exit state - after_exit state - before_enter state - after_enter event - before event - after These get called as implied just before or after the state has been updated. No ordering within the before or after groups is defined.
This commit is contained in:
parent
6685c773ca
commit
06eacd00d2
30
lib/aasm.rb
30
lib/aasm.rb
|
@ -126,23 +126,37 @@ module AASM
|
|||
end
|
||||
|
||||
def aasm_fire_event(name, persist, *args)
|
||||
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
|
||||
old_state = aasm_state_object_for_state(aasm_current_state)
|
||||
event = self.class.aasm_events[name]
|
||||
|
||||
new_state = self.class.aasm_events[name].fire(self, *args)
|
||||
old_state.call_action(:exit, self)
|
||||
|
||||
new_state_name = event.fire(self, *args)
|
||||
|
||||
unless new_state_name.nil?
|
||||
new_state = aasm_state_object_for_state(new_state_name)
|
||||
|
||||
unless new_state.nil?
|
||||
aasm_state_object_for_state(new_state).call_action(:enter, self)
|
||||
# new before_ callbacks
|
||||
old_state.call_action(:before_exit, self)
|
||||
new_state.call_action(:before_enter, self)
|
||||
event.call_action(:before, self)
|
||||
|
||||
new_state.call_action(:enter, self)
|
||||
|
||||
persist_successful = true
|
||||
if persist
|
||||
persist_successful = set_aasm_current_state_with_persistence(new_state)
|
||||
self.class.aasm_events[name].execute_success_callback(self) if persist_successful
|
||||
persist_successful = set_aasm_current_state_with_persistence(new_state_name)
|
||||
event.execute_success_callback(self) if persist_successful
|
||||
else
|
||||
self.aasm_current_state = new_state
|
||||
self.aasm_current_state = new_state_name
|
||||
end
|
||||
|
||||
if persist_successful
|
||||
self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
|
||||
old_state.call_action(:after_exit, self)
|
||||
new_state.call_action(:after_enter, self)
|
||||
event.call_action(:after, self)
|
||||
|
||||
self.aasm_event_fired(self.aasm_current_state, new_state_name) if self.respond_to?(:aasm_event_fired)
|
||||
else
|
||||
self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
|
||||
end
|
||||
|
|
11
lib/event.rb
11
lib/event.rb
|
@ -9,6 +9,7 @@ module AASM
|
|||
@name = name
|
||||
@success = options[:success]
|
||||
@transitions = []
|
||||
@options = options
|
||||
instance_eval(&block) if block
|
||||
end
|
||||
|
||||
|
@ -43,6 +44,16 @@ module AASM
|
|||
end
|
||||
end
|
||||
|
||||
def call_action(action, record)
|
||||
action = @options[action]
|
||||
case action
|
||||
when Symbol, String
|
||||
record.send(action)
|
||||
when Proc
|
||||
action.call(record)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def transitions(trans_opts)
|
||||
Array(trans_opts[:from]).each do |s|
|
||||
|
|
Loading…
Reference in New Issue