lib/aasm/aasm.rb in aasm-3.0.17 vs lib/aasm/aasm.rb in aasm-3.0.18
- old
+ new
@@ -81,11 +81,14 @@
@aasm ||= AASM::InstanceBase.new(self)
end
# may be overwritten by persistence mixins
def aasm_read_state
- aasm.enter_initial_state
+ # all the following lines behave like @current_state ||= aasm.enter_initial_state
+ current = aasm.instance_variable_get("@current_state")
+ return current if current
+ aasm.instance_variable_set("@current_state", aasm.enter_initial_state)
end
# may be overwritten by persistence mixins
def aasm_write_state(new_state)
true
@@ -132,63 +135,69 @@
aasm.human_state
end
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)
+ fired(event, old_state, new_state_name, options)
+ else
+ failed(event_name, old_state)
+ end
+ rescue StandardError => e
+ event.fire_callbacks(:error, self, e) || raise(e)
+ end
+ end
- # new before_ callbacks
- old_state.fire_callbacks(:before_exit, self)
- new_state.fire_callbacks(:before_enter, self)
+ def fired(event, old_state, new_state_name, options)
+ persist = options[:persist]
- new_state.fire_callbacks(:enter, self)
+ new_state = aasm.state_object_for_name(new_state_name)
- 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
+ # new before_ callbacks
+ old_state.fire_callbacks(:before_exit, self)
+ new_state.fire_callbacks(:before_enter, self)
- if persist_successful
- old_state.fire_callbacks(:after_exit, self)
- new_state.fire_callbacks(:after_enter, self)
- event.fire_callbacks(:after, self)
+ new_state.fire_callbacks(:enter, 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 = 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
- persist_successful
+ if persist_successful
+ old_state.fire_callbacks(:after_exit, self)
+ new_state.fire_callbacks(:after_enter, self)
+ event.fire_callbacks(:after, self)
- else
- if self.respond_to?(:aasm_event_failed)
- self.aasm_event_failed(name, old_state.name)
- end
+ 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
- if AASM::StateMachine[self.class].config.whiny_transitions
- raise AASM::InvalidTransition, "Event '#{event.name}' cannot transition from '#{aasm.current_state}'"
- else
- false
- end
- end
- rescue StandardError => e
- event.fire_callbacks(:error, self, e) || raise(e)
+ 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