Module: ActiveRecord::Transactions

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record/transactions.rb

Overview

See ActiveRecord::Transactions::ClassMethods for documentation.

Defined Under Namespace

Modules: ClassMethods Classes: TransactionError

Instance Method Summary collapse

Instance Method Details

#add_to_transactionObject

Add the record to the current transaction so that the :after_rollback and :after_commit callbacks can be called.



273
274
275
276
277
# File 'lib/active_record/transactions.rb', line 273

def add_to_transaction
  if self.class.connection.add_transaction_record(self)
    remember_transaction_record_state
  end
end

#committed!Object

Call the after_commit callbacks



257
258
259
260
261
# File 'lib/active_record/transactions.rb', line 257

def committed! #:nodoc:
  _run_commit_callbacks
ensure
  clear_transaction_record_state
end

#destroyObject

:nodoc:



231
232
233
# File 'lib/active_record/transactions.rb', line 231

def destroy #:nodoc:
  with_transaction_returning_status { super }
end

#rollback_active_record_state!Object

Reset id and @persisted if the transaction rolls back.



246
247
248
249
250
251
252
253
254
# File 'lib/active_record/transactions.rb', line 246

def rollback_active_record_state!
  remember_transaction_record_state
  yield
rescue Exception
  restore_transaction_record_state
  raise
ensure
  clear_transaction_record_state
end

#rolledback!(force_restore_state = false) ⇒ Object

Call the after rollback callbacks. The restore_state argument indicates if the record state should be rolled back to the beginning or just to the last savepoint.



265
266
267
268
269
# File 'lib/active_record/transactions.rb', line 265

def rolledback!(force_restore_state = false) #:nodoc:
  _run_rollback_callbacks
ensure
  restore_transaction_record_state(force_restore_state)
end

#saveObject

:nodoc:



235
236
237
238
239
# File 'lib/active_record/transactions.rb', line 235

def save(*) #:nodoc:
  rollback_active_record_state! do
    with_transaction_returning_status { super }
  end
end

#save!Object

:nodoc:



241
242
243
# File 'lib/active_record/transactions.rb', line 241

def save!(*) #:nodoc:
  with_transaction_returning_status { super }
end

#transaction(&block) ⇒ Object

See ActiveRecord::Transactions::ClassMethods for detailed documentation.



227
228
229
# File 'lib/active_record/transactions.rb', line 227

def transaction(&block)
  self.class.transaction(&block)
end

#with_transaction_returning_statusObject

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.



285
286
287
288
289
290
291
292
293
# File 'lib/active_record/transactions.rb', line 285

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    status = yield
    raise ActiveRecord::Rollback unless status
  end
  status
end