mongoid 8.0.3 → 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 (188) 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 +23 -21
  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 +5 -1
  19. data/lib/mongoid/association/referenced/has_many/enumerable.rb +2 -2
  20. data/lib/mongoid/association/referenced/has_many/proxy.rb +7 -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/extensions/array.rb +1 -1
  45. data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -1
  46. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +0 -8
  47. data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -11
  48. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +0 -10
  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/translator.rb +45 -0
  54. data/lib/mongoid/criteria.rb +7 -5
  55. data/lib/mongoid/deprecable.rb +1 -1
  56. data/lib/mongoid/document.rb +50 -13
  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/object.rb +2 -2
  65. data/lib/mongoid/extensions/time.rb +2 -0
  66. data/lib/mongoid/factory.rb +21 -8
  67. data/lib/mongoid/fields/localized.rb +10 -0
  68. data/lib/mongoid/fields/standard.rb +10 -0
  69. data/lib/mongoid/fields.rb +69 -13
  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/matcher.rb +21 -6
  75. data/lib/mongoid/persistable/creatable.rb +1 -0
  76. data/lib/mongoid/persistable/deletable.rb +1 -1
  77. data/lib/mongoid/persistable/savable.rb +13 -1
  78. data/lib/mongoid/persistable/unsettable.rb +2 -2
  79. data/lib/mongoid/persistable/updatable.rb +51 -1
  80. data/lib/mongoid/persistable/upsertable.rb +20 -1
  81. data/lib/mongoid/persistable.rb +3 -0
  82. data/lib/mongoid/query_cache.rb +5 -1
  83. data/lib/mongoid/railties/database.rake +7 -2
  84. data/lib/mongoid/shardable.rb +35 -11
  85. data/lib/mongoid/stateful.rb +22 -1
  86. data/lib/mongoid/tasks/database.rake +12 -0
  87. data/lib/mongoid/tasks/database.rb +20 -0
  88. data/lib/mongoid/threaded.rb +30 -0
  89. data/lib/mongoid/traversable.rb +1 -1
  90. data/lib/mongoid/utils.rb +22 -0
  91. data/lib/mongoid/validatable/macros.rb +5 -5
  92. data/lib/mongoid/validatable.rb +4 -1
  93. data/lib/mongoid/version.rb +1 -1
  94. data/lib/mongoid/warnings.rb +17 -1
  95. data/lib/mongoid.rb +16 -3
  96. data/spec/integration/app_spec.rb +2 -2
  97. data/spec/integration/callbacks_models.rb +37 -0
  98. data/spec/integration/callbacks_spec.rb +134 -0
  99. data/spec/integration/discriminator_key_spec.rb +4 -5
  100. data/spec/integration/i18n_fallbacks_spec.rb +3 -2
  101. data/spec/mongoid/association/embedded/embedded_in/proxy_spec.rb +27 -0
  102. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +57 -57
  103. data/spec/mongoid/association/embedded/embeds_many_models.rb +1 -0
  104. data/spec/mongoid/association/embedded/embeds_one/proxy_spec.rb +15 -2
  105. data/spec/mongoid/association/referenced/belongs_to_spec.rb +2 -18
  106. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +148 -224
  107. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +111 -164
  108. data/spec/mongoid/association/syncable_spec.rb +1 -1
  109. data/spec/mongoid/attributes_spec.rb +5 -8
  110. data/spec/mongoid/changeable_spec.rb +299 -24
  111. data/spec/mongoid/clients_spec.rb +122 -13
  112. data/spec/mongoid/collection_configurable_spec.rb +158 -0
  113. data/spec/mongoid/config/defaults_spec.rb +160 -0
  114. data/spec/mongoid/config_spec.rb +154 -18
  115. data/spec/mongoid/contextual/memory_spec.rb +332 -76
  116. data/spec/mongoid/contextual/mongo/documents_loader_spec.rb +187 -0
  117. data/spec/mongoid/contextual/mongo_spec.rb +995 -36
  118. data/spec/mongoid/contextual/none_spec.rb +49 -2
  119. data/spec/mongoid/copyable_spec.rb +3 -11
  120. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +4 -69
  121. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +0 -59
  122. data/spec/mongoid/criteria/queryable/optional_spec.rb +15 -0
  123. data/spec/mongoid/criteria/queryable/options_spec.rb +1 -1
  124. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +419 -0
  125. data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -1
  126. data/spec/mongoid/criteria/queryable/selector_spec.rb +1 -1
  127. data/spec/mongoid/criteria/translator_spec.rb +132 -0
  128. data/spec/mongoid/criteria_projection_spec.rb +1 -4
  129. data/spec/mongoid/criteria_spec.rb +5 -9
  130. data/spec/mongoid/errors/readonly_document_spec.rb +2 -2
  131. data/spec/mongoid/extensions/time_spec.rb +8 -43
  132. data/spec/mongoid/extensions/time_with_zone_spec.rb +7 -52
  133. data/spec/mongoid/fields/localized_spec.rb +46 -28
  134. data/spec/mongoid/fields_spec.rb +136 -34
  135. data/spec/mongoid/findable_spec.rb +391 -34
  136. data/spec/mongoid/indexable_spec.rb +16 -10
  137. data/spec/mongoid/interceptable_spec.rb +15 -3
  138. data/spec/mongoid/persistable/deletable_spec.rb +26 -6
  139. data/spec/mongoid/persistable/destroyable_spec.rb +26 -6
  140. data/spec/mongoid/persistable/incrementable_spec.rb +37 -0
  141. data/spec/mongoid/persistable/logical_spec.rb +37 -0
  142. data/spec/mongoid/persistable/poppable_spec.rb +36 -0
  143. data/spec/mongoid/persistable/pullable_spec.rb +72 -0
  144. data/spec/mongoid/persistable/pushable_spec.rb +72 -0
  145. data/spec/mongoid/persistable/renamable_spec.rb +36 -0
  146. data/spec/mongoid/persistable/savable_spec.rb +96 -0
  147. data/spec/mongoid/persistable/settable_spec.rb +37 -0
  148. data/spec/mongoid/persistable/unsettable_spec.rb +36 -0
  149. data/spec/mongoid/persistable/updatable_spec.rb +20 -28
  150. data/spec/mongoid/persistable/upsertable_spec.rb +80 -6
  151. data/spec/mongoid/persistence_context_spec.rb +7 -57
  152. data/spec/mongoid/query_cache_spec.rb +56 -61
  153. data/spec/mongoid/reloadable_spec.rb +24 -4
  154. data/spec/mongoid/scopable_spec.rb +70 -0
  155. data/spec/mongoid/serializable_spec.rb +9 -30
  156. data/spec/mongoid/shardable_models.rb +14 -0
  157. data/spec/mongoid/shardable_spec.rb +153 -61
  158. data/spec/mongoid/stateful_spec.rb +122 -8
  159. data/spec/mongoid/tasks/database_rake_spec.rb +74 -0
  160. data/spec/mongoid/tasks/database_spec.rb +127 -0
  161. data/spec/mongoid/timestamps_spec.rb +9 -11
  162. data/spec/mongoid/touchable_spec.rb +277 -5
  163. data/spec/mongoid/touchable_spec_models.rb +3 -1
  164. data/spec/mongoid/traversable_spec.rb +9 -24
  165. data/spec/mongoid/validatable/uniqueness_spec.rb +2 -3
  166. data/spec/mongoid_spec.rb +35 -9
  167. data/spec/shared/lib/mrss/docker_runner.rb +7 -0
  168. data/spec/shared/lib/mrss/event_subscriber.rb +15 -5
  169. data/spec/shared/lib/mrss/lite_constraints.rb +10 -2
  170. data/spec/shared/lib/mrss/server_version_registry.rb +16 -23
  171. data/spec/shared/lib/mrss/utils.rb +28 -6
  172. data/spec/shared/share/Dockerfile.erb +36 -40
  173. data/spec/shared/shlib/server.sh +32 -8
  174. data/spec/shared/shlib/set_env.sh +4 -4
  175. data/spec/spec_helper.rb +5 -0
  176. data/spec/support/immutable_ids.rb +118 -0
  177. data/spec/support/macros.rb +47 -15
  178. data/spec/support/models/artist.rb +0 -1
  179. data/spec/support/models/band.rb +1 -0
  180. data/spec/support/models/book.rb +1 -0
  181. data/spec/support/models/building.rb +2 -0
  182. data/spec/support/models/cover.rb +10 -0
  183. data/spec/support/models/product.rb +1 -0
  184. data.tar.gz.sig +0 -0
  185. metadata +700 -656
  186. metadata.gz.sig +0 -0
  187. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +0 -60
  188. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +0 -60
@@ -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
 
@@ -2087,283 +2085,229 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2087
2085
  end
2088
2086
  end
2089
2087
 
2090
- describe "#delete" do
2088
+ %i[ delete delete_one ].each do |method|
2089
+ describe "\##{method}" do
2090
+ let(:person) { Person.create! }
2091
+ let(:preference_one) { Preference.create!(name: "Testing") }
2092
+ let(:preference_two) { Preference.create!(name: "Test") }
2091
2093
 
2092
- let(:person) do
2093
- Person.create!
2094
- end
2095
-
2096
- let(:preference_one) do
2097
- Preference.create!(name: "Testing")
2098
- end
2099
-
2100
- let(:preference_two) do
2101
- Preference.create!(name: "Test")
2102
- end
2103
-
2104
- before do
2105
- person.preferences << [ preference_one, preference_two ]
2106
- end
2107
-
2108
- context "when the document exists" do
2109
-
2110
- let!(:deleted) do
2111
- person.preferences.delete(preference_one)
2112
- end
2113
-
2114
- it "removes the document from the relation" do
2115
- expect(person.preferences).to eq([ preference_two ])
2116
- end
2117
-
2118
- it "returns the document" do
2119
- expect(deleted).to eq(preference_one)
2120
- end
2121
-
2122
- it "removes the document key from the foreign key" do
2123
- expect(person.preference_ids).to eq([ preference_two.id ])
2124
- end
2125
-
2126
- it "removes the inverse reference" do
2127
- expect(deleted.reload.people).to be_empty
2128
- end
2129
-
2130
- it "removes the base id from the inverse keys" do
2131
- expect(deleted.reload.person_ids).to be_empty
2094
+ before do
2095
+ person.preferences << [ preference_one, preference_two ]
2132
2096
  end
2133
2097
 
2134
- context "and person and preferences are reloaded" do
2135
-
2136
- before do
2137
- person.reload
2138
- preference_one.reload
2139
- preference_two.reload
2098
+ context 'when the document exists' do
2099
+ let!(:deleted) do
2100
+ person.preferences.send(method, preference_one)
2140
2101
  end
2141
2102
 
2142
- it "nullifies the deleted preference" do
2103
+ it 'removes the document from the relation' do
2143
2104
  expect(person.preferences).to eq([ preference_two ])
2144
2105
  end
2145
2106
 
2146
- it "retains the ids for one preference" do
2147
- expect(person.preference_ids).to eq([ preference_two.id ])
2107
+ it 'returns the document' do
2108
+ expect(deleted).to eq(preference_one)
2148
2109
  end
2149
- end
2150
- end
2151
-
2152
- context "when the document does not exist" do
2153
-
2154
- let!(:deleted) do
2155
- person.preferences.delete(Preference.new)
2156
- end
2157
2110
 
2158
- it "returns nil" do
2159
- expect(deleted).to be_nil
2160
- end
2161
-
2162
- it "does not modify the relation" do
2163
- expect(person.preferences).to eq([ preference_one, preference_two ])
2164
- end
2165
-
2166
- it "does not modify the keys" do
2167
- expect(person.preference_ids).to eq([ preference_one.id, preference_two.id ])
2168
- end
2169
- end
2170
-
2171
- context "when :dependent => :nullify is set" do
2172
-
2173
- context "when :inverse_of is set" do
2174
-
2175
- let(:event) do
2176
- Event.create!
2111
+ it 'removes the document key from the foreign key' do
2112
+ expect(person.preference_ids).to eq([ preference_two.id ])
2177
2113
  end
2178
2114
 
2179
- before do
2180
- person.administrated_events << [ event ]
2115
+ it 'removes the inverse reference' do
2116
+ expect(deleted.reload.people).to be_empty
2181
2117
  end
2182
2118
 
2183
- it "deletes the document" do
2184
- expect(event.delete).to be true
2119
+ it 'removes the base id from the inverse keys' do
2120
+ expect(deleted.reload.person_ids).to be_empty
2185
2121
  end
2186
- end
2187
- end
2188
-
2189
- context "when the relationships are self referencing" do
2190
2122
 
2191
- let(:tag_one) do
2192
- Tag.create!(text: "one")
2193
- end
2123
+ context 'and person and preferences are reloaded' do
2124
+ before do
2125
+ person.reload
2126
+ preference_one.reload
2127
+ preference_two.reload
2128
+ end
2194
2129
 
2195
- let(:tag_two) do
2196
- Tag.create!(text: "two")
2197
- end
2130
+ it 'nullifies the deleted preference' do
2131
+ expect(person.preferences).to eq([ preference_two ])
2132
+ end
2198
2133
 
2199
- before do
2200
- tag_one.related << tag_two
2134
+ it 'retains the ids for one preference' do
2135
+ expect(person.preference_ids).to eq([ preference_two.id ])
2136
+ end
2137
+ end
2201
2138
  end
2202
2139
 
2203
- context "when deleting without reloading" do
2204
-
2140
+ context 'when the document does not exist' do
2205
2141
  let!(:deleted) do
2206
- tag_one.related.delete(tag_two)
2142
+ person.preferences.send(method, Preference.new)
2207
2143
  end
2208
2144
 
2209
- it "deletes the document from the relation" do
2210
- expect(tag_one.related).to be_empty
2145
+ it 'returns nil' do
2146
+ expect(deleted).to be_nil
2211
2147
  end
2212
2148
 
2213
- it "deletes the foreign key from the relation" do
2214
- expect(tag_one.related_ids).to be_empty
2149
+ it 'does not modify the relation' do
2150
+ expect(person.preferences).to eq([ preference_one, preference_two ])
2215
2151
  end
2216
2152
 
2217
- it "removes the reference from the inverse" do
2218
- expect(deleted.related).to be_empty
2219
- end
2220
-
2221
- it "removes the foreign keys from the inverse" do
2222
- expect(deleted.related_ids).to be_empty
2153
+ it 'does not modify the keys' do
2154
+ expect(person.preference_ids).to eq([ preference_one.id, preference_two.id ])
2223
2155
  end
2224
2156
  end
2225
2157
 
2226
- context "when deleting with reloading" do
2227
-
2228
- context "when deleting from the front side" do
2229
-
2230
- let(:reloaded) do
2231
- tag_one.reload
2232
- end
2233
-
2234
- let!(:deleted) do
2235
- reloaded.related.delete(tag_two)
2236
- end
2158
+ context 'when :dependent => :nullify is set' do
2159
+ context 'when :inverse_of is set' do
2160
+ let(:event) { Event.create! }
2237
2161
 
2238
- it "deletes the document from the relation" do
2239
- expect(reloaded.related).to be_empty
2162
+ before do
2163
+ person.administrated_events << [ event ]
2240
2164
  end
2241
2165
 
2242
- it "deletes the foreign key from the relation" do
2243
- expect(reloaded.related_ids).to be_empty
2166
+ it 'deletes the document' do
2167
+ expect(event.delete).to be true
2244
2168
  end
2169
+ end
2170
+ end
2245
2171
 
2246
- it "removes the reference from the inverse" do
2247
- expect(deleted.related).to be_empty
2248
- end
2172
+ context 'when the relationships are self referencing' do
2173
+ let(:tag_one) { Tag.create!(text: "one") }
2174
+ let(:tag_two) { Tag.create!(text: "two") }
2249
2175
 
2250
- it "removes the foreign keys from the inverse" do
2251
- expect(deleted.related_ids).to be_empty
2252
- end
2176
+ before do
2177
+ tag_one.related << tag_two
2253
2178
  end
2254
2179
 
2255
- context "when deleting from the inverse side" do
2180
+ context 'when deleting without reloading' do
2181
+ let!(:deleted) { tag_one.related.send(method, tag_two) }
2256
2182
 
2257
- let(:reloaded) do
2258
- tag_two.reload
2259
- end
2260
-
2261
- let!(:deleted) do
2262
- reloaded.related.delete(tag_one)
2183
+ it 'deletes the document from the relation' do
2184
+ expect(tag_one.related).to be_empty
2263
2185
  end
2264
2186
 
2265
- it "deletes the document from the relation" do
2266
- expect(reloaded.related).to be_empty
2187
+ it 'deletes the foreign key from the relation' do
2188
+ expect(tag_one.related_ids).to be_empty
2267
2189
  end
2268
2190
 
2269
- it "deletes the foreign key from the relation" do
2270
- expect(reloaded.related_ids).to be_empty
2191
+ it 'removes the reference from the inverse' do
2192
+ expect(deleted.related).to be_empty
2271
2193
  end
2272
2194
 
2273
- it "removes the foreign keys from the inverse" do
2195
+ it 'removes the foreign keys from the inverse' do
2274
2196
  expect(deleted.related_ids).to be_empty
2275
2197
  end
2276
2198
  end
2277
- end
2278
- end
2279
2199
 
2280
- context "when the association has callbacks" do
2200
+ context 'when deleting with reloading' do
2201
+ context "when deleting from the front side" do
2202
+ let(:reloaded) { tag_one.reload }
2203
+ let!(:deleted) { reloaded.related.send(method, tag_two) }
2281
2204
 
2282
- let(:post) do
2283
- Post.new
2284
- end
2205
+ it 'deletes the document from the relation' do
2206
+ expect(reloaded.related).to be_empty
2207
+ end
2285
2208
 
2286
- let(:tag) do
2287
- Tag.new
2288
- end
2209
+ it 'deletes the foreign key from the relation' do
2210
+ expect(reloaded.related_ids).to be_empty
2211
+ end
2289
2212
 
2290
- before do
2291
- post.tags << tag
2292
- end
2213
+ it 'removes the reference from the inverse' do
2214
+ expect(deleted.related).to be_empty
2215
+ end
2293
2216
 
2294
- context "when the callback is a before_remove" do
2217
+ it 'removes the foreign keys from the inverse' do
2218
+ expect(deleted.related_ids).to be_empty
2219
+ end
2220
+ end
2295
2221
 
2296
- context "when there are no errors" do
2222
+ context 'when deleting from the inverse side' do
2223
+ let(:reloaded) { tag_two.reload }
2224
+ let!(:deleted) { reloaded.related.send(method, tag_one) }
2297
2225
 
2298
- before do
2299
- post.tags.delete tag
2300
- end
2226
+ it 'deletes the document from the relation' do
2227
+ expect(reloaded.related).to be_empty
2228
+ end
2301
2229
 
2302
- it "executes the callback" do
2303
- expect(post.before_remove_called).to be true
2304
- end
2230
+ it 'deletes the foreign key from the relation' do
2231
+ expect(reloaded.related_ids).to be_empty
2232
+ end
2305
2233
 
2306
- it "removes the document from the relation" do
2307
- expect(post.tags).to be_empty
2234
+ it 'removes the foreign keys from the inverse' do
2235
+ expect(deleted.related_ids).to be_empty
2236
+ end
2308
2237
  end
2309
2238
  end
2239
+ end
2310
2240
 
2311
- context "when errors are raised" do
2241
+ context 'when the association has callbacks' do
2242
+ let(:post) { Post.new }
2243
+ let(:tag) { Tag.new }
2312
2244
 
2313
- before do
2314
- expect(post).to receive(:before_remove_tag).and_raise
2315
- begin; post.tags.delete(tag); rescue; end
2316
- end
2317
-
2318
- it "does not remove the document from the relation" do
2319
- expect(post.tags).to eq([ tag ])
2320
- end
2245
+ before do
2246
+ post.tags << tag
2321
2247
  end
2322
- end
2323
2248
 
2324
- context "when the callback is an after_remove" do
2249
+ context 'when the callback is a before_remove' do
2250
+ context 'when there are no errors' do
2251
+ before do
2252
+ post.tags.send(method, tag)
2253
+ end
2325
2254
 
2326
- context "when no errors are raised" do
2255
+ it 'executes the callback' do
2256
+ expect(post.before_remove_called).to be true
2257
+ end
2327
2258
 
2328
- before do
2329
- post.tags.delete(tag)
2259
+ it 'removes the document from the relation' do
2260
+ expect(post.tags).to be_empty
2261
+ end
2330
2262
  end
2331
2263
 
2332
- it "executes the callback" do
2333
- expect(post.after_remove_called).to be true
2334
- end
2264
+ context "when errors are raised" do
2265
+ before do
2266
+ expect(post).to receive(:before_remove_tag).and_raise
2267
+ begin; post.tags.send(method, tag); rescue; end
2268
+ end
2335
2269
 
2336
- it "removes the document from the relation" do
2337
- expect(post.tags).to be_empty
2270
+ it 'does not remove the document from the relation' do
2271
+ expect(post.tags).to eq([ tag ])
2272
+ end
2338
2273
  end
2339
2274
  end
2340
2275
 
2341
- context "when errors are raised" do
2276
+ context 'when the callback is an after_remove' do
2277
+ context 'when no errors are raised' do
2278
+ before do
2279
+ post.tags.send(method, tag)
2280
+ end
2342
2281
 
2343
- before do
2344
- expect(post).to receive(:after_remove_tag).and_raise
2345
- begin; post.tags.delete(tag); rescue; end
2282
+ it 'executes the callback' do
2283
+ expect(post.after_remove_called).to be true
2284
+ end
2285
+
2286
+ it 'removes the document from the relation' do
2287
+ expect(post.tags).to be_empty
2288
+ end
2346
2289
  end
2347
2290
 
2348
- it "removes the document from the relation" do
2349
- expect(post.tags).to be_empty
2291
+ context 'when errors are raised' do
2292
+ before do
2293
+ expect(post).to receive(:after_remove_tag).and_raise
2294
+ begin; post.tags.send(method, tag); rescue; end
2295
+ end
2296
+
2297
+ it 'removes the document from the relation' do
2298
+ expect(post.tags).to be_empty
2299
+ end
2350
2300
  end
2351
2301
  end
2352
2302
  end
2353
2303
  end
2354
2304
  end
2355
2305
 
2356
- [ :delete_all, :destroy_all ].each do |method|
2357
-
2358
- describe "##{method}" do
2359
-
2360
- context "when the relation is not polymorphic" do
2361
-
2362
- context "when conditions are provided" do
2363
-
2364
- let(:person) do
2365
- Person.create!
2366
- end
2306
+ %i[ delete_all destroy_all ].each do |method|
2307
+ describe "\##{method}" do
2308
+ context 'when the relation is not polymorphic' do
2309
+ context 'when conditions are provided' do
2310
+ let(:person) { Person.create! }
2367
2311
 
2368
2312
  let!(:preference_one) do
2369
2313
  person.preferences.create!(name: "Testing")
@@ -2523,10 +2467,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2523
2467
  context "when the id does not match" do
2524
2468
 
2525
2469
  context "when config set to raise error" do
2526
-
2527
- before do
2528
- Mongoid.raise_not_found_error = true
2529
- end
2470
+ config_override :raise_not_found_error, true
2530
2471
 
2531
2472
  it "raises an error" do
2532
2473
  expect {
@@ -2536,19 +2477,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2536
2477
  end
2537
2478
 
2538
2479
  context "when config set not to raise error" do
2480
+ config_override :raise_not_found_error, false
2539
2481
 
2540
2482
  let(:preference) do
2541
2483
  person.preferences.find(BSON::ObjectId.new)
2542
2484
  end
2543
2485
 
2544
- before do
2545
- Mongoid.raise_not_found_error = false
2546
- end
2547
-
2548
- after do
2549
- Mongoid.raise_not_found_error = true
2550
- end
2551
-
2552
2486
  it "returns nil" do
2553
2487
  expect(preference).to be_nil
2554
2488
  end
@@ -2587,10 +2521,7 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2587
2521
  context "when the ids do not match" do
2588
2522
 
2589
2523
  context "when config set to raise error" do
2590
-
2591
- before do
2592
- Mongoid.raise_not_found_error = true
2593
- end
2524
+ config_override :raise_not_found_error, true
2594
2525
 
2595
2526
  it "raises an error" do
2596
2527
  expect {
@@ -2600,19 +2531,12 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2600
2531
  end
2601
2532
 
2602
2533
  context "when config set not to raise error" do
2534
+ config_override :raise_not_found_error, false
2603
2535
 
2604
2536
  let(:preferences) do
2605
2537
  person.preferences.find([ BSON::ObjectId.new ])
2606
2538
  end
2607
2539
 
2608
- before do
2609
- Mongoid.raise_not_found_error = false
2610
- end
2611
-
2612
- after do
2613
- Mongoid.raise_not_found_error = true
2614
- end
2615
-
2616
2540
  it "returns an empty array" do
2617
2541
  expect(preferences).to be_empty
2618
2542
  end