From c80ca4c7803b4e8ed7f125ada9acc6b7c499af5f Mon Sep 17 00:00:00 2001 From: Thiago Pinto Date: Wed, 19 Mar 2014 21:13:03 -0400 Subject: ActiveRecord#touch should accept multiple attributes #14423 --- activerecord/CHANGELOG.md | 12 ++++++++++++ activerecord/lib/active_record/persistence.rb | 5 +++-- activerecord/test/cases/timestamp_test.rb | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f5b8a3145d..b6748eb24d 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,15 @@ +* `ActiveRecord#touch` should accept many attributes at once. Suggested at #14423. + + Example: + + photo = Photo.last + photo.touch(:signed_at, :sealed_at) + photo.updated_at # was changed + photo.signed_at # was changed + photo.sealed_at # was changed + + *James Pinto* + * Reap connections that were checked out by now-dead threads, instead of waiting until they disconnect by themselves. Before this change, a suitably constructed series of short-lived threads could starve diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index d85fad1e13..eb45289b2e 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -412,6 +412,7 @@ module ActiveRecord # # product.touch # updates updated_at/on # 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 # # If used along with +belongs_to+ then +touch+ will invoke +touch+ method on associated object. # @@ -432,11 +433,11 @@ module ActiveRecord # ball = Ball.new # ball.touch(:updated_at) # => raises ActiveRecordError # - def touch(name = nil) + def touch(*names) raise ActiveRecordError, "cannot touch on a new record object" unless persisted? attributes = timestamp_attributes_for_update_in_model - attributes << name if name + attributes.concat(names) unless attributes.empty? current_time = current_time_from_proper_timezone diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 717e0e1866..5308fa8808 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -89,6 +89,18 @@ class TimestampTest < ActiveRecord::TestCase assert_in_delta Time.now, task.ending, 1 end + def test_touching_many_attributes_updates_them + task = Task.first + previous_starting = task.starting + previous_ending = task.ending + task.touch(:starting, :ending) + + assert_not_equal previous_starting, task.starting + assert_not_equal previous_ending, task.ending + assert_in_delta Time.now, task.starting, 1 + assert_in_delta Time.now, task.ending, 1 + end + def test_touching_a_record_without_timestamps_is_unexceptional assert_nothing_raised { Car.first.touch } end -- cgit v1.2.3