aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/transactions.rb
diff options
context:
space:
mode:
authorArthur Neves <arthurnn@gmail.com>2015-03-01 22:57:14 -0500
committerArthur Neves <arthurnn@gmail.com>2015-03-01 22:57:14 -0500
commit61d12ebb10d9acfffad6ab76a43d3aaedfda5586 (patch)
tree19f01420e4bf53686ed1b0b276c74a5f886a7769 /activerecord/lib/active_record/transactions.rb
parentafd1a870f8046e96010052c8db6b8f40d5c3ef02 (diff)
downloadrails-61d12ebb10d9acfffad6ab76a43d3aaedfda5586.tar.gz
rails-61d12ebb10d9acfffad6ab76a43d3aaedfda5586.tar.bz2
rails-61d12ebb10d9acfffad6ab76a43d3aaedfda5586.zip
Move transaction code to transaction module
Diffstat (limited to 'activerecord/lib/active_record/transactions.rb')
-rw-r--r--activerecord/lib/active_record/transactions.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 598defce50..b620233c3a 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -442,5 +442,46 @@ module ActiveRecord
end
end
end
+
+ def set_transaction_state(state) # :nodoc:
+ @transaction_state = state
+ end
+
+ def has_transactional_callbacks? # :nodoc:
+ !_rollback_callbacks.empty? || !_commit_callbacks.empty? || !_before_commit_callbacks.empty?
+ end
+
+ # Updates the attributes on this particular ActiveRecord object so that
+ # if it is associated with a transaction, then the state of the AR object
+ # will be updated to reflect the current state of the transaction
+ #
+ # The @transaction_state variable stores the states of the associated
+ # transaction. This relies on the fact that a transaction can only be in
+ # one rollback or commit (otherwise a list of states would be required)
+ # Each AR object inside of a transaction carries that transaction's
+ # TransactionState.
+ #
+ # This method checks to see if the ActiveRecord object's state reflects
+ # the TransactionState, and rolls back or commits the ActiveRecord object
+ # as appropriate.
+ #
+ # Since ActiveRecord objects can be inside multiple transactions, this
+ # method recursively goes through the parent of the TransactionState and
+ # checks if the ActiveRecord object reflects the state of the object.
+ def sync_with_transaction_state
+ update_attributes_from_transaction_state(@transaction_state, 0)
+ end
+
+ def update_attributes_from_transaction_state(transaction_state, depth)
+ @reflects_state = [false] if depth == 0
+
+ if transaction_state && transaction_state.finalized? && !has_transactional_callbacks?
+ unless @reflects_state[depth]
+ restore_transaction_record_state if transaction_state.rolledback?
+ clear_transaction_record_state
+ @reflects_state[depth] = true
+ end
+ end
+ end
end
end