From a128070b773e687313d7c041f6ae3fe0eedd2e34 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Sun, 4 Jul 2010 22:37:45 -0400 Subject: [PATCH] update_attributes and the bang version code should be in transaction [#922 state:resolved] --- activerecord/lib/active_record/persistence.rb | 14 +++++++++----- activerecord/test/cases/transactions_test.rb | 24 ++++++++++++++++++++++++ activerecord/test/models/developer.rb | 2 +- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 50166c4..d0f8c00 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -113,15 +113,19 @@ module ActiveRecord # Updates all the attributes from the passed-in Hash and saves the record. # If the object is invalid, the saving will fail and false will be returned. def update_attributes(attributes) - self.attributes = attributes - save + with_transaction_returning_status do + self.attributes = attributes + save + end end # Updates an object just like Base.update_attributes but calls save! instead # of save so an exception is raised if the record is invalid. def update_attributes!(attributes) - self.attributes = attributes - save! + with_transaction_returning_status do + self.attributes = attributes + save! + end end # Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1). @@ -234,4 +238,4 @@ module ActiveRecord end end end -end \ No newline at end of file +end diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 958a4e4..0eba9a8 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -34,6 +34,30 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_update_attributes_should_rollback_on_failure + developer = Developer.find(1) + logs_size = developer.audit_logs.size + new_logs_size = 2 + logs_size + 2.times { developer.audit_logs.create(:message => 'audited') } + assert_equal new_logs_size, developer.audit_logs.size + status = developer.update_attributes(:name => 'Nu', :audit_log_ids => []) + assert !status + assert_equal new_logs_size, developer.audit_logs(true).size + end + + def test_update_attributes_should_rollback_on_failure! + developer = Developer.find(:first) + logs_size = developer.audit_logs.size + new_logs_size = 2 + logs_size + 2.times { developer.audit_logs.create(:message => 'audited') } + assert_equal new_logs_size, developer.audit_logs.size + assert_raise(ActiveRecord::RecordInvalid) do + developer.update_attributes!(:audit_log_ids => [], :name => nil) + end + assert_equal new_logs_size, developer.audit_logs(true).size + end + + def test_successful_with_return class << Topic.connection alias :real_commit_db_transaction :commit_db_transaction diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index de68fd7..512a7c9 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -41,7 +41,7 @@ class Developer < ActiveRecord::Base has_and_belongs_to_many :special_projects, :join_table => 'developers_projects', :association_foreign_key => 'project_id' - has_many :audit_logs + has_many :audit_logs, :dependent => :destroy scope :jamises, :conditions => {:name => 'Jamis'} -- 1.6.5.2