From 690352dce492938ab54a4b7e2befbd0a6931de3c Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Fri, 2 Jul 2010 14:36:13 -0400 Subject: consolidating updated_at and updated_on MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/timestamp.rb | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb index ffd12d2082..da8324ddcc 100644 --- a/activerecord/lib/active_record/timestamp.rb +++ b/activerecord/lib/active_record/timestamp.rb @@ -35,8 +35,7 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 \ No newline at end of file +end + -- cgit v1.2.3