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
@@ -7,11 +7,15 @@ describe 'Queries with Range criteria' do
7
7
  let(:now_in_zone) { now_utc.in_time_zone('Asia/Tokyo') }
8
8
  let(:today) { Date.new(2020, 1, 1) }
9
9
 
10
+ let(:labels) do
11
+ [ Label.new(age: 12), Label.new(age: 16) ]
12
+ end
13
+
10
14
  let!(:band1) { Band.create!(likes: 0, rating: 0.9, founded: today, updated_at: now_utc) }
11
15
  let!(:band2) { Band.create!(likes: 1, rating: 1.0, founded: today + 1.day, updated_at: now_utc + 1.days) }
12
16
  let!(:band3) { Band.create!(likes: 2, rating: 2.9, founded: today + 2.days, updated_at: now_utc + 2.days) }
13
17
  let!(:band4) { Band.create!(likes: 3, rating: 3.0, founded: today + 3.days, updated_at: now_utc + 3.days) }
14
- let!(:band5) { Band.create!(likes: 4, rating: 3.1, founded: today + 4.days, updated_at: now_utc + 4.days) }
18
+ let!(:band5) { Band.create!(likes: 4, rating: 3.1, founded: today + 4.days, updated_at: now_utc + 4.days, labels: labels) }
15
19
 
16
20
  context 'Range<Integer> criteria vs Integer field' do
17
21
 
@@ -262,4 +266,94 @@ describe 'Queries with Range criteria' do
262
266
  end
263
267
  end
264
268
  end
269
+
270
+ context 'Range<Integer> criteria vs embedded Integer field' do
271
+
272
+ it 'returns objects within the range' do
273
+ expect(Band.where("labels.age" => 10..18).to_a).to eq [band5]
274
+ expect(Band.where("labels.age" => 13...16).to_a).to eq []
275
+ end
276
+
277
+ it "does not return objects out of range" do
278
+ expect(Band.where("labels.age" => 13..14).to_a).to eq []
279
+ end
280
+
281
+ context 'endless range' do
282
+ ruby_version_gte '2.6'
283
+
284
+ it 'returns all objects above the value' do
285
+ expect(Band.where("labels.age": eval('1..')).to_a).to eq [band5]
286
+ end
287
+
288
+ it 'does not return the objects under the value' do
289
+ expect(Band.where("labels.age": eval('100..')).to_a).to eq []
290
+ end
291
+ end
292
+
293
+ context 'beginless range' do
294
+ ruby_version_gte '2.7'
295
+
296
+ it 'returns all objects under the value' do
297
+ expect(Band.where("labels.age": eval('..16')).to_a).to eq [band5]
298
+ end
299
+ it 'does not return the objects above the value' do
300
+ expect(Band.where("labels.age": eval('...12')).to_a).to eq []
301
+ end
302
+ end
303
+ end
304
+
305
+ context 'Range<Integer> criteria vs Array<Integer>' do
306
+ let!(:band6) { Band.create!(genres: [12, 16]) }
307
+
308
+ it 'returns objects within the range' do
309
+ expect(Band.where("genres" => 10..18).to_a).to eq [band6]
310
+ end
311
+
312
+ it "does not return objects out of range" do
313
+ expect(Band.where("genres" => 13..14).to_a).to eq []
314
+ end
315
+ end
316
+
317
+ context 'Range<Integer> criteria vs aliased Array<Integer>' do
318
+ let!(:person) { Person.create!(array: [12, 16]) }
319
+
320
+ it 'returns objects within the range' do
321
+ expect(Person.where("array" => 10..18).to_a).to eq [person]
322
+ end
323
+
324
+ it "does not return objects out of range" do
325
+ expect(Person.where("array" => 13..14).to_a).to eq []
326
+ end
327
+ end
328
+
329
+ context 'Range<Integer> criteria vs Array<Hash<Symbol, Integer>>' do
330
+ let!(:band6) { Band.create!(genres: [{x: 12}, {x: 16}]) }
331
+
332
+ it 'returns objects within the range' do
333
+ expect(Band.where("genres.x" => 10..18).to_a).to eq [band6]
334
+ end
335
+
336
+ it "does not return objects out of range" do
337
+ expect(Band.where("genres.x" => 13..14).to_a).to eq []
338
+ end
339
+ end
340
+
341
+ context 'Range<Integer> criteria vs aliased/doubly embedded Integer' do
342
+ let!(:person) do
343
+ Person.create!(passport: Passport.new).tap do |b|
344
+ b.passport.passport_pages.create!(num_stamps: 12)
345
+ b.passport.passport_pages.create!(num_stamps: 16)
346
+ end
347
+ end
348
+
349
+ config_override :broken_alias_handling, false
350
+
351
+ it 'returns objects within the range' do
352
+ expect(Person.where("passport.passport_pages.num_stamps" => 10..18).to_a).to eq [person]
353
+ end
354
+
355
+ it "does not return objects out of range" do
356
+ expect(Person.where("passport.passport_pages.num_stamps" => 13..14).to_a).to eq []
357
+ end
358
+ end
265
359
  end
@@ -2,17 +2,17 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe "#discriminator_key" do
5
+ describe "#discriminator_key" do
6
6
 
7
7
  context "when the discriminator key is not set on a class" do
8
- let(:piano) do
8
+ let(:piano) do
9
9
  Piano.new
10
10
  end
11
11
 
12
- let(:guitar) do
12
+ let(:guitar) do
13
13
  Guitar.new
14
- end
15
-
14
+ end
15
+
16
16
  it "sets the child discriminator key to _type: Piano" do
17
17
  expect(piano._type).to eq("Piano")
18
18
  end
@@ -21,7 +21,7 @@ describe "#discriminator_key" do
21
21
  expect(guitar._type).to eq("Guitar")
22
22
  end
23
23
  end
24
-
24
+
25
25
  context "when the discriminator key is changed in the parent" do
26
26
  before do
27
27
  Instrument.discriminator_key = "hello2"
@@ -31,11 +31,11 @@ describe "#discriminator_key" do
31
31
  Instrument.discriminator_key = nil
32
32
  end
33
33
 
34
- let(:piano) do
34
+ let(:piano) do
35
35
  Piano.new
36
36
  end
37
37
 
38
- let(:guitar) do
38
+ let(:guitar) do
39
39
  Guitar.new
40
40
  end
41
41
 
@@ -54,22 +54,24 @@ describe "#discriminator_key" do
54
54
  class GlobalIntDiscriminatorParent
55
55
  include Mongoid::Document
56
56
  end
57
-
57
+
58
58
  class GlobalIntDiscriminatorChild < GlobalIntDiscriminatorParent
59
59
  end
60
-
60
+
61
61
  Mongoid.discriminator_key = "test"
62
62
  end
63
63
 
64
64
  after do
65
65
  Mongoid.discriminator_key = "_type"
66
+ Object.send(:remove_const, :GlobalIntDiscriminatorParent)
67
+ Object.send(:remove_const, :GlobalIntDiscriminatorChild)
66
68
  end
67
69
 
68
- let(:child) do
70
+ let(:child) do
69
71
  GlobalIntDiscriminatorChild.new
70
72
  end
71
73
 
72
- it "has a discriminator key _type" do
74
+ it "has a discriminator key _type" do
73
75
  expect(child._type).to eq("GlobalIntDiscriminatorChild")
74
76
  end
75
77
 
@@ -85,30 +87,32 @@ describe "#discriminator_key" do
85
87
  class PreGlobalIntDiscriminatorParent
86
88
  include Mongoid::Document
87
89
  end
88
-
90
+
89
91
  class PreGlobalIntDiscriminatorChild < PreGlobalIntDiscriminatorParent
90
92
  end
91
93
  end
92
94
 
93
95
  after do
94
96
  Mongoid.discriminator_key = "_type"
97
+ Object.send(:remove_const, :PreGlobalIntDiscriminatorParent)
98
+ Object.send(:remove_const, :PreGlobalIntDiscriminatorChild)
95
99
  end
96
100
 
97
- let(:child) do
101
+ let(:child) do
98
102
  PreGlobalIntDiscriminatorChild.new
99
103
  end
100
104
 
101
- it "creates a field with new discriminator key" do
105
+ it "creates a field with new discriminator key" do
102
106
  expect(child.test).to eq("PreGlobalIntDiscriminatorChild")
103
107
  end
104
-
108
+
105
109
  it "does not have the default value as a field" do
106
110
  expect(defined?(child._type)).to be nil
107
111
  end
108
112
  end
109
113
  end
110
114
 
111
- context "when the discriminator key is changed in the parent" do
115
+ context "when the discriminator key is changed in the parent" do
112
116
  context "after child class creation" do
113
117
  before do
114
118
  class LocalIntDiscriminatorParent
@@ -121,15 +125,20 @@ describe "#discriminator_key" do
121
125
  LocalIntDiscriminatorParent.discriminator_key = "test2"
122
126
  end
123
127
 
124
- let(:child) do
128
+ after do
129
+ Object.send(:remove_const, :LocalIntDiscriminatorParent)
130
+ Object.send(:remove_const, :LocalIntDiscriminatorChild)
131
+ end
132
+
133
+ let(:child) do
125
134
  LocalIntDiscriminatorChild.new
126
135
  end
127
136
 
128
- it "still has _type field in the child" do
137
+ it "still has _type field in the child" do
129
138
  expect(child._type).to include("LocalIntDiscriminatorChild")
130
139
  end
131
140
 
132
- it "has the new field in the child" do
141
+ it "has the new field in the child" do
133
142
  expect(child.test2).to include("LocalIntDiscriminatorChild")
134
143
  end
135
144
  end
@@ -145,11 +154,16 @@ describe "#discriminator_key" do
145
154
  end
146
155
  end
147
156
 
148
- let(:child) do
157
+ after do
158
+ Object.send(:remove_const, :PreLocalIntDiscriminatorParent)
159
+ Object.send(:remove_const, :PreLocalIntDiscriminatorChild)
160
+ end
161
+
162
+ let(:child) do
149
163
  PreLocalIntDiscriminatorChild.new
150
164
  end
151
165
 
152
- it "creates a new field in the child" do
166
+ it "creates a new field in the child" do
153
167
  expect(child.test2).to include("PreLocalIntDiscriminatorChild")
154
168
  end
155
169
 
@@ -159,9 +173,9 @@ describe "#discriminator_key" do
159
173
  end
160
174
  end
161
175
 
162
- context "when adding to the db" do
176
+ context "when adding to the db" do
163
177
  context "when changing the discriminator_key" do
164
- before do
178
+ before do
165
179
  class DBDiscriminatorParent
166
180
  include Mongoid::Document
167
181
  self.discriminator_key = "dkey"
@@ -171,34 +185,44 @@ describe "#discriminator_key" do
171
185
  end
172
186
  DBDiscriminatorChild.create!
173
187
  end
174
-
175
- it "has the correct count" do
188
+
189
+ after do
190
+ Object.send(:remove_const, :DBDiscriminatorParent)
191
+ Object.send(:remove_const, :DBDiscriminatorChild)
192
+ end
193
+
194
+ it "has the correct count" do
176
195
  expect(DBDiscriminatorChild.count).to eq(1)
177
196
  end
178
197
 
179
- it "has the correct count in the parent" do
198
+ it "has the correct count in the parent" do
180
199
  expect(DBDiscriminatorParent.count).to eq(1)
181
200
  end
182
201
  end
183
202
 
184
203
  context "when changing the discriminator_key after saving to the db" do
185
- before do
204
+ before do
186
205
  class DBDiscriminatorParent
187
206
  include Mongoid::Document
188
207
  end
189
-
208
+
190
209
  class DBDiscriminatorChild < DBDiscriminatorParent
191
210
  end
192
211
  DBDiscriminatorChild.create!
193
212
  DBDiscriminatorParent.discriminator_key = "dkey2"
194
213
  DBDiscriminatorChild.create!
195
214
  end
196
-
197
- it "only finds the documents with the new discriminator key" do
215
+
216
+ after do
217
+ Object.send(:remove_const, :DBDiscriminatorParent)
218
+ Object.send(:remove_const, :DBDiscriminatorChild)
219
+ end
220
+
221
+ it "only finds the documents with the new discriminator key" do
198
222
  expect(DBDiscriminatorChild.count).to eq(1)
199
223
  end
200
224
 
201
- it "has the correct count in the parent" do
225
+ it "has the correct count in the parent" do
202
226
  expect(DBDiscriminatorParent.count).to eq(2)
203
227
  end
204
228
  end
@@ -213,45 +237,51 @@ describe "#discriminator_key" do
213
237
  field :x, type: Integer
214
238
  field :y, type: Integer
215
239
  embedded_in :canvas
216
-
240
+
217
241
  self.discriminator_key = "shape_type"
218
242
  end
219
-
243
+
220
244
  class Example1Circle < Example1Shape
221
245
  field :radius, type: Float
222
246
  end
223
-
247
+
224
248
  class Example1Rectangle < Example1Shape
225
249
  field :width, type: Float
226
250
  field :height, type: Float
227
251
  end
228
252
  end
229
-
230
- let(:rectangle) do
253
+
254
+ after do
255
+ Object.send(:remove_const, :Example1Shape)
256
+ Object.send(:remove_const, :Example1Circle)
257
+ Object.send(:remove_const, :Example1Rectangle)
258
+ end
259
+
260
+ let(:rectangle) do
231
261
  Example1Rectangle.new
232
262
  end
233
-
234
- let(:circle) do
263
+
264
+ let(:circle) do
235
265
  Example1Circle.new
236
266
  end
237
-
238
- it "has the new discriminator key: Rectangle" do
267
+
268
+ it "has the new discriminator key: Rectangle" do
239
269
  expect(rectangle.shape_type).to eq("Example1Rectangle")
240
270
  end
241
-
271
+
242
272
  it "does not have the default discriminator key: Rectangle" do
243
273
  expect(defined?(rectangle._type)).to be nil
244
274
  end
245
-
246
- it "has the new discriminator key: Circle" do
275
+
276
+ it "has the new discriminator key: Circle" do
247
277
  expect(circle.shape_type).to eq("Example1Circle")
248
278
  end
249
-
279
+
250
280
  it "does not have the default discriminator key: Circle" do
251
281
  expect(defined?(circle._type)).to be nil
252
282
  end
253
283
  end
254
-
284
+
255
285
  context "Example 2" do
256
286
  before do
257
287
  class Example2Shape
@@ -260,91 +290,100 @@ describe "#discriminator_key" do
260
290
  field :y, type: Integer
261
291
  embedded_in :canvas
262
292
  end
263
-
293
+
264
294
  class Example2Circle < Example2Shape
265
295
  field :radius, type: Float
266
296
  end
267
-
297
+
268
298
  class Example2Rectangle < Example2Shape
269
299
  field :width, type: Float
270
300
  field :height, type: Float
271
301
  end
272
-
302
+
273
303
  Example2Shape.discriminator_key = "shape_type"
274
304
  end
275
-
276
- let(:rectangle) do
305
+
306
+ after do
307
+ Example2Shape.discriminator_key = "_type"
308
+ Object.send(:remove_const, :Example2Shape)
309
+ Object.send(:remove_const, :Example2Circle)
310
+ Object.send(:remove_const, :Example2Rectangle)
311
+ end
312
+
313
+ let(:rectangle) do
277
314
  Example2Rectangle.new
278
315
  end
279
-
280
- let(:circle) do
316
+
317
+ let(:circle) do
281
318
  Example2Circle.new
282
319
  end
283
-
284
- it "has the new discriminator key: Rectangle" do
320
+
321
+ it "has the new discriminator key: Rectangle" do
285
322
  expect(rectangle.shape_type).to eq("Example2Rectangle")
286
323
  end
287
-
324
+
288
325
  it "has default discriminator key: Rectangle" do
289
326
  expect(rectangle._type).to eq("Example2Rectangle")
290
327
  end
291
-
292
- it "has the new discriminator key: Circle" do
328
+
329
+ it "has the new discriminator key: Circle" do
293
330
  expect(circle.shape_type).to eq("Example2Circle")
294
331
  end
295
-
332
+
296
333
  it "has default discriminator key: Circle" do
297
334
  expect(circle._type).to eq("Example2Circle")
298
335
  end
299
336
  end
300
-
337
+
301
338
  context "Example 3" do
302
339
  before do
303
-
304
340
  Mongoid.discriminator_key = "shape_type"
305
-
341
+
306
342
  class Example3Shape
307
343
  include Mongoid::Document
308
344
  field :x, type: Integer
309
345
  field :y, type: Integer
310
346
  embedded_in :canvas
311
347
  end
312
-
348
+
313
349
  class Example3Circle < Example3Shape
314
350
  field :radius, type: Float
315
351
  end
316
-
352
+
317
353
  class Example3Rectangle < Example3Shape
318
354
  field :width, type: Float
319
355
  field :height, type: Float
320
356
  end
321
-
357
+
322
358
  end
323
359
 
324
- after do
360
+ after do
325
361
  Mongoid.discriminator_key = "_type"
362
+ Object.send(:remove_const, :Example3Shape)
363
+ Object.send(:remove_const, :Example3Circle)
364
+ Object.send(:remove_const, :Example3Rectangle)
326
365
  end
327
-
328
- let(:rectangle) do
366
+
367
+ let(:rectangle) do
329
368
  Example3Rectangle.new
330
369
  end
331
-
332
- let(:circle) do
370
+
371
+ let(:circle) do
333
372
  Example3Circle.new
334
373
  end
335
-
336
- it "has the new discriminator key: Rectangle" do
374
+
375
+ it "has the new discriminator key: Rectangle" do
337
376
  expect(rectangle.shape_type).to eq("Example3Rectangle")
338
377
  end
339
-
378
+
340
379
  it "does not have the default discriminator key: Rectangle" do
341
380
  expect(defined?(rectangle._type)).to be nil
342
381
  end
343
-
344
- it "has the new discriminator key: Circle" do
382
+
383
+ it "has the new discriminator key: Circle" do
345
384
  expect(circle.shape_type).to eq("Example3Circle")
346
385
  end
347
-
386
+
348
387
  it "does not have the default discriminator key: Circle" do
349
388
  expect(defined?(circle._type)).to be nil
350
389
  end