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
-
#add_to_transaction ⇒ Object
Add the record to the current transaction so that the #after_rollback and #after_commit callbacks can be called.
-
#before_committed! ⇒ Object
:nodoc:.
-
#committed!(should_run_callbacks: true) ⇒ Object
Call the #after_commit callbacks.
-
#destroy ⇒ Object
:nodoc:.
-
#rollback_active_record_state! ⇒ Object
Reset id and @new_record if the transaction rolls back.
-
#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.
-
#with_transaction_returning_status ⇒ Object
Executes
method
within a transaction and captures its return value as a status flag.
Instance Method Details
#add_to_transaction ⇒ Object
Add the record to the current transaction so that the #after_rollback and #after_commit callbacks can be called.
367 368 369 370 371 372 373 374 375 |
# File 'lib/active_record/transactions.rb', line 367 def add_to_transaction if has_transactional_callbacks? self.class.connection.add_transaction_record(self) else sync_with_transaction_state set_transaction_state(self.class.connection.transaction_state) end remember_transaction_record_state end |
#before_committed! ⇒ Object
:nodoc:
333 334 335 336 |
# File 'lib/active_record/transactions.rb', line 333 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.
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/active_record/transactions.rb', line 342 def committed!(should_run_callbacks: true) #:nodoc: force_clear_transaction_record_state if should_run_callbacks && (destroyed? || persisted?) @_committed_already_called = true _run_commit_without_transaction_enrollment_callbacks _run_commit_callbacks end ensure @_committed_already_called = false end |
#destroy ⇒ Object
:nodoc:
304 305 306 |
# File 'lib/active_record/transactions.rb', line 304 def destroy #:nodoc: with_transaction_returning_status { super } end |
#rollback_active_record_state! ⇒ Object
Reset id and @new_record if the transaction rolls back.
323 324 325 326 327 328 329 330 331 |
# File 'lib/active_record/transactions.rb', line 323 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, 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.
355 356 357 358 359 360 361 362 363 |
# File 'lib/active_record/transactions.rb', line 355 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:
308 309 310 311 312 |
# File 'lib/active_record/transactions.rb', line 308 def save(*) #:nodoc: rollback_active_record_state! do with_transaction_returning_status { super } end end |
#save! ⇒ Object
:nodoc:
314 315 316 |
# File 'lib/active_record/transactions.rb', line 314 def save!(*) #:nodoc: with_transaction_returning_status { super } end |
#touch ⇒ Object
:nodoc:
318 319 320 |
# File 'lib/active_record/transactions.rb', line 318 def touch(*) #:nodoc: with_transaction_returning_status { super } end |
#transaction(options = {}, &block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
300 301 302 |
# File 'lib/active_record/transactions.rb', line 300 def transaction( = {}, &block) self.class.transaction(, &block) 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.
383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/active_record/transactions.rb', line 383 def with_transaction_returning_status status = nil self.class.transaction do add_to_transaction status = yield raise ActiveRecord::Rollback unless status end status ensure if @transaction_state && @transaction_state.committed? clear_transaction_record_state end end |