Module: ActiveRecord::Transactions

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

Overview

See ActiveRecord::Transactions::ClassMethods for documentation.

Defined Under Namespace

Modules: ClassMethods Classes: TransactionError

Constant Summary collapse

ACTIONS =
[:create, :destroy, :update]

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.



313
314
315
316
317
# File 'lib/active_record/transactions.rb', line 313

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.

Ensure that it is not called if the object was never persisted (failed create), but call it after the commit of a destroyed object.



297
298
299
300
301
# File 'lib/active_record/transactions.rb', line 297

def committed! #:nodoc:
  run_callbacks :commit if destroyed? || persisted?
ensure
  @_start_transaction_state.clear
end

#destroyObject

:nodoc:



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

def destroy #:nodoc:
  with_transaction_returning_status { super }
end

#rollback_active_record_state!Object

Reset id and @new_record if the transaction rolls back.



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

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 force_restore_state argument indicates if the record state should be rolled back to the beginning or just to the last savepoint.



305
306
307
308
309
# File 'lib/active_record/transactions.rb', line 305

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

#saveObject

:nodoc:



268
269
270
271
272
# File 'lib/active_record/transactions.rb', line 268

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

#save!Object

:nodoc:



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

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

#touchObject

:nodoc:



278
279
280
# File 'lib/active_record/transactions.rb', line 278

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

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

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



260
261
262
# File 'lib/active_record/transactions.rb', line 260

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.



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/active_record/transactions.rb', line 325

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    begin
      status = yield
    rescue ActiveRecord::Rollback
      @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
      status = nil
    end

    raise ActiveRecord::Rollback unless status
  end
  status
end