Skip to content

Commit

Permalink
Revert "Changed behavior of touch and added touch! Originally impleme…
Browse files Browse the repository at this point in the history
…nted by Obie Fernandez, updated touch! to act as a thin wrapper to touch. [#2520 state:resolved]"

This reverts commit 3a875e6.
  • Loading branch information
lifo committed Mar 27, 2010
1 parent 62937b8 commit 68ade93
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 43 deletions.
26 changes: 8 additions & 18 deletions activerecord/lib/active_record/timestamp.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ module Timestamp
self.record_timestamps = true self.record_timestamps = true
end end


# Updates only the record's updated_at/on attributes to the current time without checking validations. # Saves the record with the updated_at/on attributes set to the current time.
# If an attribute name is passed, that attribute is used instead of the updated_at/on attributes. # 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.
# #
# Examples: # Examples:
# #
Expand All @@ -29,24 +30,13 @@ def touch(attribute = nil)
current_time = current_time_from_proper_timezone current_time = current_time_from_proper_timezone


if attribute if attribute
update_attribute(attribute, current_time) write_attribute(attribute, current_time)
else else
update_attribute('updated_at', current_time) if respond_to?(:updated_at) write_attribute('updated_at', current_time) if respond_to?(:updated_at)
update_attribute('updated_on', current_time) if respond_to?(:updated_on) write_attribute('updated_on', current_time) if respond_to?(:updated_on)
end end
end

save!
# Saves the entire 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.
#
# Examples:
#
# product.touch! # updates updated_at
# product.touch!(:designed_at) # updates the designed_at attribute
def touch!(attribute = nil)
raise ActiveRecord::RecordInvalid.new(self) unless valid?
touch(attribute)
end end




Expand Down
25 changes: 0 additions & 25 deletions activerecord/test/cases/timestamp_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,38 +30,13 @@ def test_touching_a_record_updates_its_timestamp
assert @previously_updated_at != @developer.updated_at assert @previously_updated_at != @developer.updated_at
end end


def test_touching_a_record_updates_its_timestamp_even_if_object_instance_is_invalid
@developer.name = nil
@developer.touch

assert @previously_updated_at != @developer.updated_at
end

def test_touch_bang_a_record_updates_its_timestamp
@developer.touch!

assert @previously_updated_at != @developer.updated_at
end

def test_touch_banging_a_record_fails_if_object_instance_is_invalid
@developer.name = nil
assert_raise(ActiveRecord::RecordInvalid) { @developer.touch! }
end

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


assert previously_created_at != @developer.created_at assert previously_created_at != @developer.created_at
end end


def test_touch_banging_a_different_attribute
previously_created_at = @developer.created_at
@developer.touch!(:created_at)

assert previously_created_at != @developer.created_at
end

def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
pet = Pet.first pet = Pet.first
owner = pet.owner owner = pet.owner
Expand Down

0 comments on commit 68ade93

Please sign in to comment.