From 97019f9f5a94fa102debe13f5a957726e7d4783e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 31 Mar 2008 01:49:57 +0000 Subject: Partial updates play nice with updated_at/on timestamps git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9159 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record.rb | 4 ++-- activerecord/lib/active_record/timestamp.rb | 2 +- activerecord/test/cases/dirty_test.rb | 6 ++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 3fc40291ba..8b274120df 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -62,6 +62,8 @@ ActiveRecord::Base.class_eval do include ActiveRecord::Validations include ActiveRecord::Locking::Optimistic include ActiveRecord::Locking::Pessimistic + include ActiveRecord::AttributeMethods + include ActiveRecord::Dirty include ActiveRecord::Callbacks include ActiveRecord::Observing include ActiveRecord::Timestamp @@ -73,8 +75,6 @@ ActiveRecord::Base.class_eval do include ActiveRecord::Reflection include ActiveRecord::Calculations include ActiveRecord::Serialization - include ActiveRecord::AttributeMethods - include ActiveRecord::Dirty end require 'active_record/connection_adapters/abstract_adapter' diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index dc95d2aabb..a9e0efa6fe 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -30,7 +30,7 @@ module ActiveRecord end def update_with_timestamps(*args) #:nodoc: - if record_timestamps + if record_timestamps && (!partial_updates? || changed?) t = self.class.default_timezone == :utc ? Time.now.utc : Time.now write_attribute('updated_at', t) if respond_to?(:updated_at) write_attribute('updated_on', t) if respond_to?(:updated_on) diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 356293140c..7412e63872 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -99,13 +99,19 @@ class DirtyTest < ActiveRecord::TestCase def test_partial_update pirate = Pirate.new(:catchphrase => 'foo') + old_updated_on = 1.hour.ago.beginning_of_day with_partial_updates Pirate, false do assert_queries(2) { 2.times { pirate.save! } } + Pirate.update_all({ :updated_on => old_updated_on }, :id => pirate.id) end with_partial_updates Pirate, true do assert_queries(0) { 2.times { pirate.save! } } + assert_equal old_updated_on, pirate.reload.updated_on + + assert_queries(1) { pirate.catchphrase = 'bar'; pirate.save! } + assert_not_equal old_updated_on, pirate.reload.updated_on end end -- cgit v1.2.3