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
@@ -191,7 +191,7 @@ describe Mongoid::Indexable do
191
191
  end
192
192
  end
193
193
 
194
- context "when using a custom discriminator_key" do
194
+ context "when using a custom discriminator_key" do
195
195
  context "when indexes have not been added" do
196
196
  let(:klass) do
197
197
  Class.new do
@@ -202,15 +202,15 @@ describe Mongoid::Indexable do
202
202
  end
203
203
  end
204
204
  end
205
-
205
+
206
206
  before do
207
207
  klass.add_indexes
208
208
  end
209
-
209
+
210
210
  let(:spec) do
211
211
  klass.index_specification(dkey: 1)
212
212
  end
213
-
213
+
214
214
  it "adds the _type index" do
215
215
  expect(spec.options).to eq(unique: false, background: true)
216
216
  end
@@ -262,21 +262,6 @@ describe Mongoid::Indexable do
262
262
  end
263
263
  end
264
264
 
265
- context "when providing a drop_dups option" do
266
-
267
- before do
268
- klass.index({ name: 1 }, drop_dups: true)
269
- end
270
-
271
- let(:options) do
272
- klass.index_specification(name: 1).options
273
- end
274
-
275
- it "sets the index with drop_dups option" do
276
- expect(options).to eq(drop_dups: true)
277
- end
278
- end
279
-
280
265
  context "when providing a sparse option" do
281
266
 
282
267
  before do
@@ -438,6 +423,18 @@ describe Mongoid::Indexable do
438
423
  end
439
424
  end
440
425
 
426
+ context "when providing a geo haystack index with a bucket_size" do
427
+
428
+ let(:message) do
429
+ 'The geoHaystack type is deprecated.'
430
+ end
431
+
432
+ it "logs a deprecation warning" do
433
+ expect(Mongoid::Warnings).to receive(:warn_geo_haystack_deprecated)
434
+ klass.index({ location: "geoHaystack" }, { min: -200, max: 200, bucket_size: 0.5 })
435
+ end
436
+ end
437
+
441
438
  context "when providing a Spherical Geospatial index" do
442
439
 
443
440
  before do
@@ -1746,8 +1746,12 @@ describe Mongoid::Interceptable do
1746
1746
  [InterceptableSpec::CbChild, :after_validation],
1747
1747
  [InterceptableSpec::CbParent, :after_validation],
1748
1748
  [InterceptableSpec::CbParent, :before_save],
1749
+ [InterceptableSpec::CbParent, :around_save_open],
1749
1750
  [InterceptableSpec::CbParent, :before_create],
1751
+ [InterceptableSpec::CbParent, :around_create_open],
1752
+ [InterceptableSpec::CbParent, :around_create_close],
1750
1753
  [InterceptableSpec::CbParent, :after_create],
1754
+ [InterceptableSpec::CbParent, :around_save_close],
1751
1755
  [InterceptableSpec::CbParent, :after_save],
1752
1756
  ]
1753
1757
  end
@@ -1769,26 +1773,601 @@ describe Mongoid::Interceptable do
1769
1773
 
1770
1774
  let(:expected) do
1771
1775
  [
1776
+ [InterceptableSpec::CbCascadedChild, :before_validation],
1777
+ [InterceptableSpec::CbCascadedChild, :after_validation],
1772
1778
  [InterceptableSpec::CbParent, :before_validation],
1773
1779
  [InterceptableSpec::CbCascadedChild, :before_validation],
1774
1780
  [InterceptableSpec::CbCascadedChild, :after_validation],
1781
+
1775
1782
  [InterceptableSpec::CbParent, :after_validation],
1776
1783
  [InterceptableSpec::CbParent, :before_save],
1777
- [InterceptableSpec::CbCascadedChild, :before_save],
1784
+ [InterceptableSpec::CbParent, :around_save_open],
1778
1785
  [InterceptableSpec::CbParent, :before_create],
1786
+ [InterceptableSpec::CbParent, :around_create_open],
1787
+
1788
+ [InterceptableSpec::CbCascadedChild, :before_save],
1789
+ [InterceptableSpec::CbCascadedChild, :around_save_open],
1779
1790
  [InterceptableSpec::CbCascadedChild, :before_create],
1780
- [InterceptableSpec::CbParent, :after_create],
1791
+ [InterceptableSpec::CbCascadedChild, :around_create_open],
1792
+
1793
+ [InterceptableSpec::CbCascadedChild, :around_create_close],
1781
1794
  [InterceptableSpec::CbCascadedChild, :after_create],
1782
- [InterceptableSpec::CbParent, :after_save],
1795
+ [InterceptableSpec::CbCascadedChild, :around_save_close],
1783
1796
  [InterceptableSpec::CbCascadedChild, :after_save],
1797
+
1798
+ [InterceptableSpec::CbParent, :around_create_close],
1799
+ [InterceptableSpec::CbParent, :after_create],
1800
+ [InterceptableSpec::CbParent, :around_save_close],
1801
+ [InterceptableSpec::CbParent, :after_save]
1784
1802
  ]
1785
1803
  end
1786
1804
 
1787
1805
  it 'calls callbacks in the right order' do
1788
- pending 'MONGOID-3795'
1789
-
1790
1806
  parent.save!
1791
1807
  expect(registry.calls).to eq expected
1792
1808
  end
1793
1809
  end
1810
+
1811
+ context "with associations" do
1812
+ context "has_one" do
1813
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1814
+
1815
+ let(:parent) do
1816
+ InterceptableSpec::CbHasOneParent.new(registry).tap do |parent|
1817
+ parent.child = InterceptableSpec::CbHasOneChild.new(registry)
1818
+ end
1819
+ end
1820
+
1821
+ let(:expected) do
1822
+ [
1823
+ [InterceptableSpec::CbHasOneParent, :before_validation],
1824
+ [InterceptableSpec::CbHasOneChild, :before_validation],
1825
+ [InterceptableSpec::CbHasOneChild, :after_validation],
1826
+ [InterceptableSpec::CbHasOneParent, :after_validation],
1827
+ [InterceptableSpec::CbHasOneParent, :before_save],
1828
+
1829
+ [InterceptableSpec::CbHasOneParent, :around_save_open],
1830
+ [InterceptableSpec::CbHasOneParent, :before_create],
1831
+ [InterceptableSpec::CbHasOneParent, :around_create_open],
1832
+
1833
+ [InterceptableSpec::CbHasOneParent, :insert_into_database],
1834
+
1835
+ [InterceptableSpec::CbHasOneChild, :before_validation],
1836
+ [InterceptableSpec::CbHasOneChild, :after_validation],
1837
+ [InterceptableSpec::CbHasOneChild, :before_save],
1838
+ [InterceptableSpec::CbHasOneChild, :around_save_open],
1839
+ [InterceptableSpec::CbHasOneChild, :before_create],
1840
+ [InterceptableSpec::CbHasOneChild, :around_create_open],
1841
+
1842
+ [InterceptableSpec::CbHasOneChild, :around_create_close],
1843
+ [InterceptableSpec::CbHasOneChild, :after_create],
1844
+ [InterceptableSpec::CbHasOneChild, :around_save_close],
1845
+ [InterceptableSpec::CbHasOneChild, :after_save],
1846
+
1847
+ [InterceptableSpec::CbHasOneParent, :around_create_close],
1848
+ [InterceptableSpec::CbHasOneParent, :after_create],
1849
+ [InterceptableSpec::CbHasOneParent, :around_save_close],
1850
+ [InterceptableSpec::CbHasOneParent, :after_save],
1851
+ ]
1852
+ end
1853
+
1854
+ it 'calls callbacks in the right order' do
1855
+ parent.save!
1856
+ expect(registry.calls).to eq expected
1857
+ end
1858
+ end
1859
+
1860
+ context "embeds_one" do
1861
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1862
+
1863
+ let(:parent) do
1864
+ InterceptableSpec::CbEmbedsOneParent.new(registry).tap do |parent|
1865
+ parent.child = InterceptableSpec::CbEmbedsOneChild.new(registry)
1866
+ end
1867
+ end
1868
+
1869
+ context "create" do
1870
+ let(:expected) do
1871
+ [
1872
+ [InterceptableSpec::CbEmbedsOneChild, :before_validation],
1873
+ [InterceptableSpec::CbEmbedsOneChild, :after_validation],
1874
+ [InterceptableSpec::CbEmbedsOneParent, :before_validation],
1875
+ [InterceptableSpec::CbEmbedsOneChild, :before_validation],
1876
+ [InterceptableSpec::CbEmbedsOneChild, :after_validation],
1877
+ [InterceptableSpec::CbEmbedsOneParent, :after_validation],
1878
+
1879
+ [InterceptableSpec::CbEmbedsOneParent, :before_save],
1880
+ [InterceptableSpec::CbEmbedsOneParent, :around_save_open],
1881
+ [InterceptableSpec::CbEmbedsOneParent, :before_create],
1882
+ [InterceptableSpec::CbEmbedsOneParent, :around_create_open],
1883
+
1884
+ [InterceptableSpec::CbEmbedsOneChild, :before_save],
1885
+ [InterceptableSpec::CbEmbedsOneChild, :around_save_open],
1886
+ [InterceptableSpec::CbEmbedsOneChild, :before_create],
1887
+ [InterceptableSpec::CbEmbedsOneChild, :around_create_open],
1888
+
1889
+ [InterceptableSpec::CbEmbedsOneParent, :insert_into_database],
1890
+
1891
+ [InterceptableSpec::CbEmbedsOneChild, :around_create_close],
1892
+ [InterceptableSpec::CbEmbedsOneChild, :after_create],
1893
+ [InterceptableSpec::CbEmbedsOneChild, :around_save_close],
1894
+ [InterceptableSpec::CbEmbedsOneChild, :after_save],
1895
+
1896
+ [InterceptableSpec::CbEmbedsOneParent, :around_create_close],
1897
+ [InterceptableSpec::CbEmbedsOneParent, :after_create],
1898
+ [InterceptableSpec::CbEmbedsOneParent, :around_save_close],
1899
+ [InterceptableSpec::CbEmbedsOneParent, :after_save]
1900
+ ]
1901
+ end
1902
+
1903
+ it 'calls callbacks in the right order' do
1904
+ parent.save!
1905
+ expect(registry.calls).to eq expected
1906
+ end
1907
+ end
1908
+
1909
+ context "update" do
1910
+ let(:expected) do
1911
+ [
1912
+ [InterceptableSpec::CbEmbedsOneChild, :before_validation],
1913
+ [InterceptableSpec::CbEmbedsOneChild, :after_validation],
1914
+ [InterceptableSpec::CbEmbedsOneParent, :before_validation],
1915
+ [InterceptableSpec::CbEmbedsOneChild, :before_validation],
1916
+ [InterceptableSpec::CbEmbedsOneChild, :after_validation],
1917
+ [InterceptableSpec::CbEmbedsOneParent, :after_validation],
1918
+
1919
+ [InterceptableSpec::CbEmbedsOneParent, :before_save],
1920
+ [InterceptableSpec::CbEmbedsOneParent, :around_save_open],
1921
+ [InterceptableSpec::CbEmbedsOneParent, :before_update],
1922
+ [InterceptableSpec::CbEmbedsOneParent, :around_update_open],
1923
+
1924
+ [InterceptableSpec::CbEmbedsOneChild, :before_save],
1925
+ [InterceptableSpec::CbEmbedsOneChild, :around_save_open],
1926
+ [InterceptableSpec::CbEmbedsOneChild, :before_update],
1927
+ [InterceptableSpec::CbEmbedsOneChild, :around_update_open],
1928
+
1929
+ [InterceptableSpec::CbEmbedsOneChild, :around_update_close],
1930
+ [InterceptableSpec::CbEmbedsOneChild, :after_update],
1931
+ [InterceptableSpec::CbEmbedsOneChild, :around_save_close],
1932
+ [InterceptableSpec::CbEmbedsOneChild, :after_save],
1933
+
1934
+ [InterceptableSpec::CbEmbedsOneParent, :around_update_close],
1935
+ [InterceptableSpec::CbEmbedsOneParent, :after_update],
1936
+ [InterceptableSpec::CbEmbedsOneParent, :around_save_close],
1937
+ [InterceptableSpec::CbEmbedsOneParent, :after_save]
1938
+ ]
1939
+ end
1940
+
1941
+ it 'calls callbacks in the right order' do
1942
+ parent.callback_registry = nil
1943
+ parent.child.callback_registry = nil
1944
+ parent.save!
1945
+
1946
+ parent.callback_registry = registry
1947
+ parent.child.callback_registry = registry
1948
+ parent.name = "name"
1949
+ parent.child.age = 10
1950
+
1951
+ parent.save!
1952
+ expect(registry.calls).to eq expected
1953
+ end
1954
+ end
1955
+ end
1956
+
1957
+ context "has_many" do
1958
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
1959
+
1960
+ let(:parent) do
1961
+ InterceptableSpec::CbHasManyParent.new(registry).tap do |parent|
1962
+ parent.children = [
1963
+ InterceptableSpec::CbHasManyChild.new(registry),
1964
+ InterceptableSpec::CbHasManyChild.new(registry)
1965
+ ]
1966
+ end
1967
+ end
1968
+
1969
+ let(:expected) do
1970
+ [
1971
+ [InterceptableSpec::CbHasManyParent, :before_validation],
1972
+ [InterceptableSpec::CbHasManyChild, :before_validation],
1973
+ [InterceptableSpec::CbHasManyChild, :after_validation],
1974
+ [InterceptableSpec::CbHasManyChild, :before_validation],
1975
+ [InterceptableSpec::CbHasManyChild, :after_validation],
1976
+ [InterceptableSpec::CbHasManyParent, :after_validation],
1977
+
1978
+ [InterceptableSpec::CbHasManyParent, :before_save],
1979
+ [InterceptableSpec::CbHasManyParent, :around_save_open],
1980
+ [InterceptableSpec::CbHasManyParent, :before_create],
1981
+ [InterceptableSpec::CbHasManyParent, :around_create_open],
1982
+
1983
+ [InterceptableSpec::CbHasManyParent, :insert_into_database],
1984
+
1985
+ [InterceptableSpec::CbHasManyChild, :before_validation],
1986
+ [InterceptableSpec::CbHasManyChild, :after_validation],
1987
+ [InterceptableSpec::CbHasManyChild, :before_save],
1988
+ [InterceptableSpec::CbHasManyChild, :around_save_open],
1989
+ [InterceptableSpec::CbHasManyChild, :before_create],
1990
+ [InterceptableSpec::CbHasManyChild, :around_create_open],
1991
+ [InterceptableSpec::CbHasManyChild, :around_create_close],
1992
+ [InterceptableSpec::CbHasManyChild, :after_create],
1993
+ [InterceptableSpec::CbHasManyChild, :around_save_close],
1994
+ [InterceptableSpec::CbHasManyChild, :after_save],
1995
+
1996
+ [InterceptableSpec::CbHasManyChild, :before_validation],
1997
+ [InterceptableSpec::CbHasManyChild, :after_validation],
1998
+ [InterceptableSpec::CbHasManyChild, :before_save],
1999
+ [InterceptableSpec::CbHasManyChild, :around_save_open],
2000
+ [InterceptableSpec::CbHasManyChild, :before_create],
2001
+ [InterceptableSpec::CbHasManyChild, :around_create_open],
2002
+ [InterceptableSpec::CbHasManyChild, :around_create_close],
2003
+ [InterceptableSpec::CbHasManyChild, :after_create],
2004
+ [InterceptableSpec::CbHasManyChild, :around_save_close],
2005
+ [InterceptableSpec::CbHasManyChild, :after_save],
2006
+
2007
+ [InterceptableSpec::CbHasManyParent, :around_create_close],
2008
+ [InterceptableSpec::CbHasManyParent, :after_create],
2009
+ [InterceptableSpec::CbHasManyParent, :around_save_close],
2010
+ [InterceptableSpec::CbHasManyParent, :after_save]
2011
+ ]
2012
+ end
2013
+
2014
+ it 'calls callbacks in the right order' do
2015
+ parent.save!
2016
+ expect(registry.calls).to eq expected
2017
+ end
2018
+ end
2019
+
2020
+ context "embeds_many" do
2021
+ let(:registry) { InterceptableSpec::CallbackRegistry.new }
2022
+
2023
+ let(:parent) do
2024
+ InterceptableSpec::CbEmbedsManyParent.new(registry).tap do |parent|
2025
+ parent.children = [
2026
+ InterceptableSpec::CbEmbedsManyChild.new(registry),
2027
+ InterceptableSpec::CbEmbedsManyChild.new(registry),
2028
+ ]
2029
+ end
2030
+ end
2031
+
2032
+ let(:expected) do
2033
+ [
2034
+ [InterceptableSpec::CbEmbedsManyChild, :before_validation],
2035
+ [InterceptableSpec::CbEmbedsManyChild, :after_validation],
2036
+ [InterceptableSpec::CbEmbedsManyChild, :before_validation],
2037
+ [InterceptableSpec::CbEmbedsManyChild, :after_validation],
2038
+ [InterceptableSpec::CbEmbedsManyParent, :before_validation],
2039
+ [InterceptableSpec::CbEmbedsManyChild, :before_validation],
2040
+ [InterceptableSpec::CbEmbedsManyChild, :after_validation],
2041
+ [InterceptableSpec::CbEmbedsManyChild, :before_validation],
2042
+ [InterceptableSpec::CbEmbedsManyChild, :after_validation],
2043
+ [InterceptableSpec::CbEmbedsManyParent, :after_validation],
2044
+
2045
+ [InterceptableSpec::CbEmbedsManyParent, :before_save],
2046
+ [InterceptableSpec::CbEmbedsManyParent, :around_save_open],
2047
+ [InterceptableSpec::CbEmbedsManyParent, :before_create],
2048
+ [InterceptableSpec::CbEmbedsManyParent, :around_create_open],
2049
+
2050
+ [InterceptableSpec::CbEmbedsManyChild, :before_save],
2051
+ [InterceptableSpec::CbEmbedsManyChild, :around_save_open],
2052
+ [InterceptableSpec::CbEmbedsManyChild, :before_save],
2053
+
2054
+ [InterceptableSpec::CbEmbedsManyChild, :around_save_open],
2055
+ [InterceptableSpec::CbEmbedsManyChild, :before_create],
2056
+ [InterceptableSpec::CbEmbedsManyChild, :around_create_open],
2057
+
2058
+ [InterceptableSpec::CbEmbedsManyChild, :before_create],
2059
+ [InterceptableSpec::CbEmbedsManyChild, :around_create_open],
2060
+
2061
+ [InterceptableSpec::CbEmbedsManyParent, :insert_into_database],
2062
+
2063
+ [InterceptableSpec::CbEmbedsManyChild, :around_create_close],
2064
+ [InterceptableSpec::CbEmbedsManyChild, :after_create],
2065
+
2066
+ [InterceptableSpec::CbEmbedsManyChild, :around_create_close],
2067
+ [InterceptableSpec::CbEmbedsManyChild, :after_create],
2068
+
2069
+ [InterceptableSpec::CbEmbedsManyChild, :around_save_close],
2070
+ [InterceptableSpec::CbEmbedsManyChild, :after_save],
2071
+
2072
+ [InterceptableSpec::CbEmbedsManyChild, :around_save_close],
2073
+ [InterceptableSpec::CbEmbedsManyChild, :after_save],
2074
+
2075
+ [InterceptableSpec::CbEmbedsManyParent, :around_create_close],
2076
+ [InterceptableSpec::CbEmbedsManyParent, :after_create],
2077
+ [InterceptableSpec::CbEmbedsManyParent, :around_save_close],
2078
+ [InterceptableSpec::CbEmbedsManyParent, :after_save]
2079
+ ]
2080
+ end
2081
+
2082
+ it 'calls callbacks in the right order' do
2083
+ parent.save!
2084
+ expect(registry.calls).to eq expected
2085
+ end
2086
+ end
2087
+ end
2088
+
2089
+ context "when accessing parent document from callbacks" do
2090
+ shared_examples 'accesses the correct parent' do
2091
+ it "accesses the correct parent in after_find" do
2092
+ expect(from_db.after_find_player).to eq(player._id)
2093
+ end
2094
+
2095
+ it "accesses the correct parent in after_initialize" do
2096
+ expect(from_db.after_initialize_player).to eq(player._id)
2097
+ end
2098
+
2099
+ it "accesses the correct parent in default" do
2100
+ expect(from_db.after_default_player).to eq(player._id)
2101
+ end
2102
+
2103
+ it "accesses the correct parent in unpersisted after_initialize" do
2104
+ expect(unpersisted.after_initialize_player).to eq(player._id)
2105
+ end
2106
+ end
2107
+
2108
+ context "when using create methods" do
2109
+
2110
+ context "when the child is an embeds_many association" do
2111
+ let!(:player) do
2112
+ Player.create!.tap do |player|
2113
+ player.implants.create!
2114
+ end
2115
+ end
2116
+
2117
+ let(:unpersisted) { player.implants.first }
2118
+
2119
+ before do
2120
+ # The default is originally set when creating this document, and it is
2121
+ # subsequently persisted to the database. Therefore when we retrieve
2122
+ # this document from the database, this field is already set, and
2123
+ # the default Proc is not called. This unset is needed to allow the
2124
+ # default Proc to be called when the document is retrieved from the
2125
+ # database.
2126
+ Player.find(player.id).implants.first.unset(:after_default_player)
2127
+ end
2128
+
2129
+ let(:from_db) do
2130
+ Player.find(player.id).implants.first
2131
+ end
2132
+
2133
+ include_examples 'accesses the correct parent'
2134
+ end
2135
+
2136
+ context "when the child is an embeds_one association" do
2137
+ let!(:player) do
2138
+ Player.create!.tap do |player|
2139
+ player.create_augmentation
2140
+ end
2141
+ end
2142
+
2143
+ let(:unpersisted) { player.augmentation }
2144
+
2145
+ before do
2146
+ Player.find(player.id).augmentation.unset(:after_default_player)
2147
+ end
2148
+
2149
+ let(:from_db) do
2150
+ Player.find(player.id).augmentation
2151
+ end
2152
+
2153
+ include_examples 'accesses the correct parent'
2154
+ end
2155
+
2156
+ context "when the child is a has_many association" do
2157
+ let!(:player) do
2158
+ Player.create!.tap do |player|
2159
+ player.weapons.create!
2160
+ end
2161
+ end
2162
+
2163
+ let(:unpersisted) { player.weapons.first }
2164
+
2165
+ before do
2166
+ Player.find(player.id).weapons.first.unset(:after_default_player)
2167
+ end
2168
+
2169
+ let(:from_db) do
2170
+ Player.find(player.id).weapons.first
2171
+ end
2172
+
2173
+ include_examples 'accesses the correct parent'
2174
+ end
2175
+
2176
+ context "when the child is a has_one association" do
2177
+ let!(:player) do
2178
+ Player.create!.tap do |player|
2179
+ player.create_powerup
2180
+ player.save!
2181
+ end
2182
+ end
2183
+
2184
+ let(:unpersisted) { player.powerup }
2185
+
2186
+ before do
2187
+ Player.find(player.id).powerup.unset(:after_default_player)
2188
+ end
2189
+
2190
+ let(:from_db) do
2191
+ Player.find(player.id).powerup
2192
+ end
2193
+
2194
+ include_examples 'accesses the correct parent'
2195
+ end
2196
+
2197
+ context "when the child is a has_and_belongs_to_many association" do
2198
+ let!(:player) do
2199
+ Player.create!.tap do |player|
2200
+ player.shields.create!
2201
+ end
2202
+ end
2203
+
2204
+ let(:unpersisted) { player.shields.first }
2205
+
2206
+ before do
2207
+ Player.find(player.id).shields.unset(:after_default_player)
2208
+ end
2209
+
2210
+ let(:from_db) do
2211
+ Player.find(player.id).shields.first
2212
+ end
2213
+
2214
+ include_examples 'accesses the correct parent'
2215
+ end
2216
+ end
2217
+
2218
+ context "when using build methods" do
2219
+
2220
+ context "when the child is an embeds_many association" do
2221
+ let!(:player) do
2222
+ Player.create!.tap do |player|
2223
+ player.implants.build
2224
+ player.implants.first.save!
2225
+ end
2226
+ end
2227
+
2228
+ let(:unpersisted) { player.implants.first }
2229
+
2230
+ before do
2231
+ Player.find(player.id).implants.first.unset(:after_default_player)
2232
+ end
2233
+
2234
+ let(:from_db) do
2235
+ Player.find(player.id).implants.first
2236
+ end
2237
+
2238
+ include_examples 'accesses the correct parent'
2239
+ end
2240
+
2241
+ context "when the child is an embeds_one association" do
2242
+ let!(:player) do
2243
+ Player.create!.tap do |player|
2244
+ player.build_augmentation
2245
+ player.save!
2246
+ end
2247
+ end
2248
+
2249
+ let(:unpersisted) { player.augmentation }
2250
+
2251
+ before do
2252
+ Player.find(player.id).augmentation.unset(:after_default_player)
2253
+ end
2254
+
2255
+ let(:from_db) do
2256
+ Player.find(player.id).augmentation
2257
+ end
2258
+
2259
+ include_examples 'accesses the correct parent'
2260
+ end
2261
+
2262
+ context "when the child is a has_many association" do
2263
+ let!(:player) do
2264
+ Player.create!.tap do |player|
2265
+ player.weapons.build
2266
+ player.weapons.first.save!
2267
+ end
2268
+ end
2269
+
2270
+ let(:unpersisted) { player.weapons.first }
2271
+
2272
+ before do
2273
+ Player.find(player.id).weapons.first.unset(:after_default_player)
2274
+ end
2275
+
2276
+ let(:from_db) do
2277
+ Player.find(player.id).weapons.first
2278
+ end
2279
+
2280
+ include_examples 'accesses the correct parent'
2281
+ end
2282
+
2283
+ context "when the child is a has_one association" do
2284
+ let!(:player) do
2285
+ Player.create!.tap do |player|
2286
+ player.build_powerup
2287
+ player.powerup.save!
2288
+ end
2289
+ end
2290
+
2291
+ let(:unpersisted) { player.powerup }
2292
+
2293
+ before do
2294
+ Player.find(player.id).powerup.unset(:after_default_player)
2295
+ end
2296
+
2297
+ let(:from_db) do
2298
+ Player.find(player.id).powerup
2299
+ end
2300
+
2301
+ include_examples 'accesses the correct parent'
2302
+ end
2303
+
2304
+ context "when the child is a has_and_belongs_to_many association" do
2305
+ let!(:player) do
2306
+ Player.create!.tap do |player|
2307
+ player.shields.build
2308
+ player.shields.first.save!
2309
+ end
2310
+ end
2311
+
2312
+ let(:unpersisted) { player.shields.first }
2313
+
2314
+ before do
2315
+ Player.find(player.id).shields.unset(:after_default_player)
2316
+ end
2317
+
2318
+ let(:from_db) do
2319
+ Player.find(player.id).shields.first
2320
+ end
2321
+
2322
+ include_examples 'accesses the correct parent'
2323
+ end
2324
+ end
2325
+ end
2326
+
2327
+ context "when accessing associations in defaults" do
2328
+ context "when not using autobuilding" do
2329
+ let(:band) { InterceptableBand.create(name: "Molejo") }
2330
+ let(:song) { band.songs.create(name: "Cilada") }
2331
+
2332
+ it "assigns the default correctly" do
2333
+ expect(song.band_name).to eq("Molejo")
2334
+ end
2335
+ end
2336
+
2337
+ context "when using autobuilding" do
2338
+ before do
2339
+ InterceptablePlane.create!.tap do |plane|
2340
+ plane.wings.create!
2341
+ end
2342
+ end
2343
+
2344
+ let(:plane) { InterceptablePlane.first }
2345
+ let(:wing) { InterceptableWing.first }
2346
+ let(:engine) { wing.engine }
2347
+
2348
+ it "sets the defaults correctly" do
2349
+ expect(wing._id).to eq("hello-wing")
2350
+ expect(wing.p_id).to eq(plane._id.to_s)
2351
+ expect(wing.e_id).to eq(engine._id.to_s)
2352
+ expect(engine._id).to eq("hello-engine-#{wing.id}")
2353
+ end
2354
+ end
2355
+ end
2356
+
2357
+ # This case is rather niche. The _ids method used to use the `.only` method
2358
+ # to get only the _ids for an association, which was causing a
2359
+ # MissingAttributeError to be raised when accessing another association. This
2360
+ # was fixed by using `.pluck` over `.only`. Look at MONGOID-5306 for a more
2361
+ # detailed explanation.
2362
+ context "when accessing _ids in validate and access an association in after_initialize" do
2363
+ it "doesn't raise a MissingAttributeError" do
2364
+ company = InterceptableCompany.create!
2365
+ shop = InterceptableShop.create!(company: company)
2366
+ user = InterceptableUser.new
2367
+ user.company = company
2368
+ expect do
2369
+ user.save!
2370
+ end.to_not raise_error(ActiveModel::MissingAttributeError)
2371
+ end
2372
+ end
1794
2373
  end