mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
refactored to reduce complexity
This commit is contained in:
parent
87b13ca300
commit
47e446f501
1 changed files with 49 additions and 43 deletions
|
@ -134,61 +134,67 @@ module AASM
|
|||
|
||||
private
|
||||
|
||||
def aasm_fire_event(name, options, *args)
|
||||
persist = options[:persist]
|
||||
|
||||
event = self.class.aasm_events[name]
|
||||
def aasm_fire_event(event_name, options, *args)
|
||||
event = self.class.aasm_events[event_name]
|
||||
begin
|
||||
old_state = aasm.state_object_for_name(aasm.current_state)
|
||||
|
||||
|
||||
old_state.fire_callbacks(:exit, self)
|
||||
|
||||
# new event before callback
|
||||
event.fire_callbacks(:before, self)
|
||||
|
||||
if new_state_name = event.fire(self, *args)
|
||||
new_state = aasm.state_object_for_name(new_state_name)
|
||||
|
||||
# new before_ callbacks
|
||||
old_state.fire_callbacks(:before_exit, self)
|
||||
new_state.fire_callbacks(:before_enter, self)
|
||||
|
||||
new_state.fire_callbacks(:enter, self)
|
||||
|
||||
persist_successful = true
|
||||
if persist
|
||||
persist_successful = aasm.set_current_state_with_persistence(new_state_name)
|
||||
event.fire_callbacks(:success, self) if persist_successful
|
||||
else
|
||||
aasm.current_state = new_state_name
|
||||
end
|
||||
|
||||
if persist_successful
|
||||
old_state.fire_callbacks(:after_exit, self)
|
||||
new_state.fire_callbacks(:after_enter, self)
|
||||
event.fire_callbacks(:after, self)
|
||||
|
||||
self.aasm_event_fired(name, old_state.name, aasm.current_state) if self.respond_to?(:aasm_event_fired)
|
||||
else
|
||||
self.aasm_event_failed(name, old_state.name) if self.respond_to?(:aasm_event_failed)
|
||||
end
|
||||
|
||||
persist_successful
|
||||
|
||||
fired(event, old_state, new_state_name, options)
|
||||
else
|
||||
if self.respond_to?(:aasm_event_failed)
|
||||
self.aasm_event_failed(name, old_state.name)
|
||||
end
|
||||
|
||||
if AASM::StateMachine[self.class].config.whiny_transitions
|
||||
raise AASM::InvalidTransition, "Event '#{event.name}' cannot transition from '#{aasm.current_state}'"
|
||||
else
|
||||
false
|
||||
end
|
||||
failed(event_name, old_state)
|
||||
end
|
||||
rescue StandardError => e
|
||||
event.fire_callbacks(:error, self, e) || raise(e)
|
||||
end
|
||||
end
|
||||
|
||||
def fired(event, old_state, new_state_name, options)
|
||||
persist = options[:persist]
|
||||
|
||||
new_state = aasm.state_object_for_name(new_state_name)
|
||||
|
||||
# new before_ callbacks
|
||||
old_state.fire_callbacks(:before_exit, self)
|
||||
new_state.fire_callbacks(:before_enter, self)
|
||||
|
||||
new_state.fire_callbacks(:enter, self)
|
||||
|
||||
persist_successful = true
|
||||
if persist
|
||||
persist_successful = aasm.set_current_state_with_persistence(new_state_name)
|
||||
event.fire_callbacks(:success, self) if persist_successful
|
||||
else
|
||||
aasm.current_state = new_state_name
|
||||
end
|
||||
|
||||
if persist_successful
|
||||
old_state.fire_callbacks(:after_exit, self)
|
||||
new_state.fire_callbacks(:after_enter, self)
|
||||
event.fire_callbacks(:after, self)
|
||||
|
||||
self.aasm_event_fired(event.name, old_state.name, aasm.current_state) if self.respond_to?(:aasm_event_fired)
|
||||
else
|
||||
self.aasm_event_failed(event.name, old_state.name) if self.respond_to?(:aasm_event_failed)
|
||||
end
|
||||
|
||||
persist_successful
|
||||
end
|
||||
|
||||
def failed(event_name, old_state)
|
||||
if self.respond_to?(:aasm_event_failed)
|
||||
self.aasm_event_failed(event_name, old_state.name)
|
||||
end
|
||||
|
||||
if AASM::StateMachine[self.class].config.whiny_transitions
|
||||
raise AASM::InvalidTransition, "Event '#{event_name}' cannot transition from '#{aasm.current_state}'"
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue