lib/aasm.rb in runcoderun-aasm-2.0.2.2 vs lib/aasm.rb in runcoderun-aasm-2.0.2.3
- old
+ new
@@ -2,29 +2,32 @@
require File.join(File.dirname(__FILE__), 'state')
require File.join(File.dirname(__FILE__), 'state_machine')
require File.join(File.dirname(__FILE__), 'persistence')
module AASM
+ def self.Version
+ '2.0.2.3'
+ end
+
class InvalidTransition < RuntimeError
end
def self.included(base) #:nodoc:
# TODO - need to ensure that a machine is being created because
# AASM was either included or arrived at via inheritance. It
# cannot be both.
base.extend AASM::ClassMethods
AASM::Persistence.set_persistence(base)
AASM::StateMachine[base] = AASM::StateMachine.new('')
-
- base.class_eval do
- def base.inherited(klass)
- AASM::StateMachine[klass] = AASM::StateMachine[self].dup
- end
- end
end
module ClassMethods
+ def inherited(klass)
+ AASM::StateMachine[klass] = AASM::StateMachine[self].dup
+ super
+ end
+
def aasm_initial_state(set_state=nil)
if set_state
AASM::StateMachine[self].initial_state = set_state
else
AASM::StateMachine[self].initial_state
@@ -94,15 +97,18 @@
events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
events.map {|event| event.name}
end
private
- def aasm_current_state_with_persistence=(state)
+ def set_aasm_current_state_with_persistence(state)
+ save_success = true
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
- aasm_write_state(state)
+ save_success = aasm_write_state(state)
end
- self.aasm_current_state = state
+ self.aasm_current_state = state if save_success
+
+ save_success
end
def aasm_current_state=(state)
if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
aasm_write_state_without_persistence(state)
@@ -118,24 +124,27 @@
aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
new_state = self.class.aasm_events[name].fire(self, *args)
unless new_state.nil?
+ aasm_state_object_for_state(new_state).call_action(:enter, self)
+
+ persist_successful = true
if persist
- self.aasm_current_state_with_persistence = new_state
+ persist_successful = set_aasm_current_state_with_persistence(new_state)
+ self.class.aasm_events[name].execute_success_callback(self) if persist_successful
else
self.aasm_current_state = new_state
+ self.class.aasm_events[name].execute_success_callback(self)
end
-
- self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
-
- aasm_state_object_for_state(new_state).call_action(:enter, self)
-
- if self.respond_to?(:aasm_event_fired)
- self.aasm_event_fired(self.aasm_current_state, new_state)
+
+ if persist_successful
+ self.aasm_event_fired(self.aasm_current_state, new_state) if self.respond_to?(:aasm_event_fired)
+ else
+ self.aasm_event_failed(name) if self.respond_to?(:aasm_event_failed)
end
- true
+ persist_successful
else
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name)
end