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
@@ -24,21 +24,12 @@ describe Mongoid::QueryCache do
24
24
  Mrss::SessionRegistry.instance.verify_sessions_ended!
25
25
  end
26
26
 
27
- let(:reset_legacy_qc_warning) do
28
- begin
29
- Mongoid::QueryCache.remove_instance_variable('@legacy_query_cache_warned')
30
- rescue NameError
31
- # raised if the instance variable wasn't set
32
- end
33
- end
34
-
35
27
  describe '#cache' do
28
+
36
29
  context 'with driver query cache' do
37
30
 
38
31
  context 'when query cache is not enabled' do
39
- before do
40
- Mongoid::QueryCache.enabled = false
41
- end
32
+ override_query_cache false
42
33
 
43
34
  it 'turns on the query cache within the block' do
44
35
  expect(Mongoid::QueryCache.enabled?).to be false
@@ -52,9 +43,7 @@ describe Mongoid::QueryCache do
52
43
  end
53
44
 
54
45
  context 'when query cache is enabled' do
55
- before do
56
- Mongoid::QueryCache.enabled = true
57
- end
46
+ override_query_cache true
58
47
 
59
48
  it 'keeps the query cache enabled within the block' do
60
49
  expect(Mongoid::QueryCache.enabled?).to be true
@@ -84,12 +73,11 @@ describe Mongoid::QueryCache do
84
73
  end
85
74
 
86
75
  describe '#uncached' do
76
+
87
77
  context 'with driver query cache' do
88
78
 
89
79
  context 'when query cache is not enabled' do
90
- before do
91
- Mongoid::QueryCache.enabled = false
92
- end
80
+ override_query_cache false
93
81
 
94
82
  it 'keeps the query cache turned off within the block' do
95
83
  expect(Mongoid::QueryCache.enabled?).to be false
@@ -103,9 +91,7 @@ describe Mongoid::QueryCache do
103
91
  end
104
92
 
105
93
  context 'when query cache is enabled' do
106
- before do
107
- Mongoid::QueryCache.enabled = true
108
- end
94
+ override_query_cache true
109
95
 
110
96
  it 'turns off the query cache within the block' do
111
97
  expect(Mongoid::QueryCache.enabled?).to be true
@@ -159,10 +145,7 @@ describe Mongoid::QueryCache do
159
145
  end
160
146
 
161
147
  context 'does not query for the relation and instead sets the base' do
162
-
163
- before do
164
- Mongoid::QueryCache.enabled = false
165
- end
148
+ override_query_cache false
166
149
 
167
150
  it 'queries for each access to the base' do
168
151
  expect(server).to receive(:with_connection).exactly(0).times.and_call_original
@@ -180,10 +163,7 @@ describe Mongoid::QueryCache do
180
163
  end
181
164
 
182
165
  context 'when query cache is disabled' do
183
-
184
- before do
185
- Mongoid::QueryCache.enabled = false
186
- end
166
+ override_query_cache false
187
167
 
188
168
  it 'does not query for access to the base' do
189
169
  expect(server).to receive(:context).exactly(0).times.and_call_original
@@ -194,10 +174,7 @@ describe Mongoid::QueryCache do
194
174
  end
195
175
 
196
176
  context 'when query cache is enabled' do
197
-
198
- before do
199
- Mongoid::QueryCache.enabled = true
200
- end
177
+ override_query_cache true
201
178
 
202
179
  it 'does not query for access to the base' do
203
180
  expect(server).to receive(:context).exactly(0).times.and_call_original
@@ -243,10 +220,7 @@ describe Mongoid::QueryCache do
243
220
  end
244
221
 
245
222
  context 'when block is cached' do
246
-
247
- before do
248
- Mongoid::QueryCache.enabled = false
249
- end
223
+ override_query_cache false
250
224
 
251
225
  it 'uses the driver query cache' do
252
226
  expect(Mongo::QueryCache).to receive(:cache).and_call_original
@@ -259,10 +233,7 @@ describe Mongoid::QueryCache do
259
233
  end
260
234
 
261
235
  context 'when block is uncached' do
262
-
263
- before do
264
- Mongoid::QueryCache.enabled = true
265
- end
236
+ override_query_cache true
266
237
 
267
238
  it 'uses the driver query cache' do
268
239
  expect(Mongo::QueryCache).to receive(:uncached).and_call_original
@@ -314,10 +285,7 @@ describe Mongoid::QueryCache do
314
285
  end
315
286
 
316
287
  context "when query cache is disabled" do
317
-
318
- before do
319
- Mongoid::QueryCache.enabled = false
320
- end
288
+ override_query_cache false
321
289
 
322
290
  it "queries again" do
323
291
  expect_query(1) do
@@ -440,10 +408,7 @@ describe Mongoid::QueryCache do
440
408
  end
441
409
 
442
410
  context "when query cache is disabled" do
443
-
444
- before do
445
- Mongoid::QueryCache.enabled = false
446
- end
411
+ override_query_cache false
447
412
 
448
413
  it "queries again" do
449
414
  expect_query(1) do
@@ -669,13 +634,7 @@ describe Mongoid::QueryCache do
669
634
  end
670
635
 
671
636
  context "when query caching is enabled and the batch_size is set" do
672
-
673
- around(:each) do |example|
674
- query_cache_enabled = Mongoid::QueryCache.enabled?
675
- Mongoid::QueryCache.enabled = true
676
- example.run
677
- Mongoid::QueryCache.enabled = query_cache_enabled
678
- end
637
+ override_query_cache true
679
638
 
680
639
  it "does not raise an error when requesting the second batch" do
681
640
  expect {
@@ -684,7 +643,6 @@ describe Mongoid::QueryCache do
684
643
  end
685
644
  }.not_to raise_error
686
645
  end
687
-
688
646
  end
689
647
  end
690
648
 
@@ -752,10 +710,7 @@ describe Mongoid::QueryCache do
752
710
  end
753
711
 
754
712
  context 'when query cache is disabled' do
755
-
756
- before do
757
- Mongoid::QueryCache.enabled = false
758
- end
713
+ override_query_cache false
759
714
 
760
715
  it "queries again" do
761
716
  band = Band.find(band_id)
@@ -819,8 +774,9 @@ describe Mongoid::QueryCache do
819
774
  end
820
775
 
821
776
  context 'when the initial query does not exhaust the results' do
777
+ override_query_cache true
778
+
822
779
  before do
823
- Mongoid::QueryCache.enabled = true
824
780
  10.times { Band.create! }
825
781
 
826
782
  Band.batch_size(4).to_a
@@ -867,4 +823,43 @@ describe Mongoid::QueryCache do
867
823
  school.students.to_a.length.should == 5
868
824
  end
869
825
  end
826
+
827
+ describe 'deprecation warnings' do
828
+
829
+ context '#cache' do
830
+ it 'should raise a warning' do
831
+ expect(Mongoid::Warnings).to receive(:warn_mongoid_query_cache)
832
+ Mongoid::QueryCache.cache {}
833
+ end
834
+ end
835
+
836
+ context '#uncached' do
837
+ it 'should raise a warning' do
838
+ expect(Mongoid::Warnings).to receive(:warn_mongoid_query_cache)
839
+ Mongoid::QueryCache.uncached {}
840
+ end
841
+ end
842
+
843
+ context '#clear_cache' do
844
+ it 'should raise a warning' do
845
+ expect(Mongoid::Warnings).to receive(:warn_mongoid_query_cache_clear)
846
+ Mongoid::QueryCache.clear_cache
847
+ end
848
+ end
849
+
850
+ context '#enabled?' do
851
+ it 'should raise a warning' do
852
+ expect(Mongoid::Warnings).to receive(:warn_mongoid_query_cache)
853
+ Mongoid::QueryCache.enabled?
854
+ end
855
+ end
856
+
857
+ context '#enabled=' do
858
+ it 'should raise a warning' do
859
+ old_enabled = Mongoid::QueryCache.enabled?
860
+ expect(Mongoid::Warnings).to receive(:warn_mongoid_query_cache)
861
+ Mongoid::QueryCache.enabled = old_enabled
862
+ end
863
+ end
864
+ end
870
865
  end
@@ -390,30 +390,6 @@ describe Mongoid::Reloadable do
390
390
  end
391
391
  end
392
392
 
393
- context 'when embeds_many is modified' do
394
- let(:contractor1) { Contractor.new(name: 'b') }
395
- let(:contractor2) { Contractor.new(name: 'c') }
396
-
397
- let(:building) do
398
- Building.create!(contractors: [ contractor1 ])
399
- end
400
-
401
- let(:more_contractors) { building.contractors + [ contractor2 ] }
402
-
403
- let(:modified_building) do
404
- building.tap do
405
- building.assign_attributes contractors: more_contractors
406
- end
407
- end
408
-
409
- let(:reloaded_building) { modified_building.reload }
410
-
411
- it 'resets delayed_atomic_sets' do
412
- expect(modified_building.delayed_atomic_sets).not_to be_empty
413
- expect(reloaded_building.delayed_atomic_sets).to be_empty
414
- end
415
- end
416
-
417
393
  context "when embedded document is nil" do
418
394
 
419
395
  let(:palette) do
@@ -512,12 +488,32 @@ describe Mongoid::Reloadable do
512
488
  Person.create!
513
489
  end
514
490
 
515
- let(:reloaded) do
516
- Person.only(:name).first.reload
491
+ context "when legacy_readonly is on" do
492
+ config_override :legacy_readonly, true
493
+
494
+ let(:reloaded) do
495
+ Person.only(:name).first.reload
496
+ end
497
+
498
+ it 'resets the readonly state after reloading' do
499
+ expect(reloaded.readonly?).to be(false)
500
+ end
517
501
  end
518
502
 
519
- it 'resets the readonly state after reloading' do
520
- expect(reloaded.readonly?).to be(false)
503
+ context "when legacy_readonly is off" do
504
+ config_override :legacy_readonly, false
505
+
506
+ let(:reloaded) do
507
+ Person.only(:name).first.tap do |doc|
508
+ doc.readonly!
509
+ expect(doc.readonly?).to be true
510
+ doc.reload
511
+ end
512
+ end
513
+
514
+ it 'does not reset the readonly state after reloading' do
515
+ expect(reloaded.readonly?).to be(true)
516
+ end
521
517
  end
522
518
  end
523
519
 
@@ -121,6 +121,76 @@ describe Mongoid::Scopable do
121
121
  expect(selector).to eq({'active' => true})
122
122
  end
123
123
  end
124
+
125
+ context "when the default scope is dotted" do
126
+
127
+ let(:criteria) do
128
+ Band.where('tags.foo' => 'bar')
129
+ end
130
+
131
+ before do
132
+ Band.default_scope ->{ criteria }
133
+ end
134
+
135
+ after do
136
+ Band.default_scoping = nil
137
+ end
138
+
139
+ let!(:band) do
140
+ Band.create!
141
+ end
142
+
143
+ it "adds the scope as a dotted key attribute" do
144
+ expect(band.attributes['tags.foo']).to eq('bar')
145
+ end
146
+
147
+ it "adds the default scope to the class" do
148
+ expect(Band.default_scoping.call).to eq(criteria)
149
+ end
150
+
151
+ it "flags as being default scoped" do
152
+ expect(Band).to be_default_scoping
153
+ end
154
+
155
+ it "does not find the correct document" do
156
+ expect(Band.count).to eq(0)
157
+ end
158
+ end
159
+
160
+ context "when the default scope is dotted with a query" do
161
+
162
+ let(:criteria) do
163
+ Band.where('tags.foo' => {'$eq' => 'bar'})
164
+ end
165
+
166
+ before do
167
+ Band.default_scope ->{ criteria }
168
+ end
169
+
170
+ after do
171
+ Band.default_scoping = nil
172
+ end
173
+
174
+ let!(:band) do
175
+ Band.create!('tags' => { 'foo' => 'bar' })
176
+ end
177
+
178
+ it "does not add the scope as a dotted key attribute" do
179
+ expect(band.attributes).to_not have_key('tags.foo')
180
+ end
181
+
182
+ it "adds the default scope to the class" do
183
+ expect(Band.default_scoping.call).to eq(criteria)
184
+ end
185
+
186
+ it "flags as being default scoped" do
187
+ expect(Band).to be_default_scoping
188
+ end
189
+
190
+ it "finds the correct document" do
191
+ expect(Band.where.first).to eq(band)
192
+ end
193
+ end
124
194
  end
125
195
 
126
196
  describe ".default_scopable?" do
@@ -14,19 +14,19 @@ describe Mongoid::Serializable do
14
14
  expect(guitar.send(:field_names, {})).to eq(guitar.fields.except("_type").keys.sort)
15
15
  end
16
16
 
17
- context "when using a custom discriminator_key" do
18
- before do
17
+ context "when using a custom discriminator_key" do
18
+ before do
19
19
  Instrument.discriminator_key = "dkey"
20
20
  end
21
21
 
22
- after do
22
+ after do
23
23
  Instrument.discriminator_key = nil
24
24
  end
25
25
 
26
26
  let(:guitar) do
27
27
  Guitar.new
28
28
  end
29
-
29
+
30
30
  it "includes _type but does not include the new discriminator key" do
31
31
  expect(guitar.send(:field_names, {})).to eq(guitar.fields.except("dkey").keys.sort)
32
32
  end
@@ -41,15 +41,11 @@ describe Mongoid::Serializable do
41
41
  end
42
42
 
43
43
  after do
44
- Mongoid.include_root_in_json = false
45
44
  reload_model(:Minim)
46
45
  end
47
46
 
48
47
  context "when global config is set to true" do
49
-
50
- before do
51
- Mongoid.include_root_in_json = true
52
- end
48
+ config_override :include_root_in_json, true
53
49
 
54
50
  it "returns true" do
55
51
  expect(Minim.public_send(meth)).to be true
@@ -67,10 +63,7 @@ describe Mongoid::Serializable do
67
63
  end
68
64
 
69
65
  context "when global config set to false" do
70
-
71
- before do
72
- Mongoid.include_root_in_json = false
73
- end
66
+ config_override :include_root_in_json, false
74
67
 
75
68
  it "returns false" do
76
69
  expect(Minim.public_send(meth)).to be false
@@ -89,13 +82,13 @@ describe Mongoid::Serializable do
89
82
  end
90
83
 
91
84
  describe "#include_root_in_json" do
85
+ config_override :include_root_in_json, false
92
86
 
93
87
  before do
94
88
  reload_model(:Minim)
95
89
  end
96
90
 
97
91
  after do
98
- Mongoid.include_root_in_json = false
99
92
  reload_model(:Minim)
100
93
  end
101
94
 
@@ -127,10 +120,6 @@ describe Mongoid::Serializable do
127
120
 
128
121
  context "when global config set to false" do
129
122
 
130
- before do
131
- Mongoid.include_root_in_json = false
132
- end
133
-
134
123
  it "returns false" do
135
124
  expect(minim.public_send(meth)).to be false
136
125
  end
@@ -297,14 +286,7 @@ describe Mongoid::Serializable do
297
286
  end
298
287
 
299
288
  context "when include_type_for_serialization is true" do
300
-
301
- before do
302
- Mongoid.include_type_for_serialization = true
303
- end
304
-
305
- after do
306
- Mongoid.include_type_for_serialization = false
307
- end
289
+ config_override :include_type_for_serialization, true
308
290
 
309
291
  it "includes _type field" do
310
292
  expect(person.serializable_hash.keys).to include '_type'
@@ -528,13 +510,13 @@ describe Mongoid::Serializable do
528
510
  end
529
511
 
530
512
  it "includes the first relation" do
531
- expect(relation_hash[0]).to include(
532
- { "_id" => "kudamm", "street" => "Kudamm" })
513
+ expect(relation_hash[0]).to include
514
+ { "_id" => "kudamm", "street" => "Kudamm" }
533
515
  end
534
516
 
535
517
  it "includes the second relation" do
536
- expect(relation_hash[1]).to include(
537
- { "_id" => "tauentzienstr", "street" => "Tauentzienstr" })
518
+ expect(relation_hash[1]).to include
519
+ { "_id" => "tauentzienstr", "street" => "Tauentzienstr" }
538
520
  end
539
521
  end
540
522
 
@@ -545,13 +527,13 @@ describe Mongoid::Serializable do
545
527
  end
546
528
 
547
529
  it "includes the first relation" do
548
- expect(relation_hash[0]).to include(
549
- { "_id" => "kudamm", "street" => "Kudamm" })
530
+ expect(relation_hash[0]).to include
531
+ { "_id" => "kudamm", "street" => "Kudamm" }
550
532
  end
551
533
 
552
534
  it "includes the second relation" do
553
- expect(relation_hash[1]).to include(
554
- { "_id" => "tauentzienstr", "street" => "Tauentzienstr" })
535
+ expect(relation_hash[1]).to include
536
+ { "_id" => "tauentzienstr", "street" => "Tauentzienstr" }
555
537
  end
556
538
  end
557
539
 
@@ -670,8 +652,8 @@ describe Mongoid::Serializable do
670
652
  end
671
653
 
672
654
  it "includes the specified relation" do
673
- expect(relation_hash).to include(
674
- { "_id" => "Leo-Marvin", "first_name" => "Leo", "last_name" => "Marvin" })
655
+ expect(relation_hash).to include
656
+ { "_id" => "leo-marvin", "first_name" => "Leo", "last_name" => "Marvin" }
675
657
  end
676
658
  end
677
659
 
@@ -682,8 +664,8 @@ describe Mongoid::Serializable do
682
664
  end
683
665
 
684
666
  it "includes the specified relation" do
685
- expect(relation_hash).to include(
686
- { "_id" => "Leo-Marvin", "first_name" => "Leo", "last_name" => "Marvin" })
667
+ expect(relation_hash).to include
668
+ { "_id" => "leo-marvin", "first_name" => "Leo", "last_name" => "Marvin" }
687
669
  end
688
670
  end
689
671
 
@@ -694,8 +676,8 @@ describe Mongoid::Serializable do
694
676
  end
695
677
 
696
678
  it "includes the specified relation sans exceptions" do
697
- expect(relation_hash).to include(
698
- { "first_name" => "Leo", "last_name" => "Marvin" })
679
+ expect(relation_hash).to include
680
+ { "first_name" => "Leo", "last_name" => "Marvin" }
699
681
  end
700
682
  end
701
683
  end
@@ -881,16 +863,13 @@ describe Mongoid::Serializable do
881
863
  end
882
864
 
883
865
  context "when including root in json via Mongoid" do
866
+ config_override :include_root_in_json, false
884
867
 
885
868
  before do
886
869
  account.include_root_in_json.should be false
887
870
  Mongoid.include_root_in_json = true
888
871
  end
889
872
 
890
- after do
891
- Mongoid.include_root_in_json = false
892
- end
893
-
894
873
  it "uses the mongoid configuration" do
895
874
  expect(JSON.parse(account.to_json)).to have_key("account")
896
875
  end
@@ -136,21 +136,135 @@ describe Mongoid::Stateful do
136
136
  Band.new
137
137
  end
138
138
 
139
- context "when the document is readonly" do
139
+ context "when legacy_readonly is true" do
140
+ config_override :legacy_readonly, true
140
141
 
141
- before do
142
- document.__selected_fields = { test: 1 }
142
+ context "when the selected fields are set" do
143
+
144
+ before do
145
+ document.__selected_fields = { test: 1 }
146
+ end
147
+
148
+ it "returns true" do
149
+ expect(document).to be_readonly
150
+ end
143
151
  end
144
152
 
145
- it "returns true" do
146
- expect(document).to be_readonly
153
+ context "when no readonly has been set" do
154
+
155
+ it "returns false" do
156
+ expect(document).to_not be_readonly
157
+ end
158
+ end
159
+
160
+ context "when the readonly! method is called" do
161
+
162
+ let(:op) do
163
+ document.readonly!
164
+ end
165
+
166
+ it "returns false" do
167
+ op
168
+ expect(document).to_not be_readonly
169
+ end
170
+
171
+ it "warns" do
172
+ expect(Mongoid::Warnings).to receive(:warn_legacy_readonly)
173
+ op
174
+ end
175
+ end
176
+
177
+ context "when overriding readonly?" do
178
+
179
+ let(:doc) { ReadonlyModel.create! }
180
+
181
+ before do
182
+ class ReadonlyModel
183
+ include Mongoid::Document
184
+
185
+ attr_accessor :locked
186
+
187
+ def readonly?
188
+ !!locked
189
+ end
190
+ end
191
+ end
192
+
193
+ after do
194
+ Object.send(:remove_const, :ReadonlyModel)
195
+ end
196
+
197
+ it "raises when readonly? is true" do
198
+ expect(doc.readonly?).to be false
199
+ doc.locked = true
200
+ expect(doc.readonly?).to be true
201
+ expect do
202
+ doc.destroy
203
+ end.to raise_error(Mongoid::Errors::ReadonlyDocument)
204
+ end
147
205
  end
148
206
  end
149
207
 
150
- context "when no readonly has been set" do
208
+ context "when legacy_readonly is false" do
209
+ config_override :legacy_readonly, false
151
210
 
152
- it "returns false" do
153
- expect(document).to_not be_readonly
211
+ context "when the selected fields are set" do
212
+
213
+ before do
214
+ document.__selected_fields = { test: 1 }
215
+ end
216
+
217
+ it "returns false" do
218
+ expect(document).to_not be_readonly
219
+ end
220
+ end
221
+
222
+ context "when the readonly! method is called" do
223
+
224
+ before do
225
+ document.readonly!
226
+ end
227
+
228
+ it "returns true" do
229
+ expect(document).to be_readonly
230
+ end
231
+ end
232
+
233
+ context "when no readonly has been set" do
234
+
235
+ it "returns false" do
236
+ expect(document).to_not be_readonly
237
+ end
238
+ end
239
+
240
+ context "when overriding readonly?" do
241
+
242
+ let(:doc) { ReadonlyModel.new }
243
+
244
+ before do
245
+ class ReadonlyModel
246
+ include Mongoid::Document
247
+
248
+ attr_accessor :locked
249
+
250
+ def readonly?
251
+ !!locked
252
+ end
253
+ end
254
+ end
255
+
256
+ after do
257
+ Object.send(:remove_const, :ReadonlyModel)
258
+ end
259
+
260
+ it "raises when readonly? is true" do
261
+ expect(doc.readonly?).to be false
262
+ doc.locked = true
263
+ expect(doc.readonly?).to be true
264
+ expect do
265
+ doc.save!
266
+ end.to raise_error(Mongoid::Errors::ReadonlyDocument)
267
+ end
154
268
  end
155
269
  end
156
270
  end