Skip to content
Browse files

Merge pull request #15125 from arthurnn/small_refactoring_transaction…

…_clear

Small refactoring on clear_transaction_record_state
  • Loading branch information...
2 parents cb85ddc + 535f299 commit 9900703ebf4d2688b2203e9b28d3cc588fac1641 @chancancode chancancode committed May 16, 2014
Showing with 19 additions and 4 deletions.
  1. +6 −4 activerecord/lib/active_record/transactions.rb
  2. +13 −0 activerecord/test/cases/transactions_test.rb
View
10 activerecord/lib/active_record/transactions.rb
@@ -295,7 +295,7 @@ def rollback_active_record_state!
def committed! #:nodoc:
run_callbacks :commit if destroyed? || persisted?
ensure
- @_start_transaction_state.clear
+ clear_transaction_record_state(true)
end
# Call the +after_rollback+ callbacks. The +force_restore_state+ argument indicates if the record
@@ -328,7 +328,7 @@ def with_transaction_returning_status
begin
status = yield
rescue ActiveRecord::Rollback
- @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
+ clear_transaction_record_state
status = nil
end
@@ -353,9 +353,11 @@ def remember_transaction_record_state #:nodoc:
end
# Clear the new record state and id of a record.
- def clear_transaction_record_state #:nodoc:
+ def clear_transaction_record_state(force = false) #:nodoc:
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
- @_start_transaction_state.clear if @_start_transaction_state[:level] < 1
+ if force || @_start_transaction_state[:level] < 1
+ @_start_transaction_state.clear
+ end
end
# Restore the new record state and id of a record that was previously saved by a call to save_record_state.
View
13 activerecord/test/cases/transactions_test.rb
@@ -123,6 +123,19 @@ def @first.after_save_for_transaction
assert !Topic.find(1).approved?
end
+ def test_rolling_back_in_a_callback_rollbacks_before_save
+ def @first.before_save_for_transaction
+ raise ActiveRecord::Rollback
+ end
+ assert !@first.approved
+
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ end
+ assert !Topic.find(@first.id).approved?, "Should not commit the approved flag"
+ end
+
def test_raising_exception_in_nested_transaction_restore_state_in_save
topic = Topic.new

0 comments on commit 9900703

Please sign in to comment.
Something went wrong with that request. Please try again.