Module: ActiveRecord::Transactions
Overview
See ActiveRecord::Transactions::ClassMethods for documentation.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- ACTIONS =
:nodoc:
[:create, :destroy, :update]
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
methodwithin a transaction and captures its return value as a status flag.
Instance Method Details
#before_committed! ⇒ Object
:nodoc:
326 327 328 329 |
# File 'lib/active_record/transactions.rb', line 326 def before_committed! # :nodoc: _run_before_commit_without_transaction_enrollment_callbacks _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.
335 336 337 338 339 340 341 342 343 344 |
# File 'lib/active_record/transactions.rb', line 335 def committed!(should_run_callbacks: true) #:nodoc: if should_run_callbacks @_committed_already_called = true _run_commit_without_transaction_enrollment_callbacks _run_commit_callbacks end ensure @_committed_already_called = false force_clear_transaction_record_state end |
#destroy ⇒ Object
:nodoc:
310 311 312 |
# File 'lib/active_record/transactions.rb', line 310 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.
348 349 350 351 352 353 354 355 356 |
# File 'lib/active_record/transactions.rb', line 348 def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc: if should_run_callbacks _run_rollback_callbacks _run_rollback_without_transaction_enrollment_callbacks end ensure restore_transaction_record_state(force_restore_state) clear_transaction_record_state end |
#save ⇒ Object
:nodoc:
314 315 316 |
# File 'lib/active_record/transactions.rb', line 314 def save(*) #:nodoc: with_transaction_returning_status { super } end |
#save! ⇒ Object
:nodoc:
318 319 320 |
# File 'lib/active_record/transactions.rb', line 318 def save!(*) #:nodoc: with_transaction_returning_status { super } end |
#touch ⇒ Object
:nodoc:
322 323 324 |
# File 'lib/active_record/transactions.rb', line 322 def touch(*) #:nodoc: with_transaction_returning_status { super } end |
#transaction(options = {}, &block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
306 307 308 |
# File 'lib/active_record/transactions.rb', line 306 def transaction( = {}, &block) self.class.transaction(, &block) end |
#trigger_transactional_callbacks? ⇒ Boolean
:nodoc:
381 382 383 384 |
# File 'lib/active_record/transactions.rb', line 381 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 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.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/active_record/transactions.rb', line 364 def with_transaction_returning_status status = nil self.class.transaction do if has_transactional_callbacks? add_to_transaction else sync_with_transaction_state if @transaction_state&.finalized? @transaction_state = self.class.connection.transaction_state end remember_transaction_record_state status = yield raise ActiveRecord::Rollback unless status end status end |