Module: ActiveRecord::Transactions
Overview
See ActiveRecord::Transactions::ClassMethods for documentation.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- ACTIONS =
:nodoc:
[:create, :destroy, :update]
- CALLBACK_WARN_MESSAGE =
:nodoc:
"Currently, Active Record suppresses errors raised " \ "within `after_rollback`/`after_commit` callbacks and only print them to " \ "the logs. In the next version, these errors will no longer be suppressed. " \ "Instead, the errors will propagate normally just like in other Active " \ "Record callbacks.\n" \ "\n" \ "You can opt into the new behavior and remove this warning by setting:\n" \ "\n" \ " config.active_record.raise_in_transactional_callbacks = true\n\n"
Instance Method Summary collapse
-
#add_to_transaction ⇒ Object
Add the record to the current transaction so that the
after_rollback
andafter_commit
callbacks can be called. -
#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.
330 331 332 333 334 335 336 337 338 |
# File 'lib/active_record/transactions.rb', line 330 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 |
#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.
313 314 315 316 317 |
# File 'lib/active_record/transactions.rb', line 313 def committed!(should_run_callbacks = true) #:nodoc: _run_commit_callbacks if should_run_callbacks && destroyed? || persisted? ensure force_clear_transaction_record_state end |
#destroy ⇒ Object
:nodoc:
280 281 282 |
# File 'lib/active_record/transactions.rb', line 280 def destroy #:nodoc: with_transaction_returning_status { super } end |
#rollback_active_record_state! ⇒ Object
Reset id and @new_record if the transaction rolls back.
299 300 301 302 303 304 305 306 307 |
# File 'lib/active_record/transactions.rb', line 299 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.
321 322 323 324 325 326 |
# File 'lib/active_record/transactions.rb', line 321 def rolledback!(force_restore_state = false, should_run_callbacks = true) #:nodoc: _run_rollback_callbacks if should_run_callbacks ensure restore_transaction_record_state(force_restore_state) clear_transaction_record_state end |
#save ⇒ Object
:nodoc:
284 285 286 287 288 |
# File 'lib/active_record/transactions.rb', line 284 def save(*) #:nodoc: rollback_active_record_state! do with_transaction_returning_status { super } end end |
#save! ⇒ Object
:nodoc:
290 291 292 |
# File 'lib/active_record/transactions.rb', line 290 def save!(*) #:nodoc: with_transaction_returning_status { super } end |
#touch ⇒ Object
:nodoc:
294 295 296 |
# File 'lib/active_record/transactions.rb', line 294 def touch(*) #:nodoc: with_transaction_returning_status { super } end |
#transaction(options = {}, &block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
276 277 278 |
# File 'lib/active_record/transactions.rb', line 276 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.
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/active_record/transactions.rb', line 346 def with_transaction_returning_status status = nil self.class.transaction do add_to_transaction begin status = yield rescue ActiveRecord::Rollback clear_transaction_record_state status = nil end raise ActiveRecord::Rollback unless status end status ensure if @transaction_state && @transaction_state.committed? clear_transaction_record_state end end |