From b09829354f4699941824f53903f4a8743fa98bd5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 18 Dec 2004 15:15:27 +0000 Subject: [PATCH] Fixed the automated timestamping feature when running under Rails' development environment that resets the inheritable attributes on each request. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@212 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record.rb | 2 +- activerecord/lib/active_record/timestamp.rb | 24 +++++++++++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index db2b44ac02..e9927ae980 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed the automated timestamping feature when running under Rails' development environment that resets the inheritable attributes on each request. + * Added Base#update_attributes that'll accept a hash of attributes and save the record (returning true if it passed validation, false otherwise). Before: diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 36c367b78f..5c9043b094 100755 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -41,12 +41,12 @@ require 'active_record/acts/tree' ActiveRecord::Base.class_eval do include ActiveRecord::Validations + include ActiveRecord::Timestamp include ActiveRecord::Callbacks include ActiveRecord::Associations include ActiveRecord::Aggregations include ActiveRecord::Transactions include ActiveRecord::Reflection - include ActiveRecord::Timestamp include ActiveRecord::Acts::Tree include ActiveRecord::Acts::List end diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index 59c72aa4a9..b267284cf8 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -7,19 +7,31 @@ module ActiveRecord module Timestamp def self.append_features(base) # :nodoc: super - base.before_create :timestamp_before_create - base.before_update :timestamp_before_update + + base.class_eval do + alias_method :create_without_timestamps, :create + alias_method :create, :create_with_timestamps + + alias_method :update_without_timestamps, :update + alias_method :update, :update_with_timestamps + end end - def timestamp_before_create + def create_with_timestamps write_attribute("created_at", Time.now) if record_timestamps && respond_to?(:created_at) && created_at.nil? write_attribute("created_on", Time.now) if record_timestamps && respond_to?(:created_on) && created_on.nil? - timestamp_before_update - end - def timestamp_before_update write_attribute("updated_at", Time.now) if record_timestamps && respond_to?(:updated_at) write_attribute("updated_on", Time.now) if record_timestamps && respond_to?(:updated_on) + + create_without_timestamps + end + + def update_with_timestamps + write_attribute("updated_at", Time.now) if record_timestamps && respond_to?(:updated_at) + write_attribute("updated_on", Time.now) if record_timestamps && respond_to?(:updated_on) + + update_without_timestamps end end