From 49020a5b2852520ca001cdd85fbab7988474ea3c Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 1 Feb 2009 11:13:27 +0100 Subject: [PATCH] wrap_update_attributes_in_a_transaction --- activerecord/lib/active_record/transactions.rb | 16 ++++++++++- activerecord/test/cases/transactions_test.rb | 34 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 0b6e52c..9eabe71 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -10,7 +10,7 @@ module ActiveRecord base.extend(ClassMethods) base.class_eval do - [:destroy, :save, :save!].each do |method| + [:destroy, :save, :save!, :update_attribute, :update_attributes, :update_attributes!].each do |method| alias_method_chain method, :transactions end end @@ -200,6 +200,20 @@ module ActiveRecord rollback_active_record_state! { self.class.transaction { save_without_transactions! } } end + def update_attribute_with_transactions(name, value) + with_transaction_returning_status(:update_attribute_without_transactions, name, value) + end + + def update_attributes_with_transactions(attributes) + with_transaction_returning_status(:update_attributes_without_transactions, attributes) + end + + def update_attributes_with_transactions!(attributes) + transaction do + update_attributes_without_transactions!(attributes) + end + end + # Reset id and @new_record if the transaction rolls back. def rollback_active_record_state! id_present = has_attribute?(self.class.primary_key) diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 4a07a8b..bb469f1 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -182,6 +182,40 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_update_attribute_should_rollback_on_failure + Developer.before_save do + false + end + developer = Developer.first + developer.audit_logs.clear + 2.times { developer.audit_logs.create(:message => 'message') } + assert_equal 2, developer.audit_logs.size + status = developer.update_attribute(:audit_log_ids, []) + assert !status + assert_equal 2, developer.audit_logs(true).size + end + + def test_update_attributes_should_rollback_on_failure + developer = Developer.first + developer.audit_logs.clear + 2.times { developer.audit_logs.create(:message => 'message') } + assert_equal 2, developer.audit_logs.size + status = developer.update_attributes(:audit_log_ids => [], :name => nil) + assert !status + assert_equal 2, developer.audit_logs(true).size + end + + def test_update_attributes_should_rollback_on_failure! + developer = Developer.first + developer.audit_logs.clear + 2.times { developer.audit_logs.create(:message => 'message') } + assert_equal 2, developer.audit_logs.size + assert_raise(ActiveRecord::RecordInvalid) do + developer.update_attributes!(:audit_log_ids => [], :name => nil) + end + assert_equal 2, developer.audit_logs(true).size + end + def test_nested_explicit_transactions Topic.transaction do Topic.transaction do -- 1.6.1