Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mongoid/association/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def touch_field
#
# @return [ true | false ] returns true if this association is
# automatically touched, false otherwise. The default is false.
#
# @api private
def touchable?
!!@options[:touch]
end
Expand Down
30 changes: 13 additions & 17 deletions lib/mongoid/touchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ module InstanceMethods
def touch(field = nil)
return false if _root.new_record?

touches = __gather_touch_updates(Time.configured.now, field)
touches = _gather_touch_updates(Time.configured.now, field)
_root.send(:persist_atomic_operations, '$set' => touches) if touches.present?

__run_touch_callbacks_from_root
_run_touch_callbacks_from_root
true
end

Expand All @@ -40,13 +40,13 @@ def touch(field = nil)
# @return [ Hash<String, Time> ] The touch operations to perform as an atomic $set.
#
# @api private
def __gather_touch_updates(now, field = nil)
def _gather_touch_updates(now, field = nil)
field = database_field_name(field)
write_attribute(:updated_at, now) if respond_to?("updated_at=")
write_attribute(field, now) if field

touches = __extract_touches_from_atomic_sets(field) || {}
touches.merge!(_parent.__gather_touch_updates(now) || {}) if __touchable_parent?
touches = _extract_touches_from_atomic_sets(field) || {}
touches.merge!(_parent._gather_touch_updates(now) || {}) if _touchable_parent?
touches
end

Expand All @@ -55,15 +55,15 @@ def __gather_touch_updates(now, field = nil)
# child document.
#
# @api private
def __run_touch_callbacks_from_root
_parent.__run_touch_callbacks_from_root if __touchable_parent?
def _run_touch_callbacks_from_root
_parent._run_touch_callbacks_from_root if _touchable_parent?
run_callbacks(:touch)
end

# Indicates whether the parent exists and is touchable.
#
# @api private
def __touchable_parent?
def _touchable_parent?
_parent && _association&.inverse_association&.touchable?
end

Expand All @@ -77,11 +77,11 @@ def __touchable_parent?
# @return [ Hash ] The field-value pairs to update atomically.
#
# @api private
def __extract_touches_from_atomic_sets(field = nil)
def _extract_touches_from_atomic_sets(field = nil)
updates = atomic_updates['$set']
return {} unless updates

touchable_keys = %w(updated_at u_at)
touchable_keys = Set['updated_at', 'u_at']
touchable_keys << field.to_s if field.present?

updates.keys.each_with_object({}) do |key, touches|
Expand Down Expand Up @@ -144,13 +144,9 @@ def define_relation_touch_method(name, association)
define_method(method_name) do
without_autobuild do
if relation = __send__(name)
if association.touch_field
# Note that this looks up touch_field at runtime, rather than
# at method definition time.
relation.touch(association.touch_field)
else
relation.touch
end
# This looks up touch_field at runtime, rather than at method definition time.
# If touch_field is nil, it will only touch the default field (updated_at)
relation.touch(association.touch_field)
end
end
end
Expand Down