Skip to content

Commit

Permalink
with this fix touch method - does not call validations - doest not ca…
Browse files Browse the repository at this point in the history
…ll callbacks - updates updated_at/on along with attribute if attribute is provided - marks udpated_at/on and attribute as NOT changed

[#2520 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
Neeraj Singh authored and josevalim committed Jul 13, 2010
1 parent 2aed63e commit 1d45ea0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -111,6 +111,7 @@ def update_attribute(name, value)
if record_update_timestamps
timestamp_attributes_for_update_in_model.each do |column|
hash[column] = read_attribute(column)
@changed_attributes.delete(column.to_s)
end
end

Expand Down
16 changes: 7 additions & 9 deletions activerecord/lib/active_record/timestamp.rb
Expand Up @@ -21,24 +21,22 @@ module Timestamp
end

# Saves the record with the updated_at/on attributes set to the current time.
# If the save fails because of validation errors, an
# ActiveRecord::RecordInvalid exception is raised. If an attribute name is passed,
# that attribute is used for the touch instead of the updated_at/on attributes.
# Please note that no validation is performed and no callbacks are executed.
# If an attribute name is passed, that attribute is updated along with
# updated_at/on attributes.
#
# Examples:
#
# product.touch # updates updated_at
# product.touch(:designed_at) # updates the designed_at attribute
# product.touch # updates updated_at/on
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
def touch(attribute = nil)
current_time = current_time_from_proper_timezone

if attribute
write_attribute(attribute, current_time)
self.update_attribute(attribute, current_time)
else
timestamp_attributes_for_update_in_model.each { |column| write_attribute(column.to_s, current_time) }
timestamp_attributes_for_update_in_model.each { |column| self.update_attribute(column, current_time) }
end

save!
end

private
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/timestamp_test.rb
Expand Up @@ -25,16 +25,26 @@ def test_saving_a_unchanged_record_doesnt_update_its_timestamp
end

def test_touching_a_record_updates_its_timestamp
previous_salary = @developer.salary
@developer.salary = previous_salary + 10000
@developer.touch

assert_not_equal @previously_updated_at, @developer.updated_at
assert_equal previous_salary + 10000, @developer.salary
assert @developer.salary_changed?, 'developer salary should have changed'
assert @developer.changed?, 'developer should be marked as changed'
@developer.reload
assert_equal previous_salary, @developer.salary
end

def test_touching_a_different_attribute
previously_created_at = @developer.created_at
@developer.touch(:created_at)

assert !@developer.created_at_changed? , 'created_at should not be changed'
assert !@developer.changed?, 'record should not be changed'
assert_not_equal previously_created_at, @developer.created_at
assert_not_equal @previously_updated_at, @developer.updated_at
end

def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
Expand Down

0 comments on commit 1d45ea0

Please sign in to comment.