mongoid 7.5.4 → 8.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (298) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +3 -3
  4. data/Rakefile +0 -25
  5. data/lib/config/locales/en.yml +46 -30
  6. data/lib/mongoid/association/accessors.rb +32 -3
  7. data/lib/mongoid/association/bindable.rb +48 -0
  8. data/lib/mongoid/association/builders.rb +4 -2
  9. data/lib/mongoid/association/eager_loadable.rb +29 -7
  10. data/lib/mongoid/association/embedded/batchable.rb +28 -5
  11. data/lib/mongoid/association/embedded/embedded_in/binding.rb +24 -2
  12. data/lib/mongoid/association/embedded/embedded_in.rb +2 -1
  13. data/lib/mongoid/association/embedded/embeds_many/binding.rb +1 -0
  14. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +1 -1
  15. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +40 -18
  16. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +18 -4
  17. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +21 -2
  18. data/lib/mongoid/association/macros.rb +2 -1
  19. data/lib/mongoid/association/many.rb +5 -0
  20. data/lib/mongoid/association/nested/many.rb +2 -1
  21. data/lib/mongoid/association/proxy.rb +12 -0
  22. data/lib/mongoid/association/referenced/auto_save.rb +3 -2
  23. data/lib/mongoid/association/referenced/belongs_to/binding.rb +1 -0
  24. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +1 -1
  25. data/lib/mongoid/association/referenced/belongs_to.rb +1 -1
  26. data/lib/mongoid/association/referenced/counter_cache.rb +8 -8
  27. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +64 -11
  28. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +4 -1
  29. data/lib/mongoid/association/referenced/has_many/enumerable.rb +10 -18
  30. data/lib/mongoid/association/referenced/has_many/proxy.rb +12 -9
  31. data/lib/mongoid/association/referenced/has_one/buildable.rb +1 -1
  32. data/lib/mongoid/association/referenced/has_one/proxy.rb +8 -11
  33. data/lib/mongoid/association/referenced/syncable.rb +2 -2
  34. data/lib/mongoid/association/relatable.rb +38 -4
  35. data/lib/mongoid/attributes/processing.rb +9 -2
  36. data/lib/mongoid/attributes.rb +30 -27
  37. data/lib/mongoid/cacheable.rb +2 -2
  38. data/lib/mongoid/changeable.rb +37 -2
  39. data/lib/mongoid/clients/options.rb +4 -0
  40. data/lib/mongoid/clients/sessions.rb +2 -14
  41. data/lib/mongoid/config.rb +15 -11
  42. data/lib/mongoid/contextual/aggregable/memory.rb +23 -15
  43. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  44. data/lib/mongoid/contextual/map_reduce.rb +2 -2
  45. data/lib/mongoid/contextual/memory.rb +55 -28
  46. data/lib/mongoid/contextual/mongo.rb +173 -262
  47. data/lib/mongoid/contextual/none.rb +33 -15
  48. data/lib/mongoid/copyable.rb +32 -8
  49. data/lib/mongoid/criteria/includable.rb +24 -20
  50. data/lib/mongoid/criteria/marshalable.rb +10 -2
  51. data/lib/mongoid/criteria/queryable/extensions/array.rb +2 -15
  52. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +25 -4
  53. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
  54. data/lib/mongoid/criteria/queryable/extensions/date.rb +6 -1
  55. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +6 -1
  56. data/lib/mongoid/criteria/queryable/extensions/hash.rb +0 -16
  57. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -1
  58. data/lib/mongoid/criteria/queryable/extensions/object.rb +2 -1
  59. data/lib/mongoid/criteria/queryable/extensions/range.rb +13 -5
  60. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +1 -1
  61. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +3 -1
  62. data/lib/mongoid/criteria/queryable/extensions/time.rb +6 -1
  63. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +6 -1
  64. data/lib/mongoid/criteria/queryable/optional.rb +3 -9
  65. data/lib/mongoid/criteria/queryable/options.rb +1 -1
  66. data/lib/mongoid/criteria/queryable/selectable.rb +2 -24
  67. data/lib/mongoid/criteria/queryable/selector.rb +90 -5
  68. data/lib/mongoid/criteria/queryable/smash.rb +39 -6
  69. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  70. data/lib/mongoid/criteria/queryable.rb +11 -6
  71. data/lib/mongoid/criteria.rb +1 -28
  72. data/lib/mongoid/deprecable.rb +36 -0
  73. data/lib/mongoid/deprecation.rb +25 -0
  74. data/lib/mongoid/document.rb +88 -33
  75. data/lib/mongoid/equality.rb +4 -4
  76. data/lib/mongoid/errors/document_not_found.rb +6 -2
  77. data/lib/mongoid/errors/invalid_dot_dollar_assignment.rb +23 -0
  78. data/lib/mongoid/errors/invalid_field.rb +5 -1
  79. data/lib/mongoid/errors/invalid_field_type.rb +26 -0
  80. data/lib/mongoid/errors/too_many_nested_attribute_records.rb +1 -1
  81. data/lib/mongoid/errors.rb +2 -2
  82. data/lib/mongoid/extensions/array.rb +8 -6
  83. data/lib/mongoid/extensions/big_decimal.rb +29 -10
  84. data/lib/mongoid/extensions/binary.rb +42 -0
  85. data/lib/mongoid/extensions/boolean.rb +8 -2
  86. data/lib/mongoid/extensions/date.rb +26 -20
  87. data/lib/mongoid/extensions/date_time.rb +1 -1
  88. data/lib/mongoid/extensions/float.rb +4 -5
  89. data/lib/mongoid/extensions/hash.rb +12 -5
  90. data/lib/mongoid/extensions/integer.rb +4 -5
  91. data/lib/mongoid/extensions/object.rb +2 -0
  92. data/lib/mongoid/extensions/range.rb +41 -10
  93. data/lib/mongoid/extensions/regexp.rb +11 -4
  94. data/lib/mongoid/extensions/set.rb +11 -4
  95. data/lib/mongoid/extensions/string.rb +2 -13
  96. data/lib/mongoid/extensions/symbol.rb +3 -14
  97. data/lib/mongoid/extensions/time.rb +27 -16
  98. data/lib/mongoid/extensions/time_with_zone.rb +1 -2
  99. data/lib/mongoid/extensions.rb +1 -0
  100. data/lib/mongoid/factory.rb +42 -7
  101. data/lib/mongoid/fields/foreign_key.rb +7 -0
  102. data/lib/mongoid/fields/validators/macro.rb +3 -9
  103. data/lib/mongoid/fields.rb +49 -7
  104. data/lib/mongoid/findable.rb +21 -16
  105. data/lib/mongoid/indexable/specification.rb +1 -1
  106. data/lib/mongoid/indexable/validators/options.rb +4 -1
  107. data/lib/mongoid/interceptable.rb +69 -9
  108. data/lib/mongoid/persistable/creatable.rb +14 -5
  109. data/lib/mongoid/persistable/updatable.rb +12 -5
  110. data/lib/mongoid/persistence_context.rb +8 -42
  111. data/lib/mongoid/query_cache.rb +6 -258
  112. data/lib/mongoid/railties/controller_runtime.rb +1 -1
  113. data/lib/mongoid/reloadable.rb +7 -3
  114. data/lib/mongoid/scopable.rb +9 -11
  115. data/lib/mongoid/selectable.rb +1 -2
  116. data/lib/mongoid/shardable.rb +11 -35
  117. data/lib/mongoid/stateful.rb +27 -1
  118. data/lib/mongoid/timestamps/created.rb +1 -1
  119. data/lib/mongoid/timestamps/updated.rb +1 -1
  120. data/lib/mongoid/touchable.rb +2 -3
  121. data/lib/mongoid/traversable.rb +1 -0
  122. data/lib/mongoid/validatable/uniqueness.rb +2 -1
  123. data/lib/mongoid/version.rb +1 -1
  124. data/lib/mongoid/warnings.rb +3 -4
  125. data/lib/mongoid.rb +1 -0
  126. data/spec/config/mongoid.yml +16 -0
  127. data/spec/integration/app_spec.rb +8 -12
  128. data/spec/integration/associations/belongs_to_spec.rb +18 -0
  129. data/spec/integration/associations/embedded_spec.rb +15 -0
  130. data/spec/integration/associations/embeds_many_spec.rb +15 -2
  131. data/spec/integration/associations/embeds_one_spec.rb +18 -0
  132. data/spec/integration/associations/foreign_key_spec.rb +9 -0
  133. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +21 -0
  134. data/spec/integration/associations/has_one_spec.rb +97 -1
  135. data/spec/integration/associations/scope_option_spec.rb +1 -1
  136. data/spec/integration/callbacks_models.rb +95 -1
  137. data/spec/integration/callbacks_spec.rb +226 -4
  138. data/spec/integration/criteria/range_spec.rb +95 -1
  139. data/spec/integration/discriminator_key_spec.rb +115 -76
  140. data/spec/integration/dots_and_dollars_spec.rb +277 -0
  141. data/spec/integration/i18n_fallbacks_spec.rb +1 -15
  142. data/spec/integration/matcher_examples_spec.rb +20 -13
  143. data/spec/integration/matcher_operator_data/type_decimal.yml +3 -2
  144. data/spec/integration/matcher_operator_spec.rb +3 -5
  145. data/spec/integration/persistence/range_field_spec.rb +350 -0
  146. data/spec/mongoid/association/counter_cache_spec.rb +1 -1
  147. data/spec/mongoid/association/depending_spec.rb +9 -9
  148. data/spec/mongoid/association/eager_spec.rb +2 -1
  149. data/spec/mongoid/association/embedded/embedded_in/binding_spec.rb +2 -1
  150. data/spec/mongoid/association/embedded/embedded_in/buildable_spec.rb +54 -0
  151. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +69 -9
  152. data/spec/mongoid/association/embedded/embeds_many/buildable_spec.rb +112 -0
  153. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +198 -8
  154. data/spec/mongoid/association/embedded/embeds_many_models.rb +36 -0
  155. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +12 -0
  156. data/spec/mongoid/association/embedded/embeds_many_spec.rb +68 -0
  157. data/spec/mongoid/association/embedded/embeds_one/buildable_spec.rb +25 -0
  158. data/spec/mongoid/association/embedded/embeds_one_models.rb +19 -0
  159. data/spec/mongoid/association/embedded/embeds_one_spec.rb +28 -0
  160. data/spec/mongoid/association/referenced/belongs_to/binding_spec.rb +2 -1
  161. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +54 -0
  162. data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +15 -0
  163. data/spec/mongoid/association/referenced/belongs_to_models.rb +11 -0
  164. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -2
  165. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +38 -5
  166. data/spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb +25 -0
  167. data/spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb +35 -2
  168. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +109 -0
  169. data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +2 -56
  170. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +62 -13
  171. data/spec/mongoid/association/referenced/has_many_models.rb +3 -1
  172. data/spec/mongoid/association/referenced/has_many_spec.rb +25 -0
  173. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +2 -2
  174. data/spec/mongoid/association/referenced/has_one/proxy_spec.rb +107 -1
  175. data/spec/mongoid/association/referenced/has_one_models.rb +16 -0
  176. data/spec/mongoid/association/syncable_spec.rb +14 -0
  177. data/spec/mongoid/atomic/paths_spec.rb +0 -14
  178. data/spec/mongoid/attributes/nested_spec.rb +80 -11
  179. data/spec/mongoid/attributes/nested_spec_models.rb +48 -0
  180. data/spec/mongoid/attributes/projector_spec.rb +1 -5
  181. data/spec/mongoid/attributes_spec.rb +480 -27
  182. data/spec/mongoid/cacheable_spec.rb +3 -3
  183. data/spec/mongoid/changeable_spec.rb +130 -13
  184. data/spec/mongoid/clients/factory_spec.rb +23 -30
  185. data/spec/mongoid/clients/sessions_spec.rb +0 -38
  186. data/spec/mongoid/clients_spec.rb +2 -2
  187. data/spec/mongoid/config_spec.rb +52 -14
  188. data/spec/mongoid/contextual/aggregable/memory_spec.rb +396 -158
  189. data/spec/mongoid/contextual/aggregable/memory_table.yml +88 -0
  190. data/spec/mongoid/contextual/aggregable/memory_table_spec.rb +62 -0
  191. data/spec/mongoid/contextual/map_reduce_spec.rb +2 -16
  192. data/spec/mongoid/contextual/memory_spec.rb +521 -14
  193. data/spec/mongoid/contextual/mongo_spec.rb +566 -416
  194. data/spec/mongoid/contextual/none_spec.rb +11 -19
  195. data/spec/mongoid/copyable_spec.rb +451 -1
  196. data/spec/mongoid/criteria/findable_spec.rb +86 -210
  197. data/spec/mongoid/criteria/includable_spec.rb +1492 -0
  198. data/spec/mongoid/criteria/includable_spec_models.rb +54 -0
  199. data/spec/mongoid/criteria/marshalable_spec.rb +18 -1
  200. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +7 -19
  201. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +134 -26
  202. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +11 -0
  203. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +11 -0
  204. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +0 -15
  205. data/spec/mongoid/criteria/queryable/extensions/numeric_spec.rb +73 -7
  206. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +11 -0
  207. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +11 -0
  208. data/spec/mongoid/criteria/queryable/optional_spec.rb +0 -484
  209. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +50 -0
  210. data/spec/mongoid/criteria/queryable/selectable_spec.rb +77 -85
  211. data/spec/mongoid/criteria/queryable/selector_spec.rb +16 -77
  212. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  213. data/spec/mongoid/criteria_spec.rb +469 -1201
  214. data/spec/mongoid/document_fields_spec.rb +173 -24
  215. data/spec/mongoid/document_spec.rb +32 -41
  216. data/spec/mongoid/equality_spec.rb +12 -12
  217. data/spec/mongoid/errors/document_not_found_spec.rb +29 -2
  218. data/spec/mongoid/errors/invalid_field_spec.rb +1 -1
  219. data/spec/mongoid/errors/invalid_field_type_spec.rb +55 -0
  220. data/spec/mongoid/errors/mongoid_error_spec.rb +3 -1
  221. data/spec/mongoid/errors/no_environment_spec.rb +3 -3
  222. data/spec/mongoid/errors/too_many_nested_attribute_records_spec.rb +1 -1
  223. data/spec/mongoid/extensions/array_spec.rb +16 -2
  224. data/spec/mongoid/extensions/big_decimal_spec.rb +697 -212
  225. data/spec/mongoid/extensions/binary_spec.rb +44 -9
  226. data/spec/mongoid/extensions/boolean_spec.rb +68 -82
  227. data/spec/mongoid/extensions/date_class_mongoize_spec.rb +7 -3
  228. data/spec/mongoid/extensions/date_spec.rb +71 -1
  229. data/spec/mongoid/extensions/date_time_spec.rb +15 -9
  230. data/spec/mongoid/extensions/float_spec.rb +48 -76
  231. data/spec/mongoid/extensions/hash_spec.rb +30 -0
  232. data/spec/mongoid/extensions/integer_spec.rb +45 -66
  233. data/spec/mongoid/extensions/range_spec.rb +255 -54
  234. data/spec/mongoid/extensions/regexp_spec.rb +58 -33
  235. data/spec/mongoid/extensions/set_spec.rb +106 -0
  236. data/spec/mongoid/extensions/string_spec.rb +53 -25
  237. data/spec/mongoid/extensions/symbol_spec.rb +18 -25
  238. data/spec/mongoid/extensions/time_spec.rb +634 -66
  239. data/spec/mongoid/extensions/time_with_zone_spec.rb +17 -31
  240. data/spec/mongoid/factory_spec.rb +61 -1
  241. data/spec/mongoid/fields_spec.rb +321 -50
  242. data/spec/mongoid/findable_spec.rb +64 -29
  243. data/spec/mongoid/indexable/specification_spec.rb +2 -2
  244. data/spec/mongoid/indexable_spec.rb +16 -19
  245. data/spec/mongoid/interceptable_spec.rb +584 -5
  246. data/spec/mongoid/interceptable_spec_models.rb +235 -4
  247. data/spec/mongoid/matcher/extract_attribute_spec.rb +1 -5
  248. data/spec/mongoid/mongoizable_spec.rb +285 -0
  249. data/spec/mongoid/persistable/creatable_spec.rb +2 -2
  250. data/spec/mongoid/persistable/deletable_spec.rb +2 -2
  251. data/spec/mongoid/persistable/destroyable_spec.rb +2 -2
  252. data/spec/mongoid/persistable/upsertable_spec.rb +14 -0
  253. data/spec/mongoid/persistence_context_spec.rb +24 -0
  254. data/spec/mongoid/query_cache_middleware_spec.rb +0 -18
  255. data/spec/mongoid/query_cache_spec.rb +0 -154
  256. data/spec/mongoid/reloadable_spec.rb +35 -2
  257. data/spec/mongoid/scopable_spec.rb +36 -34
  258. data/spec/mongoid/shardable_models.rb +0 -14
  259. data/spec/mongoid/shardable_spec.rb +61 -153
  260. data/spec/mongoid/stateful_spec.rb +28 -0
  261. data/spec/mongoid/timestamps_spec.rb +390 -0
  262. data/spec/mongoid/timestamps_spec_models.rb +67 -0
  263. data/spec/mongoid/touchable_spec.rb +116 -0
  264. data/spec/mongoid/touchable_spec_models.rb +12 -8
  265. data/spec/mongoid/traversable_spec.rb +4 -11
  266. data/spec/mongoid/validatable/presence_spec.rb +1 -1
  267. data/spec/mongoid/validatable/uniqueness_spec.rb +60 -31
  268. data/spec/mongoid/warnings_spec.rb +35 -0
  269. data/spec/mongoid_spec.rb +1 -7
  270. data/spec/rails/controller_extension/controller_runtime_spec.rb +2 -2
  271. data/spec/rails/mongoid_spec.rb +4 -16
  272. data/spec/shared/lib/mrss/event_subscriber.rb +5 -15
  273. data/spec/shared/lib/mrss/lite_constraints.rb +0 -8
  274. data/spec/shared/shlib/server.sh +5 -5
  275. data/spec/support/constraints.rb +24 -0
  276. data/spec/support/macros.rb +30 -0
  277. data/spec/support/models/augmentation.rb +12 -0
  278. data/spec/support/models/band.rb +3 -0
  279. data/spec/support/models/catalog.rb +24 -0
  280. data/spec/support/models/circus.rb +3 -0
  281. data/spec/support/models/fanatic.rb +8 -0
  282. data/spec/support/models/implant.rb +9 -0
  283. data/spec/support/models/label.rb +2 -0
  284. data/spec/support/models/passport.rb +9 -0
  285. data/spec/support/models/person.rb +1 -0
  286. data/spec/support/models/player.rb +2 -0
  287. data/spec/support/models/powerup.rb +12 -0
  288. data/spec/support/models/registry.rb +1 -0
  289. data/spec/support/models/school.rb +14 -0
  290. data/spec/support/models/shield.rb +18 -0
  291. data/spec/support/models/student.rb +14 -0
  292. data/spec/support/models/weapon.rb +12 -0
  293. data.tar.gz.sig +0 -0
  294. metadata +689 -657
  295. metadata.gz.sig +0 -0
  296. data/lib/mongoid/errors/eager_load.rb +0 -23
  297. data/lib/mongoid/errors/invalid_value.rb +0 -17
  298. data/spec/mongoid/errors/eager_load_spec.rb +0 -31
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "spec_helper"
4
+ require_relative './timestamps_spec_models'
4
5
 
5
6
  describe Mongoid::Timestamps do
6
7
 
@@ -14,6 +15,15 @@ describe Mongoid::Timestamps do
14
15
  Dokument.fields
15
16
  end
16
17
 
18
+ let(:time_zone) { "Pacific Time (US & Canada)" }
19
+
20
+ around do |ex|
21
+ z = Time.zone
22
+ Time.zone = time_zone
23
+ ex.run
24
+ Time.zone = z
25
+ end
26
+
17
27
  before do
18
28
  document.run_callbacks(:create)
19
29
  document.run_callbacks(:save)
@@ -35,6 +45,14 @@ describe Mongoid::Timestamps do
35
45
  expect(document.updated_at).to be_within(10).of(Time.now.utc)
36
46
  end
37
47
 
48
+ it "sets the created_at to the correct time zone" do
49
+ expect(document.created_at.time_zone.name).to eq(time_zone)
50
+ end
51
+
52
+ it "sets the updated_at to the correct time zone" do
53
+ expect(document.updated_at.time_zone.name).to eq(time_zone)
54
+ end
55
+
38
56
  it "ensures created_at equals updated_at on new records" do
39
57
  expect(document.updated_at).to eq(document.created_at)
40
58
  end
@@ -107,4 +125,376 @@ describe Mongoid::Timestamps do
107
125
  expect(document.updated_at).to be_within(1).of(Time.now)
108
126
  end
109
127
  end
128
+
129
+ # This section of tests describes the behavior of the updated_at field for
130
+ # different updates on associations, as outlined in PR #5219.
131
+ describe "updated_at attribute" do
132
+ let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
133
+
134
+ let(:update_time) do
135
+ Timecop.freeze(Time.at(Time.now.to_i) + 2)
136
+ end
137
+
138
+ after do
139
+ Timecop.return
140
+ end
141
+
142
+ context "when touch: true" do
143
+ let(:user) { TimestampsSpec::Touch::User.create! }
144
+ let(:address) { TimestampsSpec::Touch::Address.create! }
145
+ let(:account) { TimestampsSpec::Touch::Account.create! }
146
+ let(:pet) { TimestampsSpec::Touch::Pet.create! }
147
+
148
+ before do
149
+ [user, address, account, pet]
150
+ update_time
151
+ end
152
+
153
+ context "when HABTM association" do
154
+
155
+ context "when updating the association itself" do
156
+ before do
157
+ user.update(addresses: [address])
158
+ end
159
+
160
+ it "updates the timestamps correctly" do
161
+ pending "MONGOID-4953"
162
+ user.updated_at.should == update_time
163
+ address.updated_at.should == update_time
164
+ end
165
+
166
+ it "updates the timestamps in the db correctly" do
167
+ pending "MONGOID-4953"
168
+ user.reload.updated_at.should == update_time
169
+ address.reload.updated_at.should == update_time
170
+ end
171
+ end
172
+
173
+ context "when updating the association's foreign key" do
174
+ before do
175
+ user.update(address_ids: [address.id])
176
+ end
177
+
178
+ it "updates the timestamps correctly" do
179
+ user.updated_at.should == update_time
180
+ address.updated_at.should == start_time
181
+ end
182
+
183
+ it "updates the timestamps in the db correctly" do
184
+ user.reload.updated_at.should == update_time
185
+ address.reload.updated_at.should == start_time
186
+ end
187
+ end
188
+ end
189
+
190
+ context "when has_many association" do
191
+
192
+ context "when updating the association itself" do
193
+ before do
194
+ user.update(accounts: [account])
195
+ end
196
+
197
+ it "updates the timestamps correctly" do
198
+ user.updated_at.should == update_time
199
+ account.updated_at.should == update_time
200
+ end
201
+
202
+ it "updates the timestamps in the db correctly" do
203
+ user.reload.updated_at.should == update_time
204
+ account.reload.updated_at.should == update_time
205
+ end
206
+ end
207
+
208
+ context "when updating the association's foreign key" do
209
+ before do
210
+ user.update(account_ids: [account.id])
211
+ end
212
+
213
+ it "updates the timestamps correctly" do
214
+ user.updated_at.should == update_time
215
+ account.updated_at.should == start_time
216
+ end
217
+
218
+ # The Account object's updated_at is updated in the database but not
219
+ # locally, since, in this case, the Account object is not passed into
220
+ # the update function, so the Account object updated locally is not
221
+ # the same as the one that we have locally here.
222
+ it "updates the timestamps in the db correctly" do
223
+ user.reload.updated_at.should == update_time
224
+ account.reload.updated_at.should == update_time
225
+ end
226
+ end
227
+ end
228
+
229
+ context "when belongs_to association; on has_many" do
230
+
231
+ context "when updating the association itself" do
232
+ before do
233
+ account.update(user: user)
234
+ end
235
+
236
+ it "updates the timestamps correctly" do
237
+ user.updated_at.should == update_time
238
+ account.updated_at.should == update_time
239
+ end
240
+
241
+ it "updates the timestamps in the db correctly" do
242
+ user.reload.updated_at.should == update_time
243
+ account.reload.updated_at.should == update_time
244
+ end
245
+ end
246
+
247
+ context "when updating the association's foreign key" do
248
+ before do
249
+ account.update(user_id: user.id)
250
+ end
251
+
252
+ it "updates the timestamps correctly" do
253
+ user.updated_at.should == start_time
254
+ account.updated_at.should == update_time
255
+ end
256
+
257
+ # The User object's updated_at is updated in the database but not
258
+ # locally, since, in this case, the User object is not passed into
259
+ # the update function, so the User object updated locally is not
260
+ # the same as the one that we have locally here.
261
+ it "updates the timestamps in the db correctly" do
262
+ user.reload.updated_at.should == update_time
263
+ account.reload.updated_at.should == update_time
264
+ end
265
+ end
266
+ end
267
+
268
+ context "when has_one association" do
269
+
270
+ context "when updating the association itself" do
271
+ before do
272
+ user.update(pet: pet)
273
+ end
274
+
275
+ it "updates the timestamps correctly" do
276
+ user.updated_at.should == update_time
277
+ pet.updated_at.should == update_time
278
+ end
279
+
280
+ it "updates the timestamps in the db correctly" do
281
+ user.reload.updated_at.should == update_time
282
+ pet.reload.updated_at.should == update_time
283
+ end
284
+ end
285
+ end
286
+
287
+ context "when belongs_to association; on has_one" do
288
+
289
+ context "when updating the association itself" do
290
+ before do
291
+ pet.update(user: user)
292
+ end
293
+
294
+ it "updates the timestamps correctly" do
295
+ user.updated_at.should == update_time
296
+ pet.updated_at.should == update_time
297
+ end
298
+
299
+ it "updates the timestamps in the db correctly" do
300
+ user.reload.updated_at.should == update_time
301
+ pet.reload.updated_at.should == update_time
302
+ end
303
+ end
304
+
305
+ context "when updating the association's foreign key" do
306
+ before do
307
+ pet.update(user_id: pet.id)
308
+ end
309
+
310
+ it "updates the timestamps correctly" do
311
+ user.updated_at.should == start_time
312
+ pet.updated_at.should == update_time
313
+ end
314
+
315
+ it "updates the timestamps in the db correctly" do
316
+ user.reload.updated_at.should == start_time
317
+ pet.reload.updated_at.should == update_time
318
+ end
319
+ end
320
+ end
321
+ end
322
+
323
+ context "when touch: false" do
324
+ let(:user) { TimestampsSpec::NoTouch::User.create! }
325
+ let(:address) { TimestampsSpec::NoTouch::Address.create! }
326
+ let(:account) { TimestampsSpec::NoTouch::Account.create! }
327
+ let(:pet) { TimestampsSpec::NoTouch::Pet.create! }
328
+
329
+ before do
330
+ [user, address, account, pet]
331
+ update_time
332
+ end
333
+
334
+ context "when HABTM association" do
335
+
336
+ context "when updating the association itself" do
337
+ before do
338
+ user.update(addresses: [address])
339
+ end
340
+
341
+ it "updates the timestamps correctly" do
342
+ pending "MONGOID-4953"
343
+ user.updated_at.should == update_time
344
+ address.updated_at.should == update_time
345
+ end
346
+
347
+ it "updates the timestamps in the db correctly" do
348
+ pending "MONGOID-4953"
349
+ user.reload.updated_at.should == update_time
350
+ address.reload.updated_at.should == update_time
351
+ end
352
+ end
353
+
354
+ context "when updating the association's foreign key" do
355
+ before do
356
+ user.update(address_ids: [address.id])
357
+ end
358
+
359
+ it "updates the timestamps correctly" do
360
+ user.updated_at.should == update_time
361
+ address.updated_at.should == start_time
362
+ end
363
+
364
+ it "updates the timestamps in the db correctly" do
365
+ user.reload.updated_at.should == update_time
366
+ address.reload.updated_at.should == start_time
367
+ end
368
+ end
369
+ end
370
+
371
+ context "when has_many association" do
372
+
373
+ context "when updating the association itself" do
374
+ before do
375
+ user.update(accounts: [account])
376
+ end
377
+
378
+ it "updates the timestamps correctly" do
379
+ user.updated_at.should == start_time
380
+ account.updated_at.should == update_time
381
+ end
382
+
383
+ it "updates the timestamps in the db correctly" do
384
+ user.reload.updated_at.should == start_time
385
+ account.reload.updated_at.should == update_time
386
+ end
387
+ end
388
+
389
+ context "when updating the association's foreign key" do
390
+ before do
391
+ user.update(account_ids: [account.id])
392
+ end
393
+
394
+ it "updates the timestamps correctly" do
395
+ user.updated_at.should == start_time
396
+ account.updated_at.should == start_time
397
+ end
398
+
399
+ # The Account object's updated_at is updated in the database but not
400
+ # locally, since, in this case, the Account object is not passed into
401
+ # the update function, so the Account object updated locally is not
402
+ # the same as the one that we have locally here.
403
+ it "updates the timestamps in the db correctly" do
404
+ user.reload.updated_at.should == start_time
405
+ account.reload.updated_at.should == update_time
406
+ end
407
+ end
408
+ end
409
+
410
+ context "when belongs_to association; on has_many" do
411
+
412
+ context "when updating the association itself" do
413
+ before do
414
+ account.update(user: user)
415
+ end
416
+
417
+ it "updates the timestamps correctly" do
418
+ user.updated_at.should == start_time
419
+ account.updated_at.should == update_time
420
+ end
421
+
422
+ it "updates the timestamps in the db correctly" do
423
+ user.reload.updated_at.should == start_time
424
+ account.reload.updated_at.should == update_time
425
+ end
426
+ end
427
+
428
+ context "when updating the association's foreign key" do
429
+ before do
430
+ account.update(user_id: user.id)
431
+ end
432
+
433
+ it "updates the timestamps correctly" do
434
+ user.updated_at.should == start_time
435
+ account.updated_at.should == update_time
436
+ end
437
+
438
+ it "updates the timestamps in the db correctly" do
439
+ user.reload.updated_at.should == start_time
440
+ account.reload.updated_at.should == update_time
441
+ end
442
+ end
443
+ end
444
+
445
+ context "when has_one association" do
446
+
447
+ context "when updating the association itself" do
448
+ before do
449
+ user.update(pet: pet)
450
+ end
451
+
452
+ it "updates the timestamps correctly" do
453
+ user.updated_at.should == start_time
454
+ pet.updated_at.should == update_time
455
+ end
456
+
457
+ it "updates the timestamps in the db correctly" do
458
+ user.reload.updated_at.should == start_time
459
+ pet.reload.updated_at.should == update_time
460
+ end
461
+ end
462
+ end
463
+
464
+ context "when belongs_to association; on has_one" do
465
+
466
+ context "when updating the association itself" do
467
+ before do
468
+ pet.update(user: user)
469
+ end
470
+
471
+ it "updates the timestamps correctly" do
472
+ user.updated_at.should == start_time
473
+ pet.updated_at.should == update_time
474
+ end
475
+
476
+ it "updates the timestamps in the db correctly" do
477
+ user.reload.updated_at.should == start_time
478
+ pet.reload.updated_at.should == update_time
479
+ end
480
+ end
481
+
482
+ context "when updating the association's foreign key" do
483
+ before do
484
+ pet.update(user_id: pet.id)
485
+ end
486
+
487
+ it "updates the timestamps correctly" do
488
+ user.updated_at.should == start_time
489
+ pet.updated_at.should == update_time
490
+ end
491
+
492
+ it "updates the timestamps in the db correctly" do
493
+ user.reload.updated_at.should == start_time
494
+ pet.reload.updated_at.should == update_time
495
+ end
496
+ end
497
+ end
498
+ end
499
+ end
110
500
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TimestampsSpec
4
+ module Touch
5
+ class User
6
+ include Mongoid::Document
7
+ include Mongoid::Timestamps
8
+
9
+ has_and_belongs_to_many :addresses, class_name: "TimestampsSpec::Touch::Address"
10
+ has_many :accounts, class_name: "TimestampsSpec::Touch::Account"
11
+ has_one :pet, class_name: "TimestampsSpec::Touch::Pet"
12
+ end
13
+
14
+ class Address
15
+ include Mongoid::Document
16
+ include Mongoid::Timestamps
17
+
18
+ has_and_belongs_to_many :users, class_name: "TimestampsSpec::Touch::User"
19
+ end
20
+
21
+ class Account
22
+ include Mongoid::Document
23
+ include Mongoid::Timestamps
24
+
25
+ belongs_to :user, required: false, touch: true, class_name: "TimestampsSpec::Touch::User"
26
+ end
27
+
28
+ class Pet
29
+ include Mongoid::Document
30
+ include Mongoid::Timestamps
31
+
32
+ belongs_to :user, required: false, touch: true, class_name: "TimestampsSpec::Touch::User"
33
+ end
34
+ end
35
+
36
+ module NoTouch
37
+ class User
38
+ include Mongoid::Document
39
+ include Mongoid::Timestamps
40
+
41
+ has_and_belongs_to_many :addresses, class_name: "TimestampsSpec::NoTouch::Address"
42
+ has_many :accounts, class_name: "TimestampsSpec::NoTouch::Account"
43
+ has_one :pet, class_name: "TimestampsSpec::NoTouch::Pet"
44
+ end
45
+
46
+ class Address
47
+ include Mongoid::Document
48
+ include Mongoid::Timestamps
49
+
50
+ has_and_belongs_to_many :users, class_name: "TimestampsSpec::NoTouch::User"
51
+ end
52
+
53
+ class Account
54
+ include Mongoid::Document
55
+ include Mongoid::Timestamps
56
+
57
+ belongs_to :user, required: false, touch: false, class_name: "TimestampsSpec::NoTouch::User"
58
+ end
59
+
60
+ class Pet
61
+ include Mongoid::Document
62
+ include Mongoid::Timestamps
63
+
64
+ belongs_to :user, required: false, touch: false, class_name: "TimestampsSpec::NoTouch::User"
65
+ end
66
+ end
67
+ end
@@ -584,5 +584,121 @@ describe Mongoid::Touchable do
584
584
  end
585
585
  end
586
586
  end
587
+
588
+ context "when the touch option is true" do
589
+
590
+ shared_examples "updates the updated_at" do
591
+
592
+ let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
593
+
594
+ let(:update_time) do
595
+ Timecop.freeze(Time.at(Time.now.to_i) + 2)
596
+ end
597
+
598
+ after do
599
+ Timecop.return
600
+ end
601
+
602
+ let(:building) do
603
+ parent_cls.create!
604
+ end
605
+
606
+ let(:floor) do
607
+ building.floors.create!
608
+ end
609
+
610
+ before do
611
+ floor
612
+ update_time
613
+ floor.level = 9
614
+ floor.send(meth)
615
+ end
616
+
617
+ it "the parent is not nil" do
618
+ expect(floor.building).to_not be nil
619
+ end
620
+
621
+ it "updates the parent's timestamp" do
622
+ building.updated_at.should == update_time
623
+ building.reload.updated_at.should == update_time
624
+ end
625
+ end
626
+
627
+ [ :save!, :destroy, :touch].each do |meth|
628
+ context "with #{meth} on referenced associations" do
629
+ let(:parent_cls) { TouchableSpec::Referenced::Building }
630
+ let(:meth) { meth }
631
+
632
+ include_examples "updates the updated_at"
633
+ end
634
+
635
+ context "with #{meth} on embedded associations" do
636
+ let(:parent_cls) { TouchableSpec::Embedded::Building }
637
+ let(:meth) { meth }
638
+
639
+ include_examples "updates the updated_at"
640
+ end
641
+ end
642
+ end
643
+
644
+ context "when the touch option is false" do
645
+
646
+ shared_examples "does not update the parent" do
647
+
648
+ let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
649
+
650
+ let(:update_time) do
651
+ Timecop.freeze(Time.at(Time.now.to_i) + 2)
652
+ end
653
+
654
+ after do
655
+ Timecop.return
656
+ end
657
+
658
+ let(:building) do
659
+ parent_cls.create!
660
+ end
661
+
662
+ let(:entrance) do
663
+ building.entrances.create!
664
+ end
665
+
666
+ before do
667
+ entrance
668
+ update_time
669
+ entrance.touch
670
+ end
671
+
672
+ it "updates the child's timestamp" do
673
+ entrance.updated_at.should == update_time
674
+ entrance.reload.updated_at.should == update_time
675
+ end
676
+
677
+ it "does not update the parent's timestamp" do
678
+ building.updated_at.should == start_time
679
+ building.reload.updated_at.should == start_time
680
+ end
681
+ end
682
+
683
+ [ :save!, :destroy, :touch].each do |meth|
684
+ context "with #{meth} on belongs_to" do
685
+ let(:meth) { meth }
686
+ let(:parent_cls) { TouchableSpec::Referenced::Building }
687
+
688
+ include_examples "does not update the parent"
689
+ end
690
+
691
+ context "with #{meth} on embedded_in" do
692
+ let(:meth) { meth }
693
+ let(:parent_cls) { TouchableSpec::Embedded::Building }
694
+
695
+ before do
696
+ skip "MONGOID-5274"
697
+ end
698
+
699
+ include_examples "does not update the parent"
700
+ end
701
+ end
702
+ end
587
703
  end
588
704
  end
@@ -6,15 +6,15 @@ module TouchableSpec
6
6
  include Mongoid::Document
7
7
  include Mongoid::Timestamps
8
8
 
9
- embeds_many :entrances
10
- embeds_many :floors
9
+ embeds_many :entrances, class_name: "TouchableSpec::Embedded::Entrance"
10
+ embeds_many :floors, class_name: "TouchableSpec::Embedded::Floor"
11
11
  end
12
12
 
13
13
  class Entrance
14
14
  include Mongoid::Document
15
15
  include Mongoid::Timestamps
16
16
 
17
- embedded_in :building
17
+ embedded_in :building, class_name: "TouchableSpec::Embedded::Building"
18
18
 
19
19
  field :last_used_at, type: Time
20
20
  end
@@ -23,7 +23,9 @@ module TouchableSpec
23
23
  include Mongoid::Document
24
24
  include Mongoid::Timestamps
25
25
 
26
- embedded_in :building, touch: true
26
+ field :level, type: Integer
27
+
28
+ embedded_in :building, touch: true, class_name: "TouchableSpec::Embedded::Building"
27
29
  end
28
30
  end
29
31
 
@@ -32,22 +34,24 @@ module TouchableSpec
32
34
  include Mongoid::Document
33
35
  include Mongoid::Timestamps
34
36
 
35
- has_many :entrances, inverse_of: :building
36
- has_many :floors, inverse_of: :building
37
+ has_many :entrances, inverse_of: :building, class_name: "TouchableSpec::Referenced::Entrance"
38
+ has_many :floors, inverse_of: :building, class_name: "TouchableSpec::Referenced::Floor"
37
39
  end
38
40
 
39
41
  class Entrance
40
42
  include Mongoid::Document
41
43
  include Mongoid::Timestamps
42
44
 
43
- belongs_to :building
45
+ belongs_to :building, touch: false, class_name: "TouchableSpec::Referenced::Building"
44
46
  end
45
47
 
46
48
  class Floor
47
49
  include Mongoid::Document
48
50
  include Mongoid::Timestamps
49
51
 
50
- belongs_to :building, touch: true
52
+ field :level, type: Integer
53
+
54
+ belongs_to :building, touch: true, class_name: "TouchableSpec::Referenced::Building"
51
55
  end
52
56
  end
53
57
  end
@@ -336,13 +336,7 @@ describe Mongoid::Traversable do
336
336
  end
337
337
 
338
338
  context "when the discriminator key is changed at the global level" do
339
- before do
340
- Mongoid.discriminator_key = "hello"
341
- end
342
-
343
- after do
344
- Mongoid.discriminator_key = "_type"
345
- end
339
+ config_override :discriminator_key, 'hello'
346
340
 
347
341
  it "sets the correct value globally" do
348
342
  expect(Mongoid.discriminator_key).to eq("hello")
@@ -904,10 +898,9 @@ describe Mongoid::Traversable do
904
898
  end
905
899
 
906
900
  context "when the global discriminator key conflicts with mongoid's internals" do
907
-
908
- after do
909
- Mongoid.discriminator_key = "_type"
910
- end
901
+ # This is not an override, it is meant to restore the original value
902
+ # after the test only.
903
+ config_override :discriminator_key, '_type'
911
904
 
912
905
  [:_association, :invalid].each do |meth|
913
906
  context "when the field is named #{meth}" do
@@ -287,7 +287,7 @@ describe Mongoid::Validatable::PresenceValidator do
287
287
  it "does not save the association target" do
288
288
  child.persisted?.should be false
289
289
 
290
- expect { child.reload }.to raise_error(Mongoid::Errors::DocumentNotFound)
290
+ expect { child.reload }.to raise_error(Mongoid::Errors::DocumentNotFound, /Document\(s\) not found for class HomAccreditation with id\(s\)/)
291
291
  end
292
292
  end
293
293
  end