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 273
273: def add_to_transaction
274: if self.class.connection.add_transaction_record(self)
275: remember_transaction_record_state
276: end
277: end
Reset id and @persisted if the transaction rolls back.
# File lib/active_record/transactions.rb, line 246
246: def rollback_active_record_state!
247: remember_transaction_record_state
248: yield
249: rescue Exception
250: restore_transaction_record_state
251: raise
252: ensure
253: clear_transaction_record_state
254: end
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
# File lib/active_record/transactions.rb, line 227
227: def transaction(&block)
228: self.class.transaction(&block)
229: 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 285
285: def with_transaction_returning_status
286: status = nil
287: self.class.transaction do
288: add_to_transaction
289: status = yield
290: raise ActiveRecord::Rollback unless status
291: end
292: status
293: end
Clear the new record state and id of a record.
# File lib/active_record/transactions.rb, line 311
311: def clear_transaction_record_state #:nodoc
312: if defined?(@_start_transaction_state)
313: @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
314: remove_instance_variable(:@_start_transaction_state) if @_start_transaction_state[:level] < 1
315: end
316: 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 298
298: def remember_transaction_record_state #:nodoc
299: @_start_transaction_state ||= {}
300: unless @_start_transaction_state.include?(:persisted)
301: @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
302: @_start_transaction_state[:persisted] = @persisted
303: end
304: unless @_start_transaction_state.include?(:destroyed)
305: @_start_transaction_state[:destroyed] = @destroyed
306: end
307: @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
308: 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 319
319: def restore_transaction_record_state(force = false) #:nodoc
320: if defined?(@_start_transaction_state)
321: @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
322: if @_start_transaction_state[:level] < 1
323: restore_state = remove_instance_variable(:@_start_transaction_state)
324: if restore_state
325: @attributes = @attributes.dup if @attributes.frozen?
326: @persisted = restore_state[:persisted]
327: @destroyed = restore_state[:destroyed]
328: if restore_state[:id]
329: self.id = restore_state[:id]
330: else
331: @attributes.delete(self.class.primary_key)
332: @attributes_cache.delete(self.class.primary_key)
333: end
334: end
335: end
336: end
337: end
Determine if a transaction included an action for :create, :update, or :destroy. Used in filtering callbacks.
# File lib/active_record/transactions.rb, line 345
345: def transaction_include_action?(action) #:nodoc
346: case action
347: when :create
348: transaction_record_state(:new_record) || !transaction_record_state(:persisted)
349: when :destroy
350: destroyed?
351: when :update
352: !(transaction_record_state(:new_record) || !transaction_record_state(:persisted) || destroyed?)
353: end
354: 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 340
340: def transaction_record_state(state) #:nodoc
341: @_start_transaction_state[state] if defined?(@_start_transaction_state)
342: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.