From 61d12ebb10d9acfffad6ab76a43d3aaedfda5586 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Sun, 1 Mar 2015 22:57:14 -0500 Subject: Move transaction code to transaction module --- activerecord/lib/active_record/transactions.rb | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'activerecord/lib/active_record/transactions.rb') 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 -- cgit v1.2.3