Module: ActiveRecord::Transactions

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

Overview

See ActiveRecord::Transactions::ClassMethods for documentation.

Defined Under Namespace

Modules: ClassMethods Classes: TransactionError

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

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.



279
280
281
282
283
# File 'activerecord/lib/active_record/transactions.rb', line 279

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



262
263
264
265
266
# File 'activerecord/lib/active_record/transactions.rb', line 262

def committed! #:nodoc:
  run_callbacks :commit
ensure
  clear_transaction_record_state
end

#destroyObject

:nodoc:



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

def destroy #:nodoc:
  with_transaction_returning_status { super }
end

#rollback_active_record_state!Object

Reset id and @new_record if the transaction rolls back.



250
251
252
253
254
255
256
257
258
259
# File 'activerecord/lib/active_record/transactions.rb', line 250

def rollback_active_record_state!
  remember_transaction_record_state
  yield
rescue Exception
  IdentityMap.remove(self) if IdentityMap.enabled?
  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.



270
271
272
273
274
275
# File 'activerecord/lib/active_record/transactions.rb', line 270

def rolledback!(force_restore_state = false) #:nodoc:
  run_callbacks :rollback
ensure
  IdentityMap.remove(self) if IdentityMap.enabled?
  restore_transaction_record_state(force_restore_state)
end

#saveObject

:nodoc:



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

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

#save!Object

:nodoc:



245
246
247
# File 'activerecord/lib/active_record/transactions.rb', line 245

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

#transaction(options = {}, &block) ⇒ Object

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



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

def transaction(options = {}, &block)
  self.class.transaction(options, &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.



291
292
293
294
295
296
297
298
299
# File 'activerecord/lib/active_record/transactions.rb', line 291

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