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

Constant Summary collapse

ACTIONS =

:nodoc:

[:create, :destroy, :update]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_new_record_before_last_commitObject

:nodoc:



16
17
18
# File 'lib/active_record/transactions.rb', line 16

def _new_record_before_last_commit
  @_new_record_before_last_commit
end

Instance Method Details

#before_committed!Object

:nodoc:



373
374
375
# File 'lib/active_record/transactions.rb', line 373

def before_committed! # :nodoc:
  _run_before_commit_callbacks
end

#committed!(should_run_callbacks: true) ⇒ 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.



381
382
383
384
385
386
387
388
389
# File 'lib/active_record/transactions.rb', line 381

def committed!(should_run_callbacks: true) # :nodoc:
  @_start_transaction_state = nil
  if should_run_callbacks
    @_committed_already_called = true
    _run_commit_callbacks
  end
ensure
  @_committed_already_called = @_trigger_update_callback = @_trigger_destroy_callback = false
end

#destroyObject

:nodoc:



357
358
359
# File 'lib/active_record/transactions.rb', line 357

def destroy # :nodoc:
  with_transaction_returning_status { super }
end

#rolledback!(force_restore_state: false, should_run_callbacks: true) ⇒ 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.



393
394
395
396
397
398
399
400
401
# File 'lib/active_record/transactions.rb', line 393

def rolledback!(force_restore_state: false, should_run_callbacks: true) # :nodoc:
  if should_run_callbacks
    _run_rollback_callbacks
  end
ensure
  restore_transaction_record_state(force_restore_state)
  clear_transaction_record_state
  @_trigger_update_callback = @_trigger_destroy_callback = false if force_restore_state
end

#saveObject

:nodoc:



361
362
363
# File 'lib/active_record/transactions.rb', line 361

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

#save!Object

:nodoc:



365
366
367
# File 'lib/active_record/transactions.rb', line 365

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

#touchObject

:nodoc:



369
370
371
# File 'lib/active_record/transactions.rb', line 369

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

#transaction(**options, &block) ⇒ Object

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



353
354
355
# File 'lib/active_record/transactions.rb', line 353

def transaction(**options, &block)
  self.class.transaction(**options, &block)
end

#trigger_transactional_callbacks?Boolean

:nodoc:

Returns:

  • (Boolean)


425
426
427
428
# File 'lib/active_record/transactions.rb', line 425

def trigger_transactional_callbacks? # :nodoc:
  (@_new_record_before_last_commit || _trigger_update_callback) && persisted? ||
    _trigger_destroy_callback && destroyed?
end

#with_transaction_returning_statusObject

Executes a block 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.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/active_record/transactions.rb', line 409

def with_transaction_returning_status
  self.class.with_connection do |connection|
    status = nil
    ensure_finalize = !connection.transaction_open?

    connection.transaction do
      add_to_transaction(ensure_finalize || has_transactional_callbacks?)
      remember_transaction_record_state

      status = yield
      raise ActiveRecord::Rollback unless status
    end
    status
  end
end