Add an internal (private API) after_touch callback. [#5271 state:resolved]
This commit is contained in:
parent
311ea94f73
commit
b613c3cc7b
|
@ -1498,6 +1498,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
after_save(method_name)
|
after_save(method_name)
|
||||||
|
after_touch(method_name)
|
||||||
after_destroy(method_name)
|
after_destroy(method_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -228,7 +228,7 @@ module ActiveRecord
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
CALLBACKS = [
|
CALLBACKS = [
|
||||||
:after_initialize, :after_find, :before_validation, :after_validation,
|
:after_initialize, :after_find, :after_touch, :before_validation, :after_validation,
|
||||||
:before_save, :around_save, :after_save, :before_create, :around_create,
|
:before_save, :around_save, :after_save, :before_create, :around_create,
|
||||||
:after_create, :before_update, :around_update, :after_update,
|
:after_create, :before_update, :around_update, :after_update,
|
||||||
:before_destroy, :around_destroy, :after_destroy
|
:before_destroy, :around_destroy, :after_destroy
|
||||||
|
@ -238,7 +238,7 @@ module ActiveRecord
|
||||||
extend ActiveModel::Callbacks
|
extend ActiveModel::Callbacks
|
||||||
include ActiveModel::Validations::Callbacks
|
include ActiveModel::Validations::Callbacks
|
||||||
|
|
||||||
define_model_callbacks :initialize, :find, :only => :after
|
define_model_callbacks :initialize, :find, :touch, :only => :after
|
||||||
define_model_callbacks :save, :create, :update, :destroy
|
define_model_callbacks :save, :create, :update, :destroy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -256,6 +256,10 @@ module ActiveRecord
|
||||||
_run_destroy_callbacks { super }
|
_run_destroy_callbacks { super }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def touch(*) #:nodoc:
|
||||||
|
_run_touch_callbacks { super }
|
||||||
|
end
|
||||||
|
|
||||||
def deprecated_callback_method(symbol) #:nodoc:
|
def deprecated_callback_method(symbol) #:nodoc:
|
||||||
if respond_to?(symbol, true)
|
if respond_to?(symbol, true)
|
||||||
ActiveSupport::Deprecation.warn("Overwriting #{symbol} in your models has been deprecated, please use Base##{symbol} :method_name instead")
|
ActiveSupport::Deprecation.warn("Overwriting #{symbol} in your models has been deprecated, please use Base##{symbol} :method_name instead")
|
||||||
|
|
|
@ -218,6 +218,19 @@ module ActiveRecord
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Saves the record with the updated_at/on attributes set to the current time.
|
||||||
|
# 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/on
|
||||||
|
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
|
||||||
|
def touch(attribute = nil)
|
||||||
|
update_attribute(attribute, current_time_from_proper_timezone)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def create_or_update
|
def create_or_update
|
||||||
raise ReadOnlyRecord if readonly?
|
raise ReadOnlyRecord if readonly?
|
||||||
|
|
|
@ -32,19 +32,6 @@ module ActiveRecord
|
||||||
self.record_timestamps = true
|
self.record_timestamps = true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Saves the record with the updated_at/on attributes set to the current time.
|
|
||||||
# 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/on
|
|
||||||
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
|
|
||||||
def touch(attribute = nil)
|
|
||||||
update_attribute(attribute, current_time_from_proper_timezone)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def create #:nodoc:
|
def create #:nodoc:
|
||||||
|
|
|
@ -2,9 +2,10 @@ require 'cases/helper'
|
||||||
require 'models/developer'
|
require 'models/developer'
|
||||||
require 'models/owner'
|
require 'models/owner'
|
||||||
require 'models/pet'
|
require 'models/pet'
|
||||||
|
require 'models/toy'
|
||||||
|
|
||||||
class TimestampTest < ActiveRecord::TestCase
|
class TimestampTest < ActiveRecord::TestCase
|
||||||
fixtures :developers, :owners, :pets
|
fixtures :developers, :owners, :pets, :toys
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@developer = Developer.first
|
@developer = Developer.first
|
||||||
|
@ -91,11 +92,10 @@ class TimestampTest < ActiveRecord::TestCase
|
||||||
pet = toy.pet
|
pet = toy.pet
|
||||||
owner = pet.owner
|
owner = pet.owner
|
||||||
|
|
||||||
previously_owner_updated_at = owner.updated_at
|
owner.update_attribute(:updated_at, (time = 3.days.ago))
|
||||||
|
|
||||||
toy.touch
|
toy.touch
|
||||||
|
|
||||||
assert_not_equal previously_owner_updated_at, owner.updated_at
|
assert_not_equal time, owner.updated_at
|
||||||
ensure
|
ensure
|
||||||
Toy.belongs_to :pet
|
Toy.belongs_to :pet
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue