moduleAASMmodulePersistence# This module adds transactional support for any database that supports it.# This includes rollback capability and rollback/commit callbacks.moduleORM# Writes <tt>state</tt> to the state column and persists it to the database## foo = Foo.find(1)# foo.aasm.current_state # => :opened# foo.close!# foo.aasm.current_state # => :closed# Foo.find(1).aasm.current_state # => :closed## NOTE: intended to be called from an eventdefaasm_write_state(state,name=:default)attribute_name=self.class.aasm(name).attribute_nameold_value=aasm_read_attribute(attribute_name)aasm_write_state_attributestate,namesuccess=ifaasm_skipping_validations(name)aasm_update_column(attribute_name,aasm_raw_attribute_value(state,name))elseaasm_saveendunlesssuccessaasm_rollback(name,old_value)aasm_raise_invalid_recordifaasm_whiny_persistence(name)endsuccessend# Writes <tt>state</tt> to the state field, but does not persist it to the database## foo = Foo.find(1)# foo.aasm.current_state # => :opened# foo.close# foo.aasm.current_state # => :closed# Foo.find(1).aasm.current_state # => :opened# foo.save# foo.aasm.current_state # => :closed# Foo.find(1).aasm.current_state # => :closed## NOTE: intended to be called from an eventdefaasm_write_state_without_persistence(state,name=:default)aasm_write_state_attribute(state,name)endprivate# Save the record and return true if it succeeded/false otherwise.defaasm_saveraise("Define #aasm_save_without_error in the AASM Persistence class.")enddefaasm_raise_invalid_recordraise("Define #aasm_raise_invalid_record in the AASM Persistence class.")end# Update only the column without running validations.defaasm_update_column(attribute_name,value)raise("Define #aasm_update_column in the AASM Persistence class.")enddefaasm_read_attribute(name)raise("Define #aasm_read_attribute the AASM Persistence class.")enddefaasm_write_attribute(name,value)raise("Define #aasm_write_attribute in the AASM Persistence class.")end# Returns true or false if transaction completed successfully.defaasm_transaction(requires_new,requires_lock)raise("Define #aasm_transaction the AASM Persistence class.")enddefaasm_supports_transactions?trueenddefaasm_execute_after_commityieldenddefaasm_write_state_attribute(state,name=:default)aasm_write_attribute(self.class.aasm(name).attribute_name,aasm_raw_attribute_value(state,name))enddefaasm_raw_attribute_value(state,_name=:default)state.to_senddefaasm_rollback(name,old_value)aasm_write_attribute(self.class.aasm(name).attribute_name,old_value)falseenddefaasm_whiny_persistence(state_machine_name)AASM::StateMachineStore.fetch(self.class,true).machine(state_machine_name).config.whiny_persistenceenddefaasm_skipping_validations(state_machine_name)AASM::StateMachineStore.fetch(self.class,true).machine(state_machine_name).config.skip_validation_on_saveenddefuse_transactions?(state_machine_name)AASM::StateMachineStore.fetch(self.class,true).machine(state_machine_name).config.use_transactionsenddefrequires_new?(state_machine_name)AASM::StateMachineStore.fetch(self.class,true).machine(state_machine_name).config.requires_new_transactionenddefrequires_lock?(state_machine_name)AASM::StateMachineStore.fetch(self.class,true).machine(state_machine_name).config.requires_lockend# Returns true if event was fired successfully and transaction completed.defaasm_fire_event(state_machine_name,name,options,*args,&block)returnsuperunlessaasm_supports_transactions?&&options[:persist]event=self.class.aasm(state_machine_name).state_machine.events[name]event.fire_callbacks(:before_transaction,self,*args)event.fire_global_callbacks(:before_all_transactions,self,*args)beginsuccess=ifoptions[:persist]&&use_transactions?(state_machine_name)aasm_transaction(requires_new?(state_machine_name),requires_lock?(state_machine_name))dosuperendelsesuperendifsuccess&&!(event.options.keys&[:after_commit,:after_all_commits]).empty?aasm_execute_after_commitdoevent.fire_callbacks(:after_commit,self,*args)event.fire_global_callbacks(:after_all_commits,self,*args)endendsuccessensureevent.fire_callbacks(:after_transaction,self,*args)event.fire_global_callbacks(:after_all_transactions,self,*args)endendend# Transactionalend# Persistenceend# AASM