Permalink
Browse files

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
  • Loading branch information...
1 parent 3aaf60d commit 97019f9f5a94fa102debe13f5a957726e7d4783e @jeremy jeremy committed Mar 31, 2008
@@ -62,6 +62,8 @@
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 @@
include ActiveRecord::Reflection
include ActiveRecord::Calculations
include ActiveRecord::Serialization
- include ActiveRecord::AttributeMethods
- include ActiveRecord::Dirty
end
require 'active_record/connection_adapters/abstract_adapter'
@@ -30,7 +30,7 @@ def create_with_timestamps #:nodoc:
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)
@@ -99,13 +99,19 @@ def test_attribute_should_be_compared_with_type_cast
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

0 comments on commit 97019f9

Please sign in to comment.