From 219d71fb9049da677c9426c6d81bf9e74fae1977 Mon Sep 17 00:00:00 2001 From: Hyonjee Joo Date: Mon, 16 Feb 2015 01:30:41 -0500 Subject: Add `time` option to `#touch` Fixes #18905. `#touch` now takes time as an option. Setting the option saves the record with the updated_at/on attributes set to the current time or the time specified. Updated tests and documentation accordingly. --- activerecord/lib/active_record/persistence.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/persistence.rb') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index af7aef6e43..3ca58523d7 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -429,14 +429,17 @@ module ActiveRecord self end - # Saves the record with the updated_at/on attributes set to the current time. + # Saves the record with the updated_at/on attributes set to the current time + # or the time specified. # Please note that no validation is performed and only the +after_touch+, # +after_commit+ and +after_rollback+ callbacks are executed. # + # This method can be passed attribute names and an optional time argument. # If attribute names are passed, they are updated along with updated_at/on - # attributes. + # attributes. If no time argument is passed, the current time is used as default. # - # product.touch # updates updated_at/on + # product.touch # updates updated_at/on with current time + # product.touch(time: Time.new(2015, 2, 16, 0, 0, 0)) # updates updated_at/on with specified time # product.touch(:designed_at) # updates the designed_at attribute and updated_at/on # product.touch(:started_at, :ended_at) # updates started_at, ended_at and updated_at/on attributes # @@ -460,19 +463,18 @@ module ActiveRecord # ball = Ball.new # ball.touch(:updated_at) # => raises ActiveRecordError # - def touch(*names) + def touch(*names, time: current_time_from_proper_timezone) raise ActiveRecordError, "cannot touch on a new record object" unless persisted? attributes = timestamp_attributes_for_update_in_model attributes.concat(names) unless attributes.empty? - current_time = current_time_from_proper_timezone changes = {} attributes.each do |column| column = column.to_s - changes[column] = write_attribute(column, current_time) + changes[column] = write_attribute(column, time) end changes[self.class.locking_column] = increment_lock if locking_enabled? -- cgit v1.2.3