mongoid 8.0.4 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) 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/lib/config/locales/en.yml +46 -14
  6. data/lib/mongoid/association/accessors.rb +2 -2
  7. data/lib/mongoid/association/builders.rb +1 -1
  8. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  9. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  10. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  11. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  12. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  13. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  14. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
  15. data/lib/mongoid/association/nested/one.rb +40 -2
  16. data/lib/mongoid/association/proxy.rb +1 -1
  17. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  18. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  19. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  21. data/lib/mongoid/association/reflections.rb +2 -2
  22. data/lib/mongoid/attributes/dynamic.rb +1 -1
  23. data/lib/mongoid/attributes/nested.rb +2 -2
  24. data/lib/mongoid/attributes/projector.rb +1 -1
  25. data/lib/mongoid/attributes/readonly.rb +1 -1
  26. data/lib/mongoid/attributes.rb +8 -2
  27. data/lib/mongoid/changeable.rb +104 -4
  28. data/lib/mongoid/clients/storage_options.rb +2 -5
  29. data/lib/mongoid/clients/validators/storage.rb +1 -13
  30. data/lib/mongoid/collection_configurable.rb +58 -0
  31. data/lib/mongoid/composable.rb +2 -0
  32. data/lib/mongoid/config/defaults.rb +60 -0
  33. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  34. data/lib/mongoid/config/validators.rb +1 -0
  35. data/lib/mongoid/config.rb +101 -0
  36. data/lib/mongoid/contextual/atomic.rb +1 -1
  37. data/lib/mongoid/contextual/memory.rb +233 -33
  38. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  39. data/lib/mongoid/contextual/mongo.rb +373 -113
  40. data/lib/mongoid/contextual/none.rb +162 -7
  41. data/lib/mongoid/contextual.rb +12 -0
  42. data/lib/mongoid/criteria/findable.rb +2 -2
  43. data/lib/mongoid/criteria/includable.rb +4 -3
  44. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  45. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
  46. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  47. data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
  48. data/lib/mongoid/criteria.rb +6 -5
  49. data/lib/mongoid/deprecable.rb +1 -1
  50. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  51. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  52. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  53. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  54. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  55. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  56. data/lib/mongoid/errors.rb +4 -1
  57. data/lib/mongoid/extensions/object.rb +2 -2
  58. data/lib/mongoid/extensions/time.rb +2 -0
  59. data/lib/mongoid/fields/localized.rb +10 -0
  60. data/lib/mongoid/fields/standard.rb +10 -0
  61. data/lib/mongoid/fields.rb +69 -13
  62. data/lib/mongoid/findable.rb +27 -3
  63. data/lib/mongoid/interceptable.rb +7 -6
  64. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  65. data/lib/mongoid/matcher/type.rb +1 -1
  66. data/lib/mongoid/persistable/creatable.rb +1 -0
  67. data/lib/mongoid/persistable/deletable.rb +1 -1
  68. data/lib/mongoid/persistable/savable.rb +13 -1
  69. data/lib/mongoid/persistable/unsettable.rb +2 -2
  70. data/lib/mongoid/persistable/updatable.rb +51 -1
  71. data/lib/mongoid/persistable/upsertable.rb +20 -1
  72. data/lib/mongoid/persistable.rb +3 -0
  73. data/lib/mongoid/query_cache.rb +5 -1
  74. data/lib/mongoid/railties/database.rake +7 -2
  75. data/lib/mongoid/stateful.rb +22 -1
  76. data/lib/mongoid/tasks/database.rake +12 -0
  77. data/lib/mongoid/tasks/database.rb +20 -0
  78. data/lib/mongoid/utils.rb +22 -0
  79. data/lib/mongoid/validatable/macros.rb +5 -5
  80. data/lib/mongoid/validatable.rb +4 -1
  81. data/lib/mongoid/version.rb +1 -1
  82. data/lib/mongoid/warnings.rb +17 -1
  83. data/lib/mongoid.rb +16 -3
  84. data/spec/integration/app_spec.rb +2 -2
  85. data/spec/integration/callbacks_models.rb +37 -0
  86. data/spec/integration/callbacks_spec.rb +134 -0
  87. data/spec/integration/discriminator_key_spec.rb +4 -5
  88. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  89. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  90. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  91. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  92. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  93. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  94. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +5 -27
  95. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  96. data/spec/mongoid/association/syncable_spec.rb +1 -1
  97. data/spec/mongoid/attributes_spec.rb +3 -6
  98. data/spec/mongoid/changeable_spec.rb +299 -24
  99. data/spec/mongoid/clients_spec.rb +122 -13
  100. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  101. data/spec/mongoid/config/defaults_spec.rb +160 -0
  102. data/spec/mongoid/config_spec.rb +154 -18
  103. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  104. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  105. data/spec/mongoid/contextual/mongo_spec.rb +995 -36
  106. data/spec/mongoid/contextual/none_spec.rb +49 -2
  107. data/spec/mongoid/copyable_spec.rb +3 -11
  108. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  109. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  110. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  111. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  112. data/spec/mongoid/criteria/queryable/selector_spec.rb +1 -1
  113. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  114. data/spec/mongoid/criteria_spec.rb +5 -9
  115. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  116. data/spec/mongoid/extensions/time_spec.rb +8 -43
  117. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  118. data/spec/mongoid/fields/localized_spec.rb +46 -28
  119. data/spec/mongoid/fields_spec.rb +136 -34
  120. data/spec/mongoid/findable_spec.rb +391 -34
  121. data/spec/mongoid/indexable_spec.rb +16 -10
  122. data/spec/mongoid/interceptable_spec.rb +15 -3
  123. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  124. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  125. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  126. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  127. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  128. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  129. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  130. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  131. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  132. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  133. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  134. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  135. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  136. data/spec/mongoid/persistence_context_spec.rb +7 -57
  137. data/spec/mongoid/query_cache_spec.rb +56 -61
  138. data/spec/mongoid/reloadable_spec.rb +24 -4
  139. data/spec/mongoid/scopable_spec.rb +70 -0
  140. data/spec/mongoid/serializable_spec.rb +9 -30
  141. data/spec/mongoid/stateful_spec.rb +122 -8
  142. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  143. data/spec/mongoid/tasks/database_spec.rb +127 -0
  144. data/spec/mongoid/timestamps_spec.rb +9 -11
  145. data/spec/mongoid/touchable_spec.rb +277 -5
  146. data/spec/mongoid/touchable_spec_models.rb +3 -1
  147. data/spec/mongoid/traversable_spec.rb +9 -24
  148. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  149. data/spec/mongoid_spec.rb +35 -9
  150. data/spec/shared/lib/mrss/docker_runner.rb +7 -0
  151. data/spec/shared/lib/mrss/event_subscriber.rb +15 -5
  152. data/spec/shared/lib/mrss/lite_constraints.rb +10 -2
  153. data/spec/shared/lib/mrss/server_version_registry.rb +16 -23
  154. data/spec/shared/lib/mrss/utils.rb +28 -6
  155. data/spec/shared/share/Dockerfile.erb +36 -40
  156. data/spec/shared/shlib/server.sh +32 -8
  157. data/spec/shared/shlib/set_env.sh +4 -4
  158. data/spec/spec_helper.rb +5 -0
  159. data/spec/support/immutable_ids.rb +118 -0
  160. data/spec/support/macros.rb +47 -15
  161. data/spec/support/models/artist.rb +0 -1
  162. data/spec/support/models/band.rb +1 -0
  163. data/spec/support/models/book.rb +1 -0
  164. data/spec/support/models/building.rb +2 -0
  165. data/spec/support/models/cover.rb +10 -0
  166. data/spec/support/models/product.rb +1 -0
  167. data.tar.gz.sig +0 -0
  168. metadata +43 -7
  169. metadata.gz.sig +0 -0
  170. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  171. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
@@ -12,13 +12,13 @@ describe Mongoid::Errors::ReadonlyDocument do
12
12
 
13
13
  it "contains the problem in the message" do
14
14
  expect(error.message).to include(
15
- "Attempted to persist the readonly document 'Band'."
15
+ "Attempted to persist a readonly document of class 'Band'."
16
16
  )
17
17
  end
18
18
 
19
19
  it "contains the summary in the message" do
20
20
  expect(error.message).to include(
21
- "Documents loaded from the database using #only"
21
+ "Documents that are marked readonly cannot be persisted"
22
22
  )
23
23
  end
24
24
 
@@ -50,14 +50,7 @@ describe Mongoid::Extensions::Time do
50
50
 
51
51
  context "when using the ActiveSupport time zone" do
52
52
  config_override :use_activesupport_time_zone, true
53
-
54
- before do
55
- Time.zone = "Stockholm"
56
- end
57
-
58
- after do
59
- Time.zone = nil
60
- end
53
+ time_zone_override "Stockholm"
61
54
 
62
55
  context "when demongoizing a Time" do
63
56
 
@@ -123,19 +116,12 @@ describe Mongoid::Extensions::Time do
123
116
 
124
117
  context "when using the ActiveSupport time zone" do
125
118
  config_override :use_activesupport_time_zone, true
119
+ time_zone_override "Stockholm"
126
120
 
127
121
  let(:time) do
128
122
  Time.utc(2010, 11, 19, 0, 30)
129
123
  end
130
124
 
131
- before do
132
- Time.zone = "Stockholm"
133
- end
134
-
135
- after do
136
- Time.zone = nil
137
- end
138
-
139
125
  it "returns utc" do
140
126
  expect(Time.demongoize(time)).to eq(
141
127
  ActiveSupport::TimeZone['UTC'].local(2010, 11, 19, 0, 30)
@@ -855,20 +841,13 @@ describe Mongoid::Extensions::Time do
855
841
 
856
842
  context "when using the ActiveSupport time zone" do
857
843
  config_override :use_activesupport_time_zone, true
844
+ # if this is actually your time zone, the following tests are useless
845
+ time_zone_override "Stockholm"
858
846
 
859
847
  let(:datetime) do
860
848
  DateTime.new(2010, 11, 19)
861
849
  end
862
850
 
863
- before do
864
- # if this is actually your time zone, the following tests are useless
865
- Time.zone = "Stockholm"
866
- end
867
-
868
- after do
869
- Time.zone = nil
870
- end
871
-
872
851
  it "assumes the given time is local" do
873
852
  expect(Time.mongoize(datetime)).to eq(
874
853
  Time.utc(2010, 11, 19)
@@ -945,20 +924,13 @@ describe Mongoid::Extensions::Time do
945
924
 
946
925
  context "when using the ActiveSupport time zone" do
947
926
  config_override :use_activesupport_time_zone, true
927
+ # if this is actually your time zone, the following tests are useless
928
+ time_zone_override "Stockholm"
948
929
 
949
930
  let(:date) do
950
931
  Date.new(2010, 11, 19)
951
932
  end
952
933
 
953
- before do
954
- # if this is actually your time zone, the following tests are useless
955
- Time.zone = "Stockholm"
956
- end
957
-
958
- after do
959
- Time.zone = nil
960
- end
961
-
962
934
  it "assumes the given time is local" do
963
935
  expect(Time.mongoize(date)).to eq(Time.utc(2010, 11, 18, 23))
964
936
  end
@@ -977,15 +949,8 @@ describe Mongoid::Extensions::Time do
977
949
 
978
950
  context "when using the ActiveSupport time zone" do
979
951
  config_override :use_activesupport_time_zone, true
980
-
981
- before do
982
- # if this is actually your time zone, the following tests are useless
983
- Time.zone = "Stockholm"
984
- end
985
-
986
- after do
987
- Time.zone = nil
988
- end
952
+ # if this is actually your time zone, the following tests are useless
953
+ time_zone_override "Stockholm"
989
954
 
990
955
  it "assumes the given time is local" do
991
956
  expect(Time.mongoize(array)).to eq(
@@ -50,14 +50,7 @@ describe Mongoid::Extensions::TimeWithZone do
50
50
 
51
51
  context "when using the ActiveSupport time zone" do
52
52
  config_override :use_activesupport_time_zone, true
53
-
54
- before do
55
- Time.zone = "Stockholm"
56
- end
57
-
58
- after do
59
- Time.zone = nil
60
- end
53
+ time_zone_override "Stockholm"
61
54
 
62
55
  it "returns an ActiveSupport::TimeWithZone" do
63
56
  expect(ActiveSupport::TimeWithZone.demongoize(time).class).to eq(ActiveSupport::TimeWithZone)
@@ -113,19 +106,12 @@ describe Mongoid::Extensions::TimeWithZone do
113
106
 
114
107
  context "when using the ActiveSupport time zone" do
115
108
  config_override :use_activesupport_time_zone, true
109
+ time_zone_override "Stockholm"
116
110
 
117
111
  let(:time) do
118
112
  Time.utc(2010, 11, 19, 0, 30)
119
113
  end
120
114
 
121
- before do
122
- Time.zone = "Stockholm"
123
- end
124
-
125
- after do
126
- Time.zone = nil
127
- end
128
-
129
115
  it "returns utc" do
130
116
  expect(ActiveSupport::TimeWithZone.demongoize(time)).to eq(
131
117
  ActiveSupport::TimeZone['UTC'].local(2010, 11, 19, 0, 30)
@@ -203,15 +189,8 @@ describe Mongoid::Extensions::TimeWithZone do
203
189
 
204
190
  context "when using the ActiveSupport time zone" do
205
191
  config_override :use_activesupport_time_zone, true
206
-
207
- before do
208
- # if this is actually your time zone, the following tests are useless
209
- Time.zone = "Stockholm"
210
- end
211
-
212
- after do
213
- Time.zone = nil
214
- end
192
+ # if this is actually your time zone, the following tests are useless
193
+ time_zone_override "Stockholm"
215
194
 
216
195
  context "when the local time is not observing daylight saving" do
217
196
 
@@ -247,20 +226,12 @@ describe Mongoid::Extensions::TimeWithZone do
247
226
 
248
227
  context "when using the ActiveSupport time zone" do
249
228
  config_override :use_activesupport_time_zone, true
229
+ time_zone_override "Stockholm"
250
230
 
251
231
  let(:datetime) do
252
232
  DateTime.new(2010, 11, 19)
253
233
  end
254
234
 
255
- before do
256
- # if this is actually your time zone, the following tests are useless
257
- Time.zone = "Stockholm"
258
- end
259
-
260
- after do
261
- Time.zone = nil
262
- end
263
-
264
235
  it "assumes the given time is local" do
265
236
  expect(ActiveSupport::TimeWithZone.mongoize(datetime)).to eq(
266
237
  Time.utc(2010, 11, 19)
@@ -317,20 +288,12 @@ describe Mongoid::Extensions::TimeWithZone do
317
288
 
318
289
  context "when using the ActiveSupport time zone" do
319
290
  config_override :use_activesupport_time_zone, true
291
+ time_zone_override "Stockholm"
320
292
 
321
293
  let(:date) do
322
294
  Date.new(2010, 11, 19)
323
295
  end
324
296
 
325
- before do
326
- # if this is actually your time zone, the following tests are useless
327
- Time.zone = "Stockholm"
328
- end
329
-
330
- after do
331
- Time.zone = nil
332
- end
333
-
334
297
  it "assumes the given time is local" do
335
298
  expect(ActiveSupport::TimeWithZone.mongoize(date)).to eq(Time.utc(2010, 11, 18, 23))
336
299
  end
@@ -349,15 +312,7 @@ describe Mongoid::Extensions::TimeWithZone do
349
312
 
350
313
  context "when using the ActiveSupport time zone" do
351
314
  config_override :use_activesupport_time_zone, true
352
-
353
- before do
354
- # if this is actually your time zone, the following tests are useless
355
- Time.zone = "Stockholm"
356
- end
357
-
358
- after do
359
- Time.zone = nil
360
- end
315
+ time_zone_override "Stockholm"
361
316
 
362
317
  it "assumes the given time is local" do
363
318
  expect(ActiveSupport::TimeWithZone.mongoize(array)).to eq(
@@ -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
@@ -835,16 +827,13 @@ describe Mongoid::Fields do
835
827
  end
836
828
 
837
829
  context "when a locale is set" do
830
+ with_default_i18n_configs
838
831
 
839
832
  before do
840
- ::I18n.locale = :de
833
+ I18n.locale = :de
841
834
  product.description = "Cheaper drinks"
842
835
  end
843
836
 
844
- after do
845
- ::I18n.locale = :en
846
- end
847
-
848
837
  let(:description) do
849
838
  product.attributes["description"]
850
839
  end
@@ -855,17 +844,15 @@ describe Mongoid::Fields do
855
844
  end
856
845
 
857
846
  context "when having multiple locales" do
847
+ with_default_i18n_configs
858
848
 
859
849
  before do
850
+ I18n.locale = :en
860
851
  product.description = "Cheap drinks"
861
- ::I18n.locale = :de
852
+ I18n.locale = :de
862
853
  product.description = "Cheaper drinks"
863
854
  end
864
855
 
865
- after do
866
- ::I18n.locale = :en
867
- end
868
-
869
856
  let(:description) do
870
857
  product.attributes["description"]
871
858
  end
@@ -1067,14 +1054,7 @@ describe Mongoid::Fields do
1067
1054
  end
1068
1055
 
1069
1056
  context "when reading the field" do
1070
-
1071
- before do
1072
- Time.zone = "Berlin"
1073
- end
1074
-
1075
- after do
1076
- Time.zone = nil
1077
- end
1057
+ time_zone_override "Berlin"
1078
1058
 
1079
1059
  it "performs the necessary time conversions" do
1080
1060
  expect(person.lunch_time.to_s).to eq(time.getlocal.to_s)
@@ -1964,4 +1944,126 @@ describe Mongoid::Fields do
1964
1944
  end
1965
1945
  end
1966
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
1967
2069
  end