mongoid 8.0.10 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +3 -3
  4. data/README.md +3 -3
  5. data/Rakefile +18 -67
  6. data/lib/config/locales/en.yml +46 -14
  7. data/lib/mongoid/association/accessors.rb +3 -7
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/eager_loadable.rb +0 -3
  10. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  11. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  12. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  13. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  14. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  15. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  16. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
  17. data/lib/mongoid/association/macros.rb +0 -6
  18. data/lib/mongoid/association/nested/one.rb +40 -2
  19. data/lib/mongoid/association/proxy.rb +1 -1
  20. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  21. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  22. data/lib/mongoid/association/referenced/has_many/enumerable.rb +6 -23
  23. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  24. data/lib/mongoid/association/reflections.rb +2 -2
  25. data/lib/mongoid/atomic.rb +7 -16
  26. data/lib/mongoid/attributes/dynamic.rb +1 -1
  27. data/lib/mongoid/attributes/nested.rb +2 -2
  28. data/lib/mongoid/attributes/processing.rb +5 -29
  29. data/lib/mongoid/attributes/projector.rb +1 -1
  30. data/lib/mongoid/attributes/readonly.rb +1 -1
  31. data/lib/mongoid/attributes.rb +8 -2
  32. data/lib/mongoid/changeable.rb +107 -5
  33. data/lib/mongoid/clients/storage_options.rb +2 -5
  34. data/lib/mongoid/clients/validators/storage.rb +1 -13
  35. data/lib/mongoid/collection_configurable.rb +58 -0
  36. data/lib/mongoid/composable.rb +2 -0
  37. data/lib/mongoid/config/defaults.rb +60 -0
  38. data/lib/mongoid/config/options.rb +0 -3
  39. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  40. data/lib/mongoid/config/validators.rb +1 -0
  41. data/lib/mongoid/config.rb +88 -27
  42. data/lib/mongoid/contextual/atomic.rb +1 -1
  43. data/lib/mongoid/contextual/memory.rb +233 -33
  44. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  45. data/lib/mongoid/contextual/mongo.rb +370 -133
  46. data/lib/mongoid/contextual/none.rb +162 -7
  47. data/lib/mongoid/contextual.rb +12 -0
  48. data/lib/mongoid/criteria/findable.rb +2 -2
  49. data/lib/mongoid/criteria/includable.rb +4 -3
  50. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +1 -15
  51. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  52. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
  53. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  54. data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
  55. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  56. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  57. data/lib/mongoid/criteria.rb +6 -5
  58. data/lib/mongoid/deprecable.rb +1 -2
  59. data/lib/mongoid/deprecation.rb +3 -3
  60. data/lib/mongoid/document.rb +1 -8
  61. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  62. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  63. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  64. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  65. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  66. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  67. data/lib/mongoid/errors.rb +4 -1
  68. data/lib/mongoid/extensions/hash.rb +2 -24
  69. data/lib/mongoid/extensions/object.rb +2 -2
  70. data/lib/mongoid/extensions/time.rb +2 -0
  71. data/lib/mongoid/fields/localized.rb +10 -0
  72. data/lib/mongoid/fields/standard.rb +10 -0
  73. data/lib/mongoid/fields.rb +59 -35
  74. data/lib/mongoid/findable.rb +27 -3
  75. data/lib/mongoid/interceptable.rb +6 -116
  76. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  77. data/lib/mongoid/matcher/type.rb +1 -1
  78. data/lib/mongoid/persistable/creatable.rb +1 -0
  79. data/lib/mongoid/persistable/deletable.rb +1 -1
  80. data/lib/mongoid/persistable/savable.rb +13 -1
  81. data/lib/mongoid/persistable/unsettable.rb +2 -2
  82. data/lib/mongoid/persistable/updatable.rb +51 -1
  83. data/lib/mongoid/persistable/upsertable.rb +20 -1
  84. data/lib/mongoid/persistable.rb +3 -0
  85. data/lib/mongoid/query_cache.rb +5 -1
  86. data/lib/mongoid/railties/database.rake +7 -2
  87. data/lib/mongoid/reloadable.rb +5 -3
  88. data/lib/mongoid/stateful.rb +22 -1
  89. data/lib/mongoid/tasks/database.rake +12 -0
  90. data/lib/mongoid/tasks/database.rb +20 -0
  91. data/lib/mongoid/timestamps/created.rb +1 -8
  92. data/lib/mongoid/traversable.rb +0 -12
  93. data/lib/mongoid/utils.rb +22 -0
  94. data/lib/mongoid/validatable/associated.rb +17 -98
  95. data/lib/mongoid/validatable/macros.rb +5 -5
  96. data/lib/mongoid/validatable.rb +4 -9
  97. data/lib/mongoid/version.rb +1 -1
  98. data/lib/mongoid/warnings.rb +17 -1
  99. data/lib/mongoid.rb +16 -3
  100. data/spec/integration/app_spec.rb +2 -6
  101. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +0 -40
  102. data/spec/integration/callbacks_spec.rb +99 -12
  103. data/spec/integration/discriminator_key_spec.rb +4 -5
  104. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  105. data/spec/mongoid/association/eager_spec.rb +2 -24
  106. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  107. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  108. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  109. data/spec/mongoid/association/embedded/embeds_many_query_spec.rb +0 -4
  110. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  111. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  112. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +42 -55
  113. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  114. data/spec/mongoid/association/syncable_spec.rb +1 -1
  115. data/spec/mongoid/association_spec.rb +0 -60
  116. data/spec/mongoid/attributes_spec.rb +3 -33
  117. data/spec/mongoid/changeable_spec.rb +299 -24
  118. data/spec/mongoid/clients_spec.rb +122 -13
  119. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  120. data/spec/mongoid/config/defaults_spec.rb +160 -0
  121. data/spec/mongoid/config_spec.rb +154 -27
  122. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  123. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  124. data/spec/mongoid/contextual/mongo_spec.rb +1009 -125
  125. data/spec/mongoid/contextual/none_spec.rb +49 -2
  126. data/spec/mongoid/copyable_spec.rb +2 -10
  127. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  128. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  129. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  130. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  131. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  132. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  133. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  134. data/spec/mongoid/criteria_spec.rb +5 -9
  135. data/spec/mongoid/document_spec.rb +0 -27
  136. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  137. data/spec/mongoid/extensions/hash_spec.rb +3 -3
  138. data/spec/mongoid/extensions/time_spec.rb +8 -43
  139. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  140. data/spec/mongoid/fields/localized_spec.rb +46 -28
  141. data/spec/mongoid/fields_spec.rb +136 -77
  142. data/spec/mongoid/findable_spec.rb +391 -34
  143. data/spec/mongoid/indexable_spec.rb +16 -10
  144. data/spec/mongoid/interceptable_spec.rb +153 -442
  145. data/spec/mongoid/interceptable_spec_models.rb +111 -51
  146. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  147. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  148. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  149. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  150. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  151. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  152. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  153. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  154. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  155. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  156. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  157. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  158. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  159. data/spec/mongoid/persistence_context_spec.rb +7 -57
  160. data/spec/mongoid/query_cache_spec.rb +56 -61
  161. data/spec/mongoid/reloadable_spec.rb +24 -28
  162. data/spec/mongoid/scopable_spec.rb +70 -0
  163. data/spec/mongoid/serializable_spec.rb +23 -44
  164. data/spec/mongoid/stateful_spec.rb +122 -8
  165. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  166. data/spec/mongoid/tasks/database_spec.rb +127 -0
  167. data/spec/mongoid/timestamps/created_spec.rb +0 -23
  168. data/spec/mongoid/timestamps_spec.rb +9 -11
  169. data/spec/mongoid/touchable_spec.rb +277 -5
  170. data/spec/mongoid/touchable_spec_models.rb +3 -1
  171. data/spec/mongoid/traversable_spec.rb +9 -24
  172. data/spec/mongoid/validatable/associated_spec.rb +34 -27
  173. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  174. data/spec/mongoid_spec.rb +36 -10
  175. data/spec/shared/LICENSE +20 -0
  176. data/spec/shared/bin/get-mongodb-download-url +17 -0
  177. data/spec/shared/bin/s3-copy +45 -0
  178. data/spec/shared/bin/s3-upload +69 -0
  179. data/spec/shared/lib/mrss/child_process_helper.rb +80 -0
  180. data/spec/shared/lib/mrss/cluster_config.rb +231 -0
  181. data/spec/shared/lib/mrss/constraints.rb +378 -0
  182. data/spec/shared/lib/mrss/docker_runner.rb +298 -0
  183. data/spec/shared/lib/mrss/eg_config_utils.rb +51 -0
  184. data/spec/shared/lib/mrss/event_subscriber.rb +210 -0
  185. data/spec/shared/lib/mrss/lite_constraints.rb +238 -0
  186. data/spec/shared/lib/mrss/server_version_registry.rb +113 -0
  187. data/spec/shared/lib/mrss/session_registry.rb +69 -0
  188. data/spec/shared/lib/mrss/session_registry_legacy.rb +60 -0
  189. data/spec/shared/lib/mrss/spec_organizer.rb +179 -0
  190. data/spec/shared/lib/mrss/utils.rb +37 -0
  191. data/spec/shared/share/Dockerfile.erb +321 -0
  192. data/spec/shared/share/haproxy-1.conf +16 -0
  193. data/spec/shared/share/haproxy-2.conf +17 -0
  194. data/spec/shared/shlib/config.sh +27 -0
  195. data/spec/shared/shlib/distro.sh +74 -0
  196. data/spec/shared/shlib/server.sh +416 -0
  197. data/spec/shared/shlib/set_env.sh +169 -0
  198. data/spec/spec_helper.rb +5 -0
  199. data/spec/support/immutable_ids.rb +118 -0
  200. data/spec/support/macros.rb +47 -15
  201. data/spec/support/models/artist.rb +0 -1
  202. data/spec/support/models/band.rb +1 -0
  203. data/spec/support/models/building.rb +2 -0
  204. data/spec/support/models/name.rb +0 -10
  205. data/spec/support/models/person.rb +0 -1
  206. data/spec/support/models/product.rb +1 -0
  207. data.tar.gz.sig +0 -0
  208. metadata +745 -637
  209. metadata.gz.sig +2 -0
  210. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  211. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
  212. data/spec/support/models/purse.rb +0 -9
@@ -81,16 +81,12 @@ describe Mongoid::Fields::Localized do
81
81
  end
82
82
 
83
83
  context "when a locale is provided" do
84
+ with_default_i18n_configs
84
85
 
85
86
  before do
86
- I18n.enforce_available_locales = false
87
87
  ::I18n.locale = :de
88
88
  end
89
89
 
90
- after do
91
- ::I18n.locale = :en
92
- end
93
-
94
90
  context "when the value exists" do
95
91
 
96
92
  let(:value) do
@@ -128,6 +124,7 @@ describe Mongoid::Fields::Localized do
128
124
  context "when the value does not exist" do
129
125
 
130
126
  context "when not using fallbacks" do
127
+ require_no_fallbacks
131
128
 
132
129
  let(:value) do
133
130
  field.demongoize({ "en" => "testing" })
@@ -139,8 +136,7 @@ describe Mongoid::Fields::Localized do
139
136
  end
140
137
 
141
138
  context "when using fallbacks" do
142
-
143
- with_i18n_fallbacks
139
+ require_fallbacks
144
140
 
145
141
  context "when fallbacks are defined" do
146
142
 
@@ -271,15 +267,12 @@ describe Mongoid::Fields::Localized do
271
267
  end
272
268
 
273
269
  context "when a locale is provided" do
270
+ with_default_i18n_configs
274
271
 
275
272
  before do
276
273
  ::I18n.locale = :de
277
274
  end
278
275
 
279
- after do
280
- ::I18n.locale = :en
281
- end
282
-
283
276
  context "when the value exists" do
284
277
 
285
278
  let(:value) do
@@ -305,10 +298,10 @@ describe Mongoid::Fields::Localized do
305
298
  end
306
299
 
307
300
  context "when using fallbacks" do
308
-
309
- with_i18n_fallbacks
301
+ require_fallbacks
310
302
 
311
303
  context "when fallbacks are defined" do
304
+ with_default_i18n_configs
312
305
 
313
306
  before do
314
307
  ::I18n.fallbacks[:de] = [ :de, :en, :es ]
@@ -367,6 +360,7 @@ describe Mongoid::Fields::Localized do
367
360
  end
368
361
 
369
362
  context "when no fallbacks are defined" do
363
+ with_default_i18n_configs
370
364
 
371
365
  before do
372
366
  ::I18n.fallbacks[:de] = [ :de ]
@@ -406,15 +400,12 @@ describe Mongoid::Fields::Localized do
406
400
  end
407
401
 
408
402
  context "when a locale is provided" do
403
+ with_default_i18n_configs
409
404
 
410
405
  before do
411
406
  ::I18n.locale = :de
412
407
  end
413
408
 
414
- after do
415
- ::I18n.locale = :en
416
- end
417
-
418
409
  let(:value) do
419
410
  field.mongoize("This is a test")
420
411
  end
@@ -443,15 +434,12 @@ describe Mongoid::Fields::Localized do
443
434
  end
444
435
 
445
436
  context "when a locale is provided" do
437
+ with_default_i18n_configs
446
438
 
447
439
  before do
448
440
  ::I18n.locale = :de
449
441
  end
450
442
 
451
- after do
452
- ::I18n.locale = :en
453
- end
454
-
455
443
  let(:value) do
456
444
  field.mongoize("100")
457
445
  end
@@ -462,16 +450,12 @@ describe Mongoid::Fields::Localized do
462
450
  end
463
451
 
464
452
  context 'when the type is Boolean' do
453
+ with_default_i18n_configs
465
454
 
466
455
  before do
467
- I18n.enforce_available_locales = false
468
456
  ::I18n.locale = :de
469
457
  end
470
458
 
471
- after do
472
- ::I18n.locale = :en
473
- end
474
-
475
459
  context "when the value is false" do
476
460
 
477
461
  let(:field) do
@@ -503,12 +487,12 @@ describe Mongoid::Fields::Localized do
503
487
  end
504
488
 
505
489
  context "when fallbacks are defined" do
506
-
507
- with_i18n_fallbacks
490
+ require_fallbacks
508
491
 
509
492
  context "when the lookup does not need to use fallbacks" do
510
493
 
511
494
  context "when the value is false" do
495
+ with_default_i18n_configs
512
496
 
513
497
  before do
514
498
  ::I18n.fallbacks[:de] = [:en, :es]
@@ -528,6 +512,7 @@ describe Mongoid::Fields::Localized do
528
512
  end
529
513
 
530
514
  context "when the value is true" do
515
+ with_default_i18n_configs
531
516
 
532
517
  before do
533
518
  ::I18n.fallbacks[:de] = [:en, :es]
@@ -550,4 +535,37 @@ describe Mongoid::Fields::Localized do
550
535
  end
551
536
  end
552
537
  end
538
+
539
+ describe "localize: :present" do
540
+
541
+ let(:field) do
542
+ described_class.new(:description, localize: :present, type: String)
543
+ end
544
+
545
+ context "when setting the localize to present" do
546
+
547
+ it "is localized?" do
548
+ expect(field.localized?).to be true
549
+ end
550
+
551
+ it "is localize_present?" do
552
+ expect(field.localize_present?).to be true
553
+ end
554
+ end
555
+
556
+ context "when localize is not localize_present" do
557
+
558
+ let(:field) do
559
+ described_class.new(:description, localize: true, type: String)
560
+ end
561
+
562
+ it "is localized?" do
563
+ expect(field.localized?).to be true
564
+ end
565
+
566
+ it "is not localize_present?" do
567
+ expect(field.localize_present?).to be false
568
+ end
569
+ end
570
+ end
553
571
  end
@@ -14,18 +14,15 @@ describe Mongoid::Fields do
14
14
  context "when the field is localized" do
15
15
 
16
16
  context "when translations exist" do
17
+ with_default_i18n_configs
17
18
 
18
19
  before do
20
+ I18n.locale = :en
19
21
  product.description = "test"
20
- I18n.enforce_available_locales = false
21
- ::I18n.locale = :de
22
+ I18n.locale = :de
22
23
  product.description = "The best"
23
24
  end
24
25
 
25
- after do
26
- ::I18n.locale = :en
27
- end
28
-
29
26
  let(:translations) do
30
27
  product.description_translations
31
28
  end
@@ -527,16 +524,13 @@ describe Mongoid::Fields do
527
524
  end
528
525
 
529
526
  context "when a single locale is set" do
527
+ with_default_i18n_configs
530
528
 
531
529
  before do
532
- ::I18n.locale = :de
530
+ I18n.locale = :de
533
531
  product.description = "The best"
534
532
  end
535
533
 
536
- after do
537
- ::I18n.locale = :en
538
- end
539
-
540
534
  let(:description) do
541
535
  product.description
542
536
  end
@@ -547,17 +541,15 @@ describe Mongoid::Fields do
547
541
  end
548
542
 
549
543
  context "when multiple locales are set" do
544
+ with_default_i18n_configs
550
545
 
551
546
  before do
547
+ I18n.locale = :end
552
548
  product.description = "Cheap drinks"
553
- ::I18n.locale = :de
549
+ I18n.locale = :de
554
550
  product.description = "Cheaper drinks"
555
551
  end
556
552
 
557
- after do
558
- ::I18n.locale = :en
559
- end
560
-
561
553
  let(:description) do
562
554
  product.description
563
555
  end
@@ -567,49 +559,6 @@ describe Mongoid::Fields do
567
559
  end
568
560
  end
569
561
  end
570
-
571
- context 'when the field is declared as BSON::Decimal128' do
572
- let(:document) { Mop.create!(decimal128_field: BSON::Decimal128.new(Math::PI.to_s)).reload }
573
-
574
- shared_context 'BSON::Decimal128 is BigDecimal' do
575
- it 'should return a BigDecimal' do
576
- expect(document.decimal128_field).to be_a BigDecimal
577
- end
578
- end
579
-
580
- shared_context 'BSON::Decimal128 is BSON::Decimal128' do
581
- it 'should return a BSON::Decimal128' do
582
- expect(document.decimal128_field).to be_a BSON::Decimal128
583
- end
584
- end
585
-
586
- it 'is declared as BSON::Decimal128' do
587
- expect(Mop.fields['decimal128_field'].type).to be == BSON::Decimal128
588
- end
589
-
590
- context 'when BSON version <= 4' do
591
- max_bson_version '4.99.99'
592
- it_behaves_like 'BSON::Decimal128 is BSON::Decimal128'
593
- end
594
-
595
- context 'when BSON version >= 5' do
596
- min_bson_version '5.0.0'
597
-
598
- context 'when allow_bson5_decimal128 is false' do
599
- config_override :allow_bson5_decimal128, false
600
- it_behaves_like 'BSON::Decimal128 is BigDecimal'
601
- end
602
-
603
- context 'when allow_bson5_decimal128 is true' do
604
- config_override :allow_bson5_decimal128, true
605
- it_behaves_like 'BSON::Decimal128 is BSON::Decimal128'
606
- end
607
-
608
- context 'when allow_bson5_decimal128 is default' do
609
- it_behaves_like 'BSON::Decimal128 is BigDecimal'
610
- end
611
- end
612
- end
613
562
  end
614
563
 
615
564
  describe "#getter_before_type_cast" do
@@ -878,16 +827,13 @@ describe Mongoid::Fields do
878
827
  end
879
828
 
880
829
  context "when a locale is set" do
830
+ with_default_i18n_configs
881
831
 
882
832
  before do
883
- ::I18n.locale = :de
833
+ I18n.locale = :de
884
834
  product.description = "Cheaper drinks"
885
835
  end
886
836
 
887
- after do
888
- ::I18n.locale = :en
889
- end
890
-
891
837
  let(:description) do
892
838
  product.attributes["description"]
893
839
  end
@@ -898,17 +844,15 @@ describe Mongoid::Fields do
898
844
  end
899
845
 
900
846
  context "when having multiple locales" do
847
+ with_default_i18n_configs
901
848
 
902
849
  before do
850
+ I18n.locale = :en
903
851
  product.description = "Cheap drinks"
904
- ::I18n.locale = :de
852
+ I18n.locale = :de
905
853
  product.description = "Cheaper drinks"
906
854
  end
907
855
 
908
- after do
909
- ::I18n.locale = :en
910
- end
911
-
912
856
  let(:description) do
913
857
  product.attributes["description"]
914
858
  end
@@ -1110,14 +1054,7 @@ describe Mongoid::Fields do
1110
1054
  end
1111
1055
 
1112
1056
  context "when reading the field" do
1113
-
1114
- before do
1115
- Time.zone = "Berlin"
1116
- end
1117
-
1118
- after do
1119
- Time.zone = nil
1120
- end
1057
+ time_zone_override "Berlin"
1121
1058
 
1122
1059
  it "performs the necessary time conversions" do
1123
1060
  expect(person.lunch_time.to_s).to eq(time.getlocal.to_s)
@@ -2007,4 +1944,126 @@ describe Mongoid::Fields do
2007
1944
  end
2008
1945
  end
2009
1946
  end
1947
+
1948
+ describe "localize: :present" do
1949
+
1950
+ let(:product) do
1951
+ Product.new
1952
+ end
1953
+
1954
+ context "when assigning a non blank value" do
1955
+
1956
+ before do
1957
+ product.title = "hello"
1958
+ end
1959
+
1960
+ it "assigns the value" do
1961
+ expect(product.title).to eq("hello")
1962
+ end
1963
+
1964
+ it "populates the translations hash" do
1965
+ expect(product.title_translations).to eq({ "en" => "hello" })
1966
+ end
1967
+ end
1968
+
1969
+ context "when assigning an empty string" do
1970
+ with_default_i18n_configs
1971
+
1972
+ before do
1973
+ I18n.locale = :en
1974
+ product.title = "hello"
1975
+ I18n.locale = :de
1976
+ product.title = "hello there!"
1977
+ product.title = ""
1978
+ end
1979
+
1980
+ it "assigns the value" do
1981
+ expect(product.title).to eq(nil)
1982
+ end
1983
+
1984
+ it "populates the translations hash" do
1985
+ expect(product.title_translations).to eq({ "en" => "hello" })
1986
+ end
1987
+ end
1988
+
1989
+ context "when assigning nil" do
1990
+ with_default_i18n_configs
1991
+
1992
+ before do
1993
+ I18n.locale = :en
1994
+ product.title = "hello"
1995
+ I18n.locale = :de
1996
+ product.title = "hello there!"
1997
+ product.title = nil
1998
+ end
1999
+
2000
+ it "assigns the value" do
2001
+ expect(product.title).to eq(nil)
2002
+ end
2003
+
2004
+ it "populates the translations hash" do
2005
+ expect(product.title_translations).to eq({ "en" => "hello" })
2006
+ end
2007
+ end
2008
+
2009
+ context "when assigning an empty array" do
2010
+ with_default_i18n_configs
2011
+
2012
+ before do
2013
+ I18n.locale = :en
2014
+ product.title = "hello"
2015
+ I18n.locale = :de
2016
+ product.title = "hello there!"
2017
+ product.title = []
2018
+ end
2019
+
2020
+ it "assigns the value" do
2021
+ expect(product.title).to eq(nil)
2022
+ end
2023
+
2024
+ it "populates the translations hash" do
2025
+ expect(product.title_translations).to eq({ "en" => "hello" })
2026
+ end
2027
+ end
2028
+
2029
+ context "when assigning an empty string first" do
2030
+ with_default_i18n_configs
2031
+
2032
+ before do
2033
+ product.title = ""
2034
+ end
2035
+
2036
+ it "assigns the value" do
2037
+ expect(product.title).to eq(nil)
2038
+ end
2039
+
2040
+ it "populates the translations hash" do
2041
+ expect(product.title_translations).to eq({})
2042
+ end
2043
+ end
2044
+
2045
+ context "when assigning an empty string with only one translation" do
2046
+ with_default_i18n_configs
2047
+
2048
+ before do
2049
+ product.title = "Hello"
2050
+ product.title = ""
2051
+ product.save!
2052
+ end
2053
+
2054
+ let(:from_db) { Product.first }
2055
+
2056
+ it "assigns the value" do
2057
+ expect(product.title).to eq(nil)
2058
+ end
2059
+
2060
+ it "populates the translations hash" do
2061
+ expect(product.title_translations).to eq({})
2062
+ end
2063
+
2064
+ it "round trips an empty hash" do
2065
+ expect(from_db.title_translations).to eq({})
2066
+ end
2067
+ end
2068
+ end
2010
2069
  end