From 44d1804b0a86de02865c48c552bbc57da3dc7836 Mon Sep 17 00:00:00 2001 From: Hemant Kumar Date: Wed, 9 May 2012 17:32:15 +0530 Subject: [PATCH 1/3] Fix transaction state not changing when after record gets commited --- .../lib/active_record/transactions.rb | 8 ++---- .../test/cases/transaction_callbacks_test.rb | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 743dfc5a38..64e5640791 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -302,12 +302,8 @@ module ActiveRecord def remember_transaction_record_state #:nodoc: @_start_transaction_state ||= {} @_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key) - unless @_start_transaction_state.include?(:new_record) - @_start_transaction_state[:new_record] = @new_record - end - unless @_start_transaction_state.include?(:destroyed) - @_start_transaction_state[:destroyed] = @destroyed - end + @_start_transaction_state[:new_record] = @new_record + @_start_transaction_state[:destroyed] = @destroyed @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1 end diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index f8b3e01a49..9246157a13 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -290,3 +290,28 @@ class TransactionObserverCallbacksTest < ActiveRecord::TestCase assert_equal %w{ after_rollback }, topic.history end end + +class SaveFromAfterCommitBlockTest < ActiveRecord::TestCase + self.use_transactional_fixtures = false + + class TopicWithSaveInCallback < ActiveRecord::Base + self.table_name = :topics + after_commit :cache_topic, :on => :create + attr_accessor :cached + + def cache_topic + unless cached + self.cached = true + self.save + else + self.cached = false + end + end + end + + def test_after_commit_in_save + topic = TopicWithSaveInCallback.new() + topic.save + assert_equal true, topic.cached + end +end From 1024c688a9190461549d4943661e9862cb1a17d4 Mon Sep 17 00:00:00 2001 From: Hemant Kumar Date: Wed, 9 May 2012 18:37:46 +0530 Subject: [PATCH 2/3] after_commit :on => :update should be called when save is called from after_commit callback --- activerecord/test/cases/transaction_callbacks_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index 9246157a13..6e6a9afeff 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -297,7 +297,13 @@ class SaveFromAfterCommitBlockTest < ActiveRecord::TestCase class TopicWithSaveInCallback < ActiveRecord::Base self.table_name = :topics after_commit :cache_topic, :on => :create + after_commit :call_update, :on => :update attr_accessor :cached + attr_accessor :record_updated + + def call_update + self.record_updated = true + end def cache_topic unless cached @@ -313,5 +319,6 @@ class SaveFromAfterCommitBlockTest < ActiveRecord::TestCase topic = TopicWithSaveInCallback.new() topic.save assert_equal true, topic.cached + assert_equal true, topic.record_updated end end From 041b6c6ccb2130ee8c87db2dd53736c22a79f3e8 Mon Sep 17 00:00:00 2001 From: Hemant Kumar Date: Wed, 9 May 2012 18:43:18 +0530 Subject: [PATCH 3/3] make both cached and record_updated accessors in one line --- activerecord/test/cases/transaction_callbacks_test.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/activerecord/test/cases/transaction_callbacks_test.rb b/activerecord/test/cases/transaction_callbacks_test.rb index 6e6a9afeff..9846f5b12d 100644 --- a/activerecord/test/cases/transaction_callbacks_test.rb +++ b/activerecord/test/cases/transaction_callbacks_test.rb @@ -298,8 +298,7 @@ class SaveFromAfterCommitBlockTest < ActiveRecord::TestCase self.table_name = :topics after_commit :cache_topic, :on => :create after_commit :call_update, :on => :update - attr_accessor :cached - attr_accessor :record_updated + attr_accessor :cached, :record_updated def call_update self.record_updated = true