aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2014-03-20 08:00:28 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2014-03-20 08:00:28 -0300
commit140ea2251b17ee9851e2511bf0a82a9e62197cd7 (patch)
tree9c45fec539f9b820e224c8c25f26153fff387288 /activerecord
parenteafec4694c5b37eff9d83b1188b8e331fa6027fa (diff)
parentc80ca4c7803b4e8ed7f125ada9acc6b7c499af5f (diff)
downloadrails-140ea2251b17ee9851e2511bf0a82a9e62197cd7.tar.gz
rails-140ea2251b17ee9851e2511bf0a82a9e62197cd7.tar.bz2
rails-140ea2251b17ee9851e2511bf0a82a9e62197cd7.zip
Merge pull request #14423 from yakko/persistence-touches-many
ActiveRecord#touch should accept multiple attributes Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md12
-rw-r--r--activerecord/lib/active_record/persistence.rb5
-rw-r--r--activerecord/test/cases/timestamp_test.rb12
3 files changed, 27 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 3aee68e3c9..28e81428c0 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*
+
* `rake db:structure:dump` only dumps schema information if the schema
migration table exists.
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