Skip to content

Commit b613c3c

Browse files
committed
Add an internal (private API) after_touch callback. [rails#5271 state:resolved]
1 parent 311ea94 commit b613c3c

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ def add_touch_callbacks(reflection, touch_attribute)
14981498
end
14991499
end
15001500
after_save(method_name)
1501+
after_touch(method_name)
15011502
after_destroy(method_name)
15021503
end
15031504

activerecord/lib/active_record/callbacks.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ module Callbacks
228228
extend ActiveSupport::Concern
229229

230230
CALLBACKS = [
231-
:after_initialize, :after_find, :before_validation, :after_validation,
231+
:after_initialize, :after_find, :after_touch, :before_validation, :after_validation,
232232
:before_save, :around_save, :after_save, :before_create, :around_create,
233233
:after_create, :before_update, :around_update, :after_update,
234234
:before_destroy, :around_destroy, :after_destroy
@@ -238,7 +238,7 @@ module Callbacks
238238
extend ActiveModel::Callbacks
239239
include ActiveModel::Validations::Callbacks
240240

241-
define_model_callbacks :initialize, :find, :only => :after
241+
define_model_callbacks :initialize, :find, :touch, :only => :after
242242
define_model_callbacks :save, :create, :update, :destroy
243243
end
244244

@@ -256,6 +256,10 @@ def destroy #:nodoc:
256256
_run_destroy_callbacks { super }
257257
end
258258

259+
def touch(*) #:nodoc:
260+
_run_touch_callbacks { super }
261+
end
262+
259263
def deprecated_callback_method(symbol) #:nodoc:
260264
if respond_to?(symbol, true)
261265
ActiveSupport::Deprecation.warn("Overwriting #{symbol} in your models has been deprecated, please use Base##{symbol} :method_name instead")

activerecord/lib/active_record/persistence.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ def reload(options = nil)
218218
self
219219
end
220220

221+
# Saves the record with the updated_at/on attributes set to the current time.
222+
# Please note that no validation is performed and no callbacks are executed.
223+
# If an attribute name is passed, that attribute is updated along with
224+
# updated_at/on attributes.
225+
#
226+
# Examples:
227+
#
228+
# product.touch # updates updated_at/on
229+
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
230+
def touch(attribute = nil)
231+
update_attribute(attribute, current_time_from_proper_timezone)
232+
end
233+
221234
private
222235
def create_or_update
223236
raise ReadOnlyRecord if readonly?

activerecord/lib/active_record/timestamp.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,6 @@ module Timestamp
3131
class_inheritable_accessor :record_timestamps, :instance_writer => false
3232
self.record_timestamps = true
3333
end
34-
35-
# Saves the record with the updated_at/on attributes set to the current time.
36-
# Please note that no validation is performed and no callbacks are executed.
37-
# If an attribute name is passed, that attribute is updated along with
38-
# updated_at/on attributes.
39-
#
40-
# Examples:
41-
#
42-
# product.touch # updates updated_at/on
43-
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
44-
def touch(attribute = nil)
45-
update_attribute(attribute, current_time_from_proper_timezone)
46-
end
4734

4835
private
4936

activerecord/test/cases/timestamp_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
require 'models/developer'
33
require 'models/owner'
44
require 'models/pet'
5+
require 'models/toy'
56

67
class TimestampTest < ActiveRecord::TestCase
7-
fixtures :developers, :owners, :pets
8+
fixtures :developers, :owners, :pets, :toys
89

910
def setup
1011
@developer = Developer.first
@@ -91,11 +92,10 @@ def test_touching_a_record_touches_parent_record_and_grandparent_record
9192
pet = toy.pet
9293
owner = pet.owner
9394

94-
previously_owner_updated_at = owner.updated_at
95-
95+
owner.update_attribute(:updated_at, (time = 3.days.ago))
9696
toy.touch
9797

98-
assert_not_equal previously_owner_updated_at, owner.updated_at
98+
assert_not_equal time, owner.updated_at
9999
ensure
100100
Toy.belongs_to :pet
101101
end

0 commit comments

Comments
 (0)