Skip to content

Commit ce7dcb7

Browse files
committed
- Synchronize timestamps when touching embedded documents
- Do not run :touch in after_touch callback on embedded, since it's fully handled within the :touch method itself
1 parent e64c340 commit ce7dcb7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/mongoid/touchable.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ def touch(field = nil)
4242
# @api private
4343
def __gather_touch_updates(now, field = nil)
4444
field = database_field_name(field)
45+
# TODO: we should check the type of field is time-like (date/time/datetime/bson-timestamp) before writing
4546
write_attribute(:updated_at, now) if respond_to?("updated_at=")
4647
write_attribute(field, now) if field
4748

4849
touches = __extract_touches_from_atomic_sets(field) || {}
50+
51+
# TODO: this needs to a guard `... if _parent && _association_to_parent.options[:touch]`
52+
# However, the `_association_to_parent` method doesn't exist!
4953
touches.merge!(_parent.__gather_touch_updates(now) || {}) if _parent
5054
touches
5155
end
@@ -78,6 +82,7 @@ def __extract_touches_from_atomic_sets(field = nil)
7882
updates = atomic_updates['$set']
7983
return {} unless updates
8084

85+
# TODO: this should evaluate the field alias of updated_at rather than assuming u_at
8186
touchable_keys = %w(updated_at u_at)
8287
touchable_keys << field.to_s if field.present?
8388

@@ -104,9 +109,13 @@ def define_touchable!(association)
104109
name = association.name
105110
method_name = define_relation_touch_method(name, association)
106111
association.inverse_class.tap do |klass|
112+
# TODO: for EMBEDDED docs, save/destroy we should call .touch within the save/destroy
113+
# action rather than as a callback this will ensure synchronized timestamps
107114
klass.after_save method_name
108115
klass.after_destroy method_name
109-
klass.after_touch method_name
116+
117+
# Embedded docs recursively handle touch updates within the #touch method itself
118+
klass.after_touch method_name unless association.embedded?
110119
end
111120
end
112121

0 commit comments

Comments
 (0)