aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/transactions_test.rb
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2007-01-02 05:36:30 +0000
committerRick Olson <technoweenie@gmail.com>2007-01-02 05:36:30 +0000
commit1af2022cc32b604529a5ac3a85a3bd92a3c42936 (patch)
tree494c54bce8f7446f066afea2675425233342fdfa /activerecord/test/transactions_test.rb
parentb75f28edb4675250e5f18cf9760e4a11a5f4c926 (diff)
downloadrails-1af2022cc32b604529a5ac3a85a3bd92a3c42936.tar.gz
rails-1af2022cc32b604529a5ac3a85a3bd92a3c42936.tar.bz2
rails-1af2022cc32b604529a5ac3a85a3bd92a3c42936.zip
Rollback #new_record? and #id values for created records that rollback in an after_save callback. Closes #6910 [Ben Curren]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5830 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/transactions_test.rb')
-rw-r--r--activerecord/test/transactions_test.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/activerecord/test/transactions_test.rb b/activerecord/test/transactions_test.rb
index b0d1eb4eb8..ecc0da425d 100644
--- a/activerecord/test/transactions_test.rb
+++ b/activerecord/test/transactions_test.rb
@@ -121,6 +121,36 @@ class TransactionTest < Test::Unit::TestCase
remove_exception_raising_after_save_callback_to_topic
end
end
+
+ def test_callback_rollback_in_create
+ new_topic = Topic.new(
+ :title => "A new topic",
+ :author_name => "Ben",
+ :author_email_address => "ben@example.com",
+ :written_on => "2003-07-16t15:28:11.2233+01:00",
+ :last_read => "2004-04-15",
+ :bonus_time => "2005-01-30t15:28:00.00+01:00",
+ :content => "Have a nice day",
+ :approved => false)
+ new_record_snapshot = new_topic.new_record?
+ id_snapshot = new_topic.id
+
+ # Make sure the second save gets the after_create callback called.
+ 2.times do
+ begin
+ add_exception_raising_after_create_callback_to_topic
+ new_topic.approved = true
+ new_topic.save
+ flunk
+ rescue => e
+ assert_equal "Make the transaction rollback", e.message
+ assert_equal new_record_snapshot, new_topic.new_record?, "The topic should have its old new_record value"
+ assert_equal id_snapshot, new_topic.id, "The topic should have its old id"
+ ensure
+ remove_exception_raising_after_create_callback_to_topic
+ end
+ end
+ end
def test_nested_explicit_transactions
Topic.transaction do
@@ -144,6 +174,14 @@ class TransactionTest < Test::Unit::TestCase
def remove_exception_raising_after_save_callback_to_topic
Topic.class_eval { remove_method :after_save }
end
+
+ def add_exception_raising_after_create_callback_to_topic
+ Topic.class_eval { def after_create() raise "Make the transaction rollback" end }
+ end
+
+ def remove_exception_raising_after_create_callback_to_topic
+ Topic.class_eval { remove_method :after_create }
+ end
end
if current_adapter?(:PostgreSQLAdapter)