module AASM::Persistence::ORM
This module adds transactional support for any database that supports it. This includes rollback capability and rollback/commit callbacks.
Public Instance Methods
aasm_write_state(state, name=:default)
click to toggle source
Writes state
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 event
# File lib/aasm/persistence/orm.rb, line 16 def aasm_write_state(state, name=:default) attribute_name = self.class.aasm(name).attribute_name old_value = aasm_read_attribute(attribute_name) aasm_write_state_attribute state, name success = if aasm_skipping_validations(name) aasm_update_column(attribute_name, aasm_raw_attribute_value(state, name)) else aasm_save end unless success aasm_rollback(name, old_value) aasm_raise_invalid_record if aasm_whiny_persistence(name) end success end
aasm_write_state_without_persistence(state, name=:default)
click to toggle source
Writes state
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 event
# File lib/aasm/persistence/orm.rb, line 47 def aasm_write_state_without_persistence(state, name=:default) aasm_write_state_attribute(state, name) end
Private Instance Methods
aasm_execute_after_commit() { || ... }
click to toggle source
# File lib/aasm/persistence/orm.rb, line 84 def aasm_execute_after_commit yield end
aasm_fire_event(state_machine_name, name, options, *args, &block)
click to toggle source
Returns true if event was fired successfully and transaction completed.
Calls superclass method
# File lib/aasm/persistence/orm.rb, line 122 def aasm_fire_event(state_machine_name, name, options, *args, &block) return super unless aasm_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) begin success = if options[:persist] && use_transactions?(state_machine_name) aasm_transaction(requires_new?(state_machine_name), requires_lock?(state_machine_name)) do super end else super end if success && !(event.options.keys & [:after_commit, :after_all_commits]).empty? aasm_execute_after_commit do event.fire_callbacks(:after_commit, self, *args) event.fire_global_callbacks(:after_all_commits, self, *args) end end success ensure event.fire_callbacks(:after_transaction, self, *args) event.fire_global_callbacks(:after_all_transactions, self, *args) end end
aasm_raise_invalid_record()
click to toggle source
# File lib/aasm/persistence/orm.rb, line 58 def aasm_raise_invalid_record raise("Define #aasm_raise_invalid_record in the AASM Persistence class.") end
aasm_raw_attribute_value(state, _name=:default)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 92 def aasm_raw_attribute_value(state, _name=:default) state.to_s end
aasm_read_attribute(name)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 67 def aasm_read_attribute(name) raise("Define #aasm_read_attribute the AASM Persistence class.") end
aasm_rollback(name, old_value)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 96 def aasm_rollback(name, old_value) aasm_write_attribute(self.class.aasm(name).attribute_name, old_value) false end
aasm_save()
click to toggle source
Save the record and return true if it succeeded/false otherwise.
# File lib/aasm/persistence/orm.rb, line 54 def aasm_save raise("Define #aasm_save_without_error in the AASM Persistence class.") end
aasm_skipping_validations(state_machine_name)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 105 def aasm_skipping_validations(state_machine_name) AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.skip_validation_on_save end
aasm_supports_transactions?()
click to toggle source
# File lib/aasm/persistence/orm.rb, line 80 def aasm_supports_transactions? true end
aasm_transaction(requires_new, requires_lock)
click to toggle source
Returns true or false if transaction completed successfully.
# File lib/aasm/persistence/orm.rb, line 76 def aasm_transaction(requires_new, requires_lock) raise("Define #aasm_transaction the AASM Persistence class.") end
aasm_update_column(attribute_name, value)
click to toggle source
Update only the column without running validations.
# File lib/aasm/persistence/orm.rb, line 63 def aasm_update_column(attribute_name, value) raise("Define #aasm_update_column in the AASM Persistence class.") end
aasm_whiny_persistence(state_machine_name)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 101 def aasm_whiny_persistence(state_machine_name) AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.whiny_persistence end
aasm_write_attribute(name, value)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 71 def aasm_write_attribute(name, value) raise("Define #aasm_write_attribute in the AASM Persistence class.") end
aasm_write_state_attribute(state, name=:default)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 88 def aasm_write_state_attribute(state, name=:default) aasm_write_attribute(self.class.aasm(name).attribute_name, aasm_raw_attribute_value(state, name)) end
requires_lock?(state_machine_name)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 117 def requires_lock?(state_machine_name) AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.requires_lock end
requires_new?(state_machine_name)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 113 def requires_new?(state_machine_name) AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.requires_new_transaction end
use_transactions?(state_machine_name)
click to toggle source
# File lib/aasm/persistence/orm.rb, line 109 def use_transactions?(state_machine_name) AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.use_transactions end