mongoid 8.0.6 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) 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 +0 -25
  6. data/lib/config/locales/en.yml +46 -14
  7. data/lib/mongoid/association/accessors.rb +2 -2
  8. data/lib/mongoid/association/builders.rb +1 -1
  9. data/lib/mongoid/association/embedded/batchable.rb +2 -2
  10. data/lib/mongoid/association/embedded/embedded_in/buildable.rb +2 -2
  11. data/lib/mongoid/association/embedded/embedded_in/proxy.rb +2 -1
  12. data/lib/mongoid/association/embedded/embeds_many/buildable.rb +3 -2
  13. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +6 -6
  14. data/lib/mongoid/association/embedded/embeds_one/buildable.rb +1 -1
  15. data/lib/mongoid/association/embedded/embeds_one/proxy.rb +1 -1
  16. data/lib/mongoid/association/macros.rb +0 -6
  17. data/lib/mongoid/association/nested/one.rb +40 -2
  18. data/lib/mongoid/association/proxy.rb +1 -1
  19. data/lib/mongoid/association/referenced/counter_cache.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +1 -1
  21. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -2
  22. data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
  23. data/lib/mongoid/association/reflections.rb +2 -2
  24. data/lib/mongoid/atomic.rb +0 -7
  25. data/lib/mongoid/attributes/dynamic.rb +1 -1
  26. data/lib/mongoid/attributes/nested.rb +2 -2
  27. data/lib/mongoid/attributes/processing.rb +5 -29
  28. data/lib/mongoid/attributes/projector.rb +1 -1
  29. data/lib/mongoid/attributes/readonly.rb +1 -1
  30. data/lib/mongoid/attributes.rb +8 -2
  31. data/lib/mongoid/changeable.rb +107 -5
  32. data/lib/mongoid/clients/storage_options.rb +2 -5
  33. data/lib/mongoid/clients/validators/storage.rb +1 -13
  34. data/lib/mongoid/collection_configurable.rb +58 -0
  35. data/lib/mongoid/composable.rb +2 -0
  36. data/lib/mongoid/config/defaults.rb +60 -0
  37. data/lib/mongoid/config/options.rb +0 -3
  38. data/lib/mongoid/config/validators/async_query_executor.rb +24 -0
  39. data/lib/mongoid/config/validators.rb +1 -0
  40. data/lib/mongoid/config.rb +99 -15
  41. data/lib/mongoid/contextual/atomic.rb +1 -1
  42. data/lib/mongoid/contextual/memory.rb +233 -33
  43. data/lib/mongoid/contextual/mongo/documents_loader.rb +177 -0
  44. data/lib/mongoid/contextual/mongo.rb +373 -113
  45. data/lib/mongoid/contextual/none.rb +162 -7
  46. data/lib/mongoid/contextual.rb +12 -0
  47. data/lib/mongoid/criteria/findable.rb +2 -2
  48. data/lib/mongoid/criteria/includable.rb +4 -3
  49. data/lib/mongoid/criteria/queryable/key.rb +1 -1
  50. data/lib/mongoid/criteria/queryable/mergeable.rb +1 -1
  51. data/lib/mongoid/criteria/queryable/optional.rb +8 -8
  52. data/lib/mongoid/criteria/queryable/selectable.rb +43 -12
  53. data/lib/mongoid/criteria/queryable/selector.rb +1 -1
  54. data/lib/mongoid/criteria/queryable/storable.rb +1 -1
  55. data/lib/mongoid/criteria.rb +6 -5
  56. data/lib/mongoid/deprecable.rb +1 -1
  57. data/lib/mongoid/errors/create_collection_failure.rb +33 -0
  58. data/lib/mongoid/errors/drop_collection_failure.rb +27 -0
  59. data/lib/mongoid/errors/immutable_attribute.rb +26 -0
  60. data/lib/mongoid/errors/invalid_async_query_executor.rb +25 -0
  61. data/lib/mongoid/errors/invalid_global_executor_concurrency.rb +22 -0
  62. data/lib/mongoid/errors/invalid_storage_parent.rb +2 -0
  63. data/lib/mongoid/errors.rb +4 -1
  64. data/lib/mongoid/extensions/hash.rb +2 -6
  65. data/lib/mongoid/extensions/object.rb +2 -2
  66. data/lib/mongoid/extensions/time.rb +2 -0
  67. data/lib/mongoid/fields/localized.rb +10 -0
  68. data/lib/mongoid/fields/standard.rb +10 -0
  69. data/lib/mongoid/fields.rb +53 -24
  70. data/lib/mongoid/findable.rb +27 -3
  71. data/lib/mongoid/interceptable.rb +7 -6
  72. data/lib/mongoid/matcher/eq_impl.rb +1 -1
  73. data/lib/mongoid/matcher/type.rb +1 -1
  74. data/lib/mongoid/persistable/creatable.rb +1 -0
  75. data/lib/mongoid/persistable/deletable.rb +1 -1
  76. data/lib/mongoid/persistable/savable.rb +13 -1
  77. data/lib/mongoid/persistable/unsettable.rb +2 -2
  78. data/lib/mongoid/persistable/updatable.rb +51 -1
  79. data/lib/mongoid/persistable/upsertable.rb +20 -1
  80. data/lib/mongoid/persistable.rb +3 -0
  81. data/lib/mongoid/query_cache.rb +5 -1
  82. data/lib/mongoid/railties/database.rake +7 -2
  83. data/lib/mongoid/reloadable.rb +5 -3
  84. data/lib/mongoid/stateful.rb +22 -1
  85. data/lib/mongoid/tasks/database.rake +12 -0
  86. data/lib/mongoid/tasks/database.rb +20 -0
  87. data/lib/mongoid/utils.rb +22 -0
  88. data/lib/mongoid/validatable/macros.rb +5 -5
  89. data/lib/mongoid/validatable.rb +4 -1
  90. data/lib/mongoid/version.rb +1 -1
  91. data/lib/mongoid/warnings.rb +17 -1
  92. data/lib/mongoid.rb +16 -3
  93. data/spec/integration/app_spec.rb +2 -2
  94. data/spec/integration/callbacks_models.rb +37 -0
  95. data/spec/integration/callbacks_spec.rb +134 -0
  96. data/spec/integration/discriminator_key_spec.rb +4 -5
  97. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  98. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  99. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +20 -25
  100. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  101. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  102. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  103. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +5 -27
  104. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +9 -50
  105. data/spec/mongoid/association/syncable_spec.rb +1 -1
  106. data/spec/mongoid/attributes_spec.rb +3 -33
  107. data/spec/mongoid/changeable_spec.rb +299 -24
  108. data/spec/mongoid/clients_spec.rb +122 -13
  109. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  110. data/spec/mongoid/config/defaults_spec.rb +160 -0
  111. data/spec/mongoid/config_spec.rb +154 -27
  112. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  113. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  114. data/spec/mongoid/contextual/mongo_spec.rb +1006 -77
  115. data/spec/mongoid/contextual/none_spec.rb +49 -2
  116. data/spec/mongoid/copyable_spec.rb +3 -11
  117. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -10
  118. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  119. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  120. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  121. data/spec/mongoid/criteria/queryable/selector_spec.rb +3 -76
  122. data/spec/mongoid/criteria/queryable/storable_spec.rb +0 -72
  123. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  124. data/spec/mongoid/criteria_spec.rb +5 -9
  125. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  126. data/spec/mongoid/extensions/hash_spec.rb +3 -3
  127. data/spec/mongoid/extensions/time_spec.rb +8 -43
  128. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  129. data/spec/mongoid/fields/localized_spec.rb +46 -28
  130. data/spec/mongoid/fields_spec.rb +136 -77
  131. data/spec/mongoid/findable_spec.rb +391 -34
  132. data/spec/mongoid/indexable_spec.rb +16 -10
  133. data/spec/mongoid/interceptable_spec.rb +15 -3
  134. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  135. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  136. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  137. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  138. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  139. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  140. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  141. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  142. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  143. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  144. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  145. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  146. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  147. data/spec/mongoid/persistence_context_spec.rb +7 -57
  148. data/spec/mongoid/query_cache_spec.rb +56 -61
  149. data/spec/mongoid/reloadable_spec.rb +24 -28
  150. data/spec/mongoid/scopable_spec.rb +70 -0
  151. data/spec/mongoid/serializable_spec.rb +9 -30
  152. data/spec/mongoid/stateful_spec.rb +122 -8
  153. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  154. data/spec/mongoid/tasks/database_spec.rb +127 -0
  155. data/spec/mongoid/timestamps_spec.rb +9 -11
  156. data/spec/mongoid/touchable_spec.rb +277 -5
  157. data/spec/mongoid/touchable_spec_models.rb +3 -1
  158. data/spec/mongoid/traversable_spec.rb +9 -24
  159. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  160. data/spec/mongoid_spec.rb +36 -10
  161. data/spec/shared/lib/mrss/docker_runner.rb +7 -0
  162. data/spec/shared/lib/mrss/lite_constraints.rb +2 -2
  163. data/spec/shared/lib/mrss/server_version_registry.rb +16 -23
  164. data/spec/shared/lib/mrss/utils.rb +28 -6
  165. data/spec/shared/share/Dockerfile.erb +36 -40
  166. data/spec/shared/shlib/server.sh +28 -4
  167. data/spec/shared/shlib/set_env.sh +4 -4
  168. data/spec/spec_helper.rb +5 -0
  169. data/spec/support/immutable_ids.rb +118 -0
  170. data/spec/support/macros.rb +47 -15
  171. data/spec/support/models/artist.rb +0 -1
  172. data/spec/support/models/band.rb +1 -0
  173. data/spec/support/models/book.rb +1 -0
  174. data/spec/support/models/building.rb +2 -0
  175. data/spec/support/models/cover.rb +10 -0
  176. data/spec/support/models/person.rb +0 -1
  177. data/spec/support/models/product.rb +1 -0
  178. data.tar.gz.sig +0 -0
  179. metadata +686 -652
  180. metadata.gz.sig +0 -0
  181. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  182. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
  183. data/spec/support/models/purse.rb +0 -9
@@ -44,7 +44,7 @@ describe Mongoid::Criteria::Queryable::Selector do
44
44
  end
45
45
  end
46
46
 
47
- context "when selector contains a $nin string" do
47
+ context "when selector contains a $nin" do
48
48
 
49
49
  let(:initial) do
50
50
  { "$nin" => ["foo"] }
@@ -72,35 +72,7 @@ describe Mongoid::Criteria::Queryable::Selector do
72
72
  end
73
73
  end
74
74
 
75
- context "when selector contains a $nin symbol" do
76
-
77
- let(:initial) do
78
- { :$nin => ["foo"] }
79
- end
80
-
81
- before do
82
- selector["field"] = initial
83
- end
84
-
85
- context "when merging in a new $nin" do
86
-
87
- let(:other) do
88
- { "field" => { :$nin => ["bar"] } }
89
- end
90
-
91
- before do
92
- selector.merge!(other)
93
- end
94
-
95
- it "combines the two $nin queries into one" do
96
- expect(selector).to eq({
97
- "field" => { :$nin => ["foo", "bar"] }
98
- })
99
- end
100
- end
101
- end
102
-
103
- context "when selector contains a $in string" do
75
+ context "when selector contains a $in" do
104
76
 
105
77
  let(:initial) do
106
78
  { "$in" => [1, 2] }
@@ -145,51 +117,6 @@ describe Mongoid::Criteria::Queryable::Selector do
145
117
  end
146
118
  end
147
119
 
148
- context "when selector contains a $in symbol" do
149
-
150
- let(:initial) do
151
- { :$in => [1, 2] }
152
- end
153
-
154
- before do
155
- selector["field"] = initial
156
- end
157
-
158
- context "when merging in a new $in with an intersecting value" do
159
-
160
- let(:other) do
161
- { "field" => { :$in => [1] } }
162
- end
163
-
164
- before do
165
- selector.merge!(other)
166
- end
167
-
168
- it "intersects the $in values" do
169
- expect(selector).to eq({
170
- "field" => { :$in => [1] }
171
- })
172
- end
173
- end
174
-
175
- context "when merging in a new $in with no intersecting values" do
176
-
177
- let(:other) do
178
- { "field" => { :$in => [3] } }
179
- end
180
-
181
- before do
182
- selector.merge!(other)
183
- end
184
-
185
- it "intersects the $in values" do
186
- expect(selector).to eq({
187
- "field" => { :$in => [] }
188
- })
189
- end
190
- end
191
- end
192
-
193
120
  context "when selector is not nested" do
194
121
 
195
122
  before do
@@ -681,6 +608,7 @@ describe Mongoid::Criteria::Queryable::Selector do
681
608
  end
682
609
 
683
610
  context "when the serializer is localized" do
611
+ with_default_i18n_configs
684
612
 
685
613
  before(:all) do
686
614
  class Field
@@ -696,7 +624,6 @@ describe Mongoid::Criteria::Queryable::Selector do
696
624
 
697
625
  after(:all) do
698
626
  Object.send(:remove_const, :Field)
699
- ::I18n.locale = :en
700
627
  end
701
628
 
702
629
  let(:selector) do
@@ -210,79 +210,7 @@ describe Mongoid::Criteria::Queryable::Storable do
210
210
  }
211
211
  end
212
212
  end
213
-
214
- context 'when value is a hash combine values with different operator keys' do
215
- let(:base) do
216
- query.add_field_expression('foo', {'$in' => ['bar']})
217
- end
218
-
219
- let(:modified) do
220
- base.add_field_expression('foo', {'$nin' => ['zoom']})
221
- end
222
-
223
- it 'combines the conditions using $and' do
224
- modified.selector.should == {
225
- 'foo' => {
226
- '$in' => ['bar'],
227
- '$nin' => ['zoom']
228
- }
229
- }
230
- end
231
- end
232
-
233
- context 'when value is a hash with symbol operator key combine values with different operator keys' do
234
- let(:base) do
235
- query.add_field_expression('foo', {:$in => ['bar']})
236
- end
237
-
238
- let(:modified) do
239
- base.add_field_expression('foo', {:$nin => ['zoom']})
240
- end
241
-
242
- it 'combines the conditions using $and' do
243
- modified.selector.should == {
244
- 'foo' => {
245
- :$in => ['bar'],
246
- :$nin => ['zoom']
247
- }
248
- }
249
- end
250
- end
251
-
252
- context 'when value is a hash add values with same operator keys using $and' do
253
- let(:base) do
254
- query.add_field_expression('foo', {'$in' => ['bar']})
255
- end
256
-
257
- let(:modified) do
258
- base.add_field_expression('foo', {'$in' => ['zoom']})
259
- end
260
-
261
- it 'adds the new condition using $and' do
262
- modified.selector.should == {
263
- 'foo' => {'$in' => ['bar']},
264
- '$and' => ['foo' => {'$in' => ['zoom']}]
265
- }
266
- end
267
- end
268
-
269
- context 'when value is a hash with symbol operator key add values with same operator keys using $and' do
270
- let(:base) do
271
- query.add_field_expression('foo', {:$in => ['bar']})
272
- end
273
-
274
- let(:modified) do
275
- base.add_field_expression('foo', {:$in => ['zoom']})
276
- end
277
-
278
- it 'adds the new condition using $and' do
279
- modified.selector.should == {
280
- 'foo' => {:$in => ['bar']},
281
- '$and' => ['foo' => {:$in => ['zoom']}]
282
- }
283
- end
284
213
  end
285
- end
286
214
 
287
215
  describe '#add_operator_expression' do
288
216
  let(:query_method) { :add_operator_expression }
@@ -154,6 +154,7 @@ describe Mongoid::Criteria do
154
154
  end
155
155
 
156
156
  context 'when the field is localized' do
157
+ with_default_i18n_configs
157
158
 
158
159
  before do
159
160
  I18n.locale = :en
@@ -163,10 +164,6 @@ describe Mongoid::Criteria do
163
164
  d.save!
164
165
  end
165
166
 
166
- after do
167
- I18n.locale = :en
168
- end
169
-
170
167
  context 'when entire field is included' do
171
168
 
172
169
  let(:dictionary) do
@@ -1953,6 +1953,7 @@ describe Mongoid::Criteria do
1953
1953
  end
1954
1954
 
1955
1955
  context 'when plucking a localized field' do
1956
+ with_default_i18n_configs
1956
1957
 
1957
1958
  before do
1958
1959
  I18n.locale = :en
@@ -1962,10 +1963,6 @@ describe Mongoid::Criteria do
1962
1963
  d.save!
1963
1964
  end
1964
1965
 
1965
- after do
1966
- I18n.locale = :en
1967
- end
1968
-
1969
1966
  context 'when plucking the entire field' do
1970
1967
  let(:plucked) do
1971
1968
  Dictionary.all.pluck(:description)
@@ -2059,13 +2056,10 @@ describe Mongoid::Criteria do
2059
2056
  end
2060
2057
 
2061
2058
  context 'when fallbacks are enabled with a locale list' do
2062
- with_i18n_fallbacks
2059
+ require_fallbacks
2063
2060
 
2064
- around(:all) do |example|
2065
- prev_fallbacks = I18n.fallbacks.dup
2061
+ before do
2066
2062
  I18n.fallbacks[:he] = [ :en ]
2067
- example.run
2068
- I18n.fallbacks = prev_fallbacks
2069
2063
  end
2070
2064
 
2071
2065
  let(:plucked) do
@@ -2093,6 +2087,8 @@ describe Mongoid::Criteria do
2093
2087
  end
2094
2088
 
2095
2089
  context "when the localized field is embedded" do
2090
+ with_default_i18n_configs
2091
+
2096
2092
  before do
2097
2093
  p = Passport.new
2098
2094
  I18n.locale = :en
@@ -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
 
@@ -178,7 +178,7 @@ describe Mongoid::Extensions::Hash do
178
178
 
179
179
  it "moves the non hash values under the provided key" do
180
180
  expect(consolidated).to eq({
181
- "$set" => { 'name' => "Tool", likes: 10 }, "$inc" => { 'plays' => 1 }
181
+ "$set" => { name: "Tool", likes: 10 }, "$inc" => { plays: 1 }
182
182
  })
183
183
  end
184
184
  end
@@ -195,7 +195,7 @@ describe Mongoid::Extensions::Hash do
195
195
 
196
196
  it "moves the non hash values under the provided key" do
197
197
  expect(consolidated).to eq({
198
- "$set" => { likes: 10, 'name' => "Tool" }, "$inc" => { 'plays' => 1 }
198
+ "$set" => { likes: 10, name: "Tool" }, "$inc" => { plays: 1 }
199
199
  })
200
200
  end
201
201
  end
@@ -213,7 +213,7 @@ describe Mongoid::Extensions::Hash do
213
213
 
214
214
  it "moves the non hash values under the provided key" do
215
215
  expect(consolidated).to eq({
216
- "$set" => { likes: 10, name: "Tool" }, "$inc" => { 'plays' => 1 }
216
+ "$set" => { likes: 10, name: "Tool" }, "$inc" => { plays: 1 }
217
217
  })
218
218
  end
219
219
  end
@@ -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