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
This commit is contained in:
David Heinemeier Hansson 2004-12-18 15:15:27 +00:00
parent 3e6fe858b6
commit b09829354f
3 changed files with 21 additions and 7 deletions

View File

@ -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:

View File

@ -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

View File

@ -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