See ActiveRecord::Transactions::ClassMethods for documentation.
Add the record to the current transaction so that the :after_rollback and :after_commit callbacks can be called.
# File lib/active_record/transactions.rb, line 276 def add_to_transaction if self.class.connection.add_transaction_record(self) remember_transaction_record_state end end
Reset id and @new_record if the transaction rolls back.
# File lib/active_record/transactions.rb, line 249 def rollback_active_record_state! remember_transaction_record_state yield rescue Exception restore_transaction_record_state raise ensure clear_transaction_record_state end
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
# File lib/active_record/transactions.rb, line 230 def transaction(&block) self.class.transaction(&block) end
Executes method within a transaction and captures its return value as a status flag. If the status is true the transaction is committed, otherwise a ROLLBACK is issued. In any case the status flag is returned.
This method is available within the context of an ActiveRecord::Base instance.
# File lib/active_record/transactions.rb, line 288 def with_transaction_returning_status status = nil self.class.transaction do add_to_transaction status = yield raise ActiveRecord::Rollback unless status end status end
Clear the new record state and id of a record.
# File lib/active_record/transactions.rb, line 314 def clear_transaction_record_state #:nodoc if defined?(@_start_transaction_state) @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1 remove_instance_variable(:@_start_transaction_state) if @_start_transaction_state[:level] < 1 end end
Save the new record state and id of a record so it can be restored later if a transaction fails.
# File lib/active_record/transactions.rb, line 301 def remember_transaction_record_state #:nodoc @_start_transaction_state ||= {} unless @_start_transaction_state.include?(:new_record) @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key) @_start_transaction_state[:new_record] = @new_record end unless @_start_transaction_state.include?(:destroyed) @_start_transaction_state[:destroyed] = @destroyed end @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1 end
Restore the new record state and id of a record that was previously saved by a call to save_record_state.
# File lib/active_record/transactions.rb, line 322 def restore_transaction_record_state(force = false) #:nodoc if defined?(@_start_transaction_state) @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1 if @_start_transaction_state[:level] < 1 restore_state = remove_instance_variable(:@_start_transaction_state) if restore_state @attributes = @attributes.dup if @attributes.frozen? @new_record = restore_state[:new_record] @destroyed = restore_state[:destroyed] if restore_state[:id] self.id = restore_state[:id] else @attributes.delete(self.class.primary_key) @attributes_cache.delete(self.class.primary_key) end end end end end
Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
# File lib/active_record/transactions.rb, line 348 def transaction_include_action?(action) #:nodoc case action when :create transaction_record_state(:new_record) when :destroy destroyed? when :update !(transaction_record_state(:new_record) || destroyed?) end end
Determine if a record was created or destroyed in a transaction. State should be one of :new_record or :destroyed.
# File lib/active_record/transactions.rb, line 343 def transaction_record_state(state) #:nodoc @_start_transaction_state[state] if defined?(@_start_transaction_state) end
Generated with the Darkfish Rdoc Generator 2.