@@ -42,10 +42,14 @@ def touch(field = nil)
42
42
# @api private
43
43
def __gather_touch_updates ( now , field = nil )
44
44
field = database_field_name ( field )
45
+ # TODO: we should check the type of field is time-like (date/time/datetime/bson-timestamp) before writing
45
46
write_attribute ( :updated_at , now ) if respond_to? ( "updated_at=" )
46
47
write_attribute ( field , now ) if field
47
48
48
49
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!
49
53
touches . merge! ( _parent . __gather_touch_updates ( now ) || { } ) if _parent
50
54
touches
51
55
end
@@ -78,6 +82,7 @@ def __extract_touches_from_atomic_sets(field = nil)
78
82
updates = atomic_updates [ '$set' ]
79
83
return { } unless updates
80
84
85
+ # TODO: this should evaluate the field alias of updated_at rather than assuming u_at
81
86
touchable_keys = %w( updated_at u_at )
82
87
touchable_keys << field . to_s if field . present?
83
88
@@ -104,9 +109,13 @@ def define_touchable!(association)
104
109
name = association . name
105
110
method_name = define_relation_touch_method ( name , association )
106
111
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
107
114
klass . after_save method_name
108
115
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?
110
119
end
111
120
end
112
121
0 commit comments