aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/transactions.rb
diff options
context:
space:
mode:
authorArthur Nogueira Neves <arthurnn@gmail.com>2015-02-24 19:25:11 -0500
committerArthur Nogueira Neves <arthurnn@gmail.com>2015-02-24 19:25:11 -0500
commit27ce16aa9b348f32d866a697e1dd6cb061ce0ed2 (patch)
treeb0676474e5a8018f31233de49a6415dd61edfadf /activerecord/lib/active_record/transactions.rb
parentf4bee7ecbfd664a9268ae8a6cdf3deabf0ed74e4 (diff)
parent45755135fe9669d1ba3c8567c517559a6a291304 (diff)
downloadrails-27ce16aa9b348f32d866a697e1dd6cb061ce0ed2.tar.gz
rails-27ce16aa9b348f32d866a697e1dd6cb061ce0ed2.tar.bz2
rails-27ce16aa9b348f32d866a697e1dd6cb061ce0ed2.zip
Merge pull request #18936 from arthurnn/txn_callbacks
Spike on new transaction callbacks
Diffstat (limited to 'activerecord/lib/active_record/transactions.rb')
-rw-r--r--activerecord/lib/active_record/transactions.rb39
1 files changed, 37 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index dd405c7796..598defce50 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -7,6 +7,10 @@ module ActiveRecord
included do
define_callbacks :commit, :rollback,
+ :before_commit,
+ :before_commit_without_transaction_enrollment,
+ :commit_without_transaction_enrollment,
+ :rollback_without_transaction_enrollment,
scope: [:kind, :name]
end
@@ -206,6 +210,11 @@ module ActiveRecord
connection.transaction(options, &block)
end
+ def before_commit(*args, &block) # :nodoc:
+ set_options_for_callbacks!(args)
+ set_callback(:before_commit, :before, *args, &block)
+ end
+
# This callback is called after a record has been created, updated, or destroyed.
#
# You can specify that the callback should only be fired by a certain action with
@@ -233,6 +242,21 @@ module ActiveRecord
set_callback(:rollback, :after, *args, &block)
end
+ def before_commit_without_transaction_enrollment(*args, &block) # :nodoc:
+ set_options_for_callbacks!(args)
+ set_callback(:before_commit_without_transaction_enrollment, :before, *args, &block)
+ end
+
+ def after_commit_without_transaction_enrollment(*args, &block) # :nodoc:
+ set_options_for_callbacks!(args)
+ set_callback(:commit_without_transaction_enrollment, :after, *args, &block)
+ end
+
+ def after_rollback_without_transaction_enrollment(*args, &block) # :nodoc:
+ set_options_for_callbacks!(args)
+ set_callback(:rollback_without_transaction_enrollment, :after, *args, &block)
+ end
+
def raise_in_transactional_callbacks
ActiveSupport::Deprecation.warn('ActiveRecord::Base.raise_in_transactional_callbacks is deprecated and will be removed without replacement.')
true
@@ -296,12 +320,20 @@ module ActiveRecord
clear_transaction_record_state
end
+ def before_committed! # :nodoc:
+ _run_before_commit_without_transaction_enrollment_callbacks
+ _run_before_commit_callbacks
+ end
+
# 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.
def committed!(should_run_callbacks: true) #:nodoc:
- _run_commit_callbacks if should_run_callbacks && destroyed? || persisted?
+ if should_run_callbacks && destroyed? || persisted?
+ _run_commit_without_transaction_enrollment_callbacks
+ _run_commit_callbacks
+ end
ensure
force_clear_transaction_record_state
end
@@ -309,7 +341,10 @@ module ActiveRecord
# 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.
def rolledback!(force_restore_state: false, should_run_callbacks: true) #:nodoc:
- _run_rollback_callbacks if should_run_callbacks
+ if should_run_callbacks
+ _run_rollback_without_transaction_enrollment_callbacks
+ _run_rollback_callbacks
+ end
ensure
restore_transaction_record_state(force_restore_state)
clear_transaction_record_state