From 1abc081bd90967c9fcd0ddaada4077f1a7812cb0 Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Sun, 3 Apr 2011 11:42:50 +0200 Subject: [PATCH] update_attributes now accepts a block in addition to attributes hash --- activerecord/lib/active_record/persistence.rb | 1 + activerecord/test/cases/persistence_test.rb | 28 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index a916c88..389c08f 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -141,6 +141,7 @@ module ActiveRecord # attributes assignment. For example, setting the IDs of a child collection. with_transaction_returning_status do self.attributes = attributes + yield(self) if block_given? save end end diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 9aa13f0..c2643a2 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -512,6 +512,34 @@ class PersistencesTest < ActiveRecord::TestCase Reply.reset_callbacks(:validate) end + def test_update_attributes_with_block + company = Company.find(1) + assert_equal "37signals", company.name + assert_equal 1, company.rating + + company.update_attributes(:name => '37signals, LLC') do |c| + c.rating = 37 + end + + company.reload + assert_equal "37signals, LLC", company.name + assert_equal 37, company.rating + end + + def test_update_attributes!_with_block + company = Company.find(1) + assert_equal "37signals", company.name + assert_equal 1, company.rating + + company.update_attributes!(:name => '37signals, LLC') do |c| + c.rating = 37 + end + + company.reload + assert_equal "37signals, LLC", company.name + assert_equal 37, company.rating + end + def test_destroyed_returns_boolean developer = Developer.first assert_equal false, developer.destroyed? -- 1.7.4.1