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
@@ -616,4 +616,31 @@ describe Mongoid::Association::Embedded::EmbeddedIn::Proxy do
616
616
  end
617
617
  end
618
618
  end
619
+
620
+ context "when assigning a hash" do
621
+ let(:building_address) { BuildingAddress.new }
622
+
623
+ before do
624
+ building_address.building = { name: "Chrysler" }
625
+ end
626
+
627
+ it "creates the objects correctly" do
628
+ expect(building_address.building).to be_a(Building)
629
+ expect(building_address.building.name).to eq("Chrysler")
630
+ end
631
+ end
632
+
633
+ context "when replacing an association with a hash" do
634
+ let(:building_address) { BuildingAddress.new }
635
+
636
+ before do
637
+ building_address.building = { name: "Chrysler" }
638
+ building_address.building = { name: "Empire State" }
639
+ end
640
+
641
+ it "creates the objects correctly" do
642
+ expect(building_address.building).to be_a(Building)
643
+ expect(building_address.building.name).to eq("Empire State")
644
+ end
645
+ end
619
646
  end
@@ -2357,10 +2357,7 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
2357
2357
  context "when the id does not match" do
2358
2358
 
2359
2359
  context "when config set to raise error" do
2360
-
2361
- before do
2362
- Mongoid.raise_not_found_error = true
2363
- end
2360
+ config_override :raise_not_found_error, true
2364
2361
 
2365
2362
  it "raises an error" do
2366
2363
  expect {
@@ -2370,19 +2367,12 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
2370
2367
  end
2371
2368
 
2372
2369
  context "when config set not to raise error" do
2370
+ config_override :raise_not_found_error, false
2373
2371
 
2374
2372
  let(:address) do
2375
2373
  person.addresses.find(BSON::ObjectId.new)
2376
2374
  end
2377
2375
 
2378
- before do
2379
- Mongoid.raise_not_found_error = false
2380
- end
2381
-
2382
- after do
2383
- Mongoid.raise_not_found_error = true
2384
- end
2385
-
2386
2376
  it "returns nil" do
2387
2377
  expect(address).to be_nil
2388
2378
  end
@@ -2406,10 +2396,7 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
2406
2396
  context "when the ids do not match" do
2407
2397
 
2408
2398
  context "when config set to raise error" do
2409
-
2410
- before do
2411
- Mongoid.raise_not_found_error = true
2412
- end
2399
+ config_override :raise_not_found_error, true
2413
2400
 
2414
2401
  it "raises an error" do
2415
2402
  expect {
@@ -2419,19 +2406,12 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
2419
2406
  end
2420
2407
 
2421
2408
  context "when config set not to raise error" do
2409
+ config_override :raise_not_found_error, false
2422
2410
 
2423
2411
  let(:addresses) do
2424
2412
  person.addresses.find([ BSON::ObjectId.new ])
2425
2413
  end
2426
2414
 
2427
- before do
2428
- Mongoid.raise_not_found_error = false
2429
- end
2430
-
2431
- after do
2432
- Mongoid.raise_not_found_error = true
2433
- end
2434
-
2435
2415
  it "returns an empty array" do
2436
2416
  expect(addresses).to be_empty
2437
2417
  end
@@ -4591,7 +4571,7 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
4591
4571
  before do
4592
4572
  band.collection.
4593
4573
  find(_id: band.id).
4594
- update_one("$set" => { records: [{ name: "Moderat" }]})
4574
+ update_one("$set" => { records: [{ _id: BSON::ObjectId.new, name: "Moderat" }]})
4595
4575
  end
4596
4576
 
4597
4577
  context "when loading the documents" do
@@ -4865,4 +4845,19 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
4865
4845
  expect(from_db.company_tags.size).to eq(2)
4866
4846
  end
4867
4847
  end
4848
+
4849
+ context "when assigning hashes" do
4850
+ let(:user) { EmmUser.create! }
4851
+
4852
+ before do
4853
+ user.orders = [ { sku: 1 }, { sku: 2 } ]
4854
+ end
4855
+
4856
+ it "creates the objects correctly" do
4857
+ expect(user.orders.first).to be_a(EmmOrder)
4858
+ expect(user.orders.last).to be_a(EmmOrder)
4859
+
4860
+ expect(user.orders.map(&:sku).sort).to eq([ 1, 2 ])
4861
+ end
4862
+ end
4868
4863
  end
@@ -100,6 +100,7 @@ end
100
100
  class EmmOrder
101
101
  include Mongoid::Document
102
102
 
103
+ field :sku
103
104
  field :amount, type: Integer
104
105
 
105
106
  embedded_in :user, class_name: 'EmmUser'
@@ -202,7 +202,7 @@ describe Mongoid::Association::Embedded::EmbedsOne::Proxy do
202
202
  end
203
203
  end
204
204
  end
205
-
205
+
206
206
  context 'when the original document does not need to be unset because it will be replaced by the $set' do
207
207
 
208
208
  let!(:pet_owner) do
@@ -971,7 +971,7 @@ describe Mongoid::Association::Embedded::EmbedsOne::Proxy do
971
971
  before do
972
972
  band.collection.
973
973
  find(_id: band.id).
974
- update_one("$set" => { label: { name: "Mute" }})
974
+ update_one("$set" => { label: { _id: BSON::ObjectId.new, name: "Mute" }})
975
975
  end
976
976
 
977
977
  context "when loading the documents" do
@@ -1022,4 +1022,17 @@ describe Mongoid::Association::Embedded::EmbedsOne::Proxy do
1022
1022
  expect(building.building_address).to be_a(BuildingAddress)
1023
1023
  end
1024
1024
  end
1025
+
1026
+ context "when assigning a hash" do
1027
+ let(:building) { Building.create! }
1028
+
1029
+ before do
1030
+ building.building_address = { city: "NYC" }
1031
+ end
1032
+
1033
+ it "creates the objects correctly" do
1034
+ expect(building.building_address).to be_a(BuildingAddress)
1035
+ expect(building.building_address.city).to eq("NYC")
1036
+ end
1037
+ end
1025
1038
  end
@@ -546,17 +546,7 @@ describe Mongoid::Association::Referenced::BelongsTo do
546
546
  end
547
547
 
548
548
  context 'when the global config option is true' do
549
-
550
- around(:example) do |example|
551
- original_config = Mongoid.belongs_to_required_by_default
552
- Mongoid.belongs_to_required_by_default = true
553
- example.run
554
- Mongoid.belongs_to_required_by_default = original_config
555
- end
556
-
557
- let!(:original_required_config) do
558
- Mongoid.belongs_to_required_by_default
559
- end
549
+ config_override :belongs_to_required_by_default, true
560
550
 
561
551
  context 'when the required option is true' do
562
552
 
@@ -684,13 +674,7 @@ describe Mongoid::Association::Referenced::BelongsTo do
684
674
  end
685
675
 
686
676
  context 'when the global config option is false' do
687
-
688
- around(:example) do |example|
689
- original_config = Mongoid.belongs_to_required_by_default
690
- Mongoid.belongs_to_required_by_default = false
691
- example.run
692
- Mongoid.belongs_to_required_by_default = original_config
693
- end
677
+ config_override :belongs_to_required_by_default, false
694
678
 
695
679
  context 'when the required option is true' do
696
680
 
@@ -4,14 +4,12 @@ require "spec_helper"
4
4
  require_relative "../has_and_belongs_to_many_models"
5
5
 
6
6
  describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
7
+ config_override :raise_not_found_error, true
7
8
 
8
9
  around(:each) do |example|
9
- original_raise_not_found_error = Mongoid.raise_not_found_error
10
10
  original_preferences_association = Person.relations["preferences"]
11
- Mongoid.raise_not_found_error = true
12
11
  Person.has_and_belongs_to_many :preferences, autosave: true
13
12
  example.run
14
- Mongoid.raise_not_found_error = original_raise_not_found_error
15
13
  Person.relations["preferences"] = original_preferences_association
16
14
  end
17
15
 
@@ -2469,10 +2467,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2469
2467
  context "when the id does not match" do
2470
2468
 
2471
2469
  context "when config set to raise error" do
2472
-
2473
- before do
2474
- Mongoid.raise_not_found_error = true
2475
- end
2470
+ config_override :raise_not_found_error, true
2476
2471
 
2477
2472
  it "raises an error" do
2478
2473
  expect {
@@ -2482,19 +2477,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2482
2477
  end
2483
2478
 
2484
2479
  context "when config set not to raise error" do
2480
+ config_override :raise_not_found_error, false
2485
2481
 
2486
2482
  let(:preference) do
2487
2483
  person.preferences.find(BSON::ObjectId.new)
2488
2484
  end
2489
2485
 
2490
- before do
2491
- Mongoid.raise_not_found_error = false
2492
- end
2493
-
2494
- after do
2495
- Mongoid.raise_not_found_error = true
2496
- end
2497
-
2498
2486
  it "returns nil" do
2499
2487
  expect(preference).to be_nil
2500
2488
  end
@@ -2533,10 +2521,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2533
2521
  context "when the ids do not match" do
2534
2522
 
2535
2523
  context "when config set to raise error" do
2536
-
2537
- before do
2538
- Mongoid.raise_not_found_error = true
2539
- end
2524
+ config_override :raise_not_found_error, true
2540
2525
 
2541
2526
  it "raises an error" do
2542
2527
  expect {
@@ -2546,19 +2531,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2546
2531
  end
2547
2532
 
2548
2533
  context "when config set not to raise error" do
2534
+ config_override :raise_not_found_error, false
2549
2535
 
2550
2536
  let(:preferences) do
2551
2537
  person.preferences.find([ BSON::ObjectId.new ])
2552
2538
  end
2553
2539
 
2554
- before do
2555
- Mongoid.raise_not_found_error = false
2556
- end
2557
-
2558
- after do
2559
- Mongoid.raise_not_found_error = true
2560
- end
2561
-
2562
2540
  it "returns an empty array" do
2563
2541
  expect(preferences).to be_empty
2564
2542
  end
@@ -23,10 +23,9 @@ module RefHasManySpec
23
23
  end
24
24
 
25
25
  describe Mongoid::Association::Referenced::HasMany::Proxy do
26
+ config_override :raise_not_found_error, true
26
27
 
27
28
  before :all do
28
- Mongoid.raise_not_found_error = true
29
-
30
29
  Drug.belongs_to :person, primary_key: :username
31
30
  Person.has_many :drugs, validate: false, primary_key: :username
32
31
  end
@@ -2535,10 +2534,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2535
2534
  context "when the id does not match" do
2536
2535
 
2537
2536
  context "when config set to raise error" do
2538
-
2539
- before do
2540
- Mongoid.raise_not_found_error = true
2541
- end
2537
+ config_override :raise_not_found_error, true
2542
2538
 
2543
2539
  it "raises an error" do
2544
2540
  expect {
@@ -2548,19 +2544,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2548
2544
  end
2549
2545
 
2550
2546
  context "when config set not to raise error" do
2547
+ config_override :raise_not_found_error, false
2551
2548
 
2552
2549
  let(:post) do
2553
2550
  person.posts.find(BSON::ObjectId.new)
2554
2551
  end
2555
2552
 
2556
- before do
2557
- Mongoid.raise_not_found_error = false
2558
- end
2559
-
2560
- after do
2561
- Mongoid.raise_not_found_error = true
2562
- end
2563
-
2564
2553
  it "returns nil" do
2565
2554
  expect(post).to be_nil
2566
2555
  end
@@ -2584,10 +2573,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2584
2573
  context "when the ids do not match" do
2585
2574
 
2586
2575
  context "when config set to raise error" do
2587
-
2588
- before do
2589
- Mongoid.raise_not_found_error = true
2590
- end
2576
+ config_override :raise_not_found_error, true
2591
2577
 
2592
2578
  it "raises an error" do
2593
2579
  expect {
@@ -2597,19 +2583,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2597
2583
  end
2598
2584
 
2599
2585
  context "when config set not to raise error" do
2586
+ config_override :raise_not_found_error, false
2600
2587
 
2601
2588
  let(:posts) do
2602
2589
  person.posts.find([ BSON::ObjectId.new ])
2603
2590
  end
2604
2591
 
2605
- before do
2606
- Mongoid.raise_not_found_error = false
2607
- end
2608
-
2609
- after do
2610
- Mongoid.raise_not_found_error = true
2611
- end
2612
-
2613
2592
  it "returns an empty array" do
2614
2593
  expect(posts).to be_empty
2615
2594
  end
@@ -2648,10 +2627,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2648
2627
  context "when the id does not match" do
2649
2628
 
2650
2629
  context "when config set to raise error" do
2651
-
2652
- before do
2653
- Mongoid.raise_not_found_error = true
2654
- end
2630
+ config_override :raise_not_found_error, true
2655
2631
 
2656
2632
  it "raises an error" do
2657
2633
  expect {
@@ -2661,19 +2637,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2661
2637
  end
2662
2638
 
2663
2639
  context "when config set not to raise error" do
2640
+ config_override :raise_not_found_error, false
2664
2641
 
2665
2642
  let(:rating) do
2666
2643
  movie.ratings.find(BSON::ObjectId.new)
2667
2644
  end
2668
2645
 
2669
- before do
2670
- Mongoid.raise_not_found_error = false
2671
- end
2672
-
2673
- after do
2674
- Mongoid.raise_not_found_error = true
2675
- end
2676
-
2677
2646
  it "returns nil" do
2678
2647
  expect(rating).to be_nil
2679
2648
  end
@@ -2705,10 +2674,7 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2705
2674
  context "when the ids do not match" do
2706
2675
 
2707
2676
  context "when config set to raise error" do
2708
-
2709
- before do
2710
- Mongoid.raise_not_found_error = true
2711
- end
2677
+ config_override :raise_not_found_error, true
2712
2678
 
2713
2679
  it "raises an error" do
2714
2680
  expect {
@@ -2718,19 +2684,12 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
2718
2684
  end
2719
2685
 
2720
2686
  context "when config set not to raise error" do
2687
+ config_override :raise_not_found_error, false
2721
2688
 
2722
2689
  let(:ratings) do
2723
2690
  movie.ratings.find([ BSON::ObjectId.new ])
2724
2691
  end
2725
2692
 
2726
- before do
2727
- Mongoid.raise_not_found_error = false
2728
- end
2729
-
2730
- after do
2731
- Mongoid.raise_not_found_error = true
2732
- end
2733
-
2734
2693
  it "returns an empty array" do
2735
2694
  expect(ratings).to be_empty
2736
2695
  end
@@ -3,9 +3,9 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe "Syncable Association" do
6
+ config_override :raise_not_found_error, true
6
7
 
7
8
  before(:all) do
8
- Mongoid.raise_not_found_error = true
9
9
  Person._synced(Person.relations["preferences"])
10
10
  Person.has_and_belongs_to_many :preferences
11
11
  Preference.has_and_belongs_to_many :people
@@ -393,11 +393,10 @@ describe Mongoid::Attributes do
393
393
  end
394
394
 
395
395
  context "when reloaded" do
396
+ config_override :raise_not_found_error, false
396
397
 
397
398
  before do
398
- Mongoid.raise_not_found_error = false
399
399
  person.reload
400
- Mongoid.raise_not_found_error = true
401
400
  end
402
401
 
403
402
  it "returns the default value" do
@@ -1030,14 +1029,13 @@ describe Mongoid::Attributes do
1030
1029
  end
1031
1030
 
1032
1031
  context "when the attribute does not exist" do
1032
+ config_override :raise_not_found_error, false
1033
1033
 
1034
1034
  before do
1035
1035
  person.collection
1036
1036
  .find({ _id: person.id })
1037
1037
  .update_one({ "$unset" => { age: 1 }})
1038
- Mongoid.raise_not_found_error = false
1039
1038
  person.reload
1040
- Mongoid.raise_not_found_error = true
1041
1039
  end
1042
1040
 
1043
1041
  it "returns the default value" do
@@ -1161,14 +1159,13 @@ describe Mongoid::Attributes do
1161
1159
  end
1162
1160
 
1163
1161
  context "when the attribute does not exist" do
1162
+ config_override :raise_not_found_error, false
1164
1163
 
1165
1164
  before do
1166
1165
  person.collection
1167
1166
  .find({ _id: person.id })
1168
1167
  .update_one({ "$unset" => { age: 1 }})
1169
- Mongoid.raise_not_found_error = false
1170
1168
  person.reload
1171
- Mongoid.raise_not_found_error = true
1172
1169
  end
1173
1170
 
1174
1171
  it "returns true" do
@@ -2711,31 +2708,4 @@ describe Mongoid::Attributes do
2711
2708
  catalog.set_field.should == Set.new([ 1, 2 ])
2712
2709
  end
2713
2710
  end
2714
-
2715
- context 'when an embedded field has a capitalized store_as name' do
2716
- let(:person) { Person.new(Purse: { brand: 'Gucci' }) }
2717
-
2718
- it 'sets the value' do
2719
- expect(person.purse.brand).to eq('Gucci')
2720
- end
2721
-
2722
- it 'saves successfully' do
2723
- expect(person.save!).to eq(true)
2724
- end
2725
-
2726
- context 'when persisted' do
2727
- before do
2728
- person.save!
2729
- person.reload
2730
- end
2731
-
2732
- it 'persists the value' do
2733
- expect(person.reload.purse.brand).to eq('Gucci')
2734
- end
2735
-
2736
- it 'uses the correct key in the database' do
2737
- expect(person.collection.find(_id: person.id).first['Purse']['_id']).to eq(person.purse.id)
2738
- end
2739
- end
2740
- end
2741
2711
  end