Permalink
Browse files

consolidating updated_at and updated_on

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 17650e3 commit 690352dce492938ab54a4b7e2befbd0a6931de3c Neeraj Singh committed with josevalim Jul 2, 2010
Showing with 16 additions and 9 deletions.
  1. +16 −9 activerecord/lib/active_record/timestamp.rb
View
25 activerecord/lib/active_record/timestamp.rb
@@ -35,8 +35,7 @@ def touch(attribute = nil)
if attribute
write_attribute(attribute, current_time)
else
- write_attribute('updated_at', current_time) if respond_to?(:updated_at)
- write_attribute('updated_on', current_time) if respond_to?(:updated_on)
+ timestamp_attributes_for_update_in_model.each { |column| write_attribute(column.to_s, current_time) }
end
save!
@@ -50,8 +49,9 @@ def create #:nodoc:
write_attribute('created_at', current_time) if respond_to?(:created_at) && created_at.nil?
write_attribute('created_on', current_time) if respond_to?(:created_on) && created_on.nil?
- write_attribute('updated_at', current_time) if respond_to?(:updated_at) && updated_at.nil?
- write_attribute('updated_on', current_time) if respond_to?(:updated_on) && updated_on.nil?
+ timestamp_attributes_for_update.each do |column|
+ write_attribute(column.to_s, current_time) if respond_to?(column) && self.send(column).nil?
+ end
end
super
@@ -60,16 +60,23 @@ def create #:nodoc:
def update(*args) #:nodoc:
if record_timestamps && (!partial_updates? || changed?)
current_time = current_time_from_proper_timezone
-
- write_attribute('updated_at', current_time) if respond_to?(:updated_at)
- write_attribute('updated_on', current_time) if respond_to?(:updated_on)
+ timestamp_attributes_for_update_in_model.each { |column| write_attribute(column.to_s, current_time) }
end
super
end
+
+ def timestamp_attributes_for_update #:nodoc:
+ [:updated_at, :updated_on]
+ end
+
+ def timestamp_attributes_for_update_in_model #:nodoc:
+ ([:updated_at, :updated_on].inject([]) { |sum, elem| respond_to?(elem) ? sum << elem : sum })
+ end
- def current_time_from_proper_timezone
+ def current_time_from_proper_timezone #:nodoc:
self.class.default_timezone == :utc ? Time.now.utc : Time.now
end
end
-end
+end
+

0 comments on commit 690352d

Please sign in to comment.