Module: ActiveRecord::Transactions
- Extended by:
- ActiveSupport::Concern
- Included in:
- Base
- Defined in:
- activerecord/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
-
#_last_transaction_return_status ⇒ Object
:nodoc:.
-
#_new_record_before_last_commit ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#before_committed! ⇒ Object
:nodoc:.
-
#committed!(should_run_callbacks: true) ⇒ Object
Call the #after_commit callbacks.
-
#destroy ⇒ Object
:nodoc:.
-
#rolledback!(force_restore_state: false, should_run_callbacks: true) ⇒ Object
Call the #after_rollback callbacks.
-
#save ⇒ Object
:nodoc:.
-
#save! ⇒ Object
:nodoc:.
-
#touch ⇒ Object
:nodoc:.
-
#transaction(**options, &block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
-
#trigger_transactional_callbacks? ⇒ Boolean
:nodoc:.
-
#with_transaction_returning_status ⇒ Object
Executes a block within a transaction and captures its return value as a status flag.
Methods included from ActiveSupport::Concern
append_features, class_methods, extended, included, prepend_features, prepended
Instance Attribute Details
#_last_transaction_return_status ⇒ Object
:nodoc:
15 16 17 |
# File 'activerecord/lib/active_record/transactions.rb', line 15 def _last_transaction_return_status @_last_transaction_return_status end |
#_new_record_before_last_commit ⇒ Object
:nodoc:
15 16 17 |
# File 'activerecord/lib/active_record/transactions.rb', line 15 def _new_record_before_last_commit @_new_record_before_last_commit end |
Instance Method Details
#before_committed! ⇒ Object
:nodoc:
391 392 393 |
# File 'activerecord/lib/active_record/transactions.rb', line 391 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.
399 400 401 402 403 404 405 406 407 |
# File 'activerecord/lib/active_record/transactions.rb', line 399 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 |
#destroy ⇒ Object
:nodoc:
375 376 377 |
# File 'activerecord/lib/active_record/transactions.rb', line 375 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.
411 412 413 414 415 416 417 418 419 |
# File 'activerecord/lib/active_record/transactions.rb', line 411 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 |
#save ⇒ Object
:nodoc:
379 380 381 |
# File 'activerecord/lib/active_record/transactions.rb', line 379 def save(**) # :nodoc: with_transaction_returning_status { super } end |
#save! ⇒ Object
:nodoc:
383 384 385 |
# File 'activerecord/lib/active_record/transactions.rb', line 383 def save!(**) # :nodoc: with_transaction_returning_status { super } end |
#touch ⇒ Object
:nodoc:
387 388 389 |
# File 'activerecord/lib/active_record/transactions.rb', line 387 def touch(*, **) # :nodoc: with_transaction_returning_status { super } end |
#transaction(**options, &block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
371 372 373 |
# File 'activerecord/lib/active_record/transactions.rb', line 371 def transaction(**, &block) self.class.transaction(**, &block) end |
#trigger_transactional_callbacks? ⇒ Boolean
:nodoc:
446 447 448 449 |
# File 'activerecord/lib/active_record/transactions.rb', line 446 def trigger_transactional_callbacks? # :nodoc: (@_new_record_before_last_commit || _trigger_update_callback) && persisted? || _trigger_destroy_callback && destroyed? end |
#with_transaction_returning_status ⇒ Object
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.
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'activerecord/lib/active_record/transactions.rb', line 427 def with_transaction_returning_status # :nodoc: self.class.with_connection do |connection| connection.pool.with_pool_transaction_isolation_level(ActiveRecord.default_transaction_isolation_level, connection.transaction_open?) do 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 @_last_transaction_return_status = status status end end end |