mongoid 8.0.3 → 8.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/mongoid/association/embedded/embeds_many/proxy.rb +17 -15
  4. data/lib/mongoid/association/referenced/has_and_belongs_to_many/proxy.rb +4 -0
  5. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -0
  6. data/lib/mongoid/criteria/queryable/extensions/array.rb +1 -1
  7. data/lib/mongoid/criteria/queryable/extensions/hash.rb +1 -1
  8. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +0 -8
  9. data/lib/mongoid/criteria/queryable/extensions/string.rb +1 -11
  10. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +0 -10
  11. data/lib/mongoid/criteria/translator.rb +45 -0
  12. data/lib/mongoid/criteria.rb +1 -0
  13. data/lib/mongoid/document.rb +50 -13
  14. data/lib/mongoid/factory.rb +21 -8
  15. data/lib/mongoid/matcher.rb +21 -6
  16. data/lib/mongoid/shardable.rb +35 -11
  17. data/lib/mongoid/threaded.rb +30 -0
  18. data/lib/mongoid/traversable.rb +1 -1
  19. data/lib/mongoid/version.rb +1 -1
  20. data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +37 -32
  21. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +143 -197
  22. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +102 -114
  23. data/spec/mongoid/attributes_spec.rb +2 -2
  24. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +0 -59
  25. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +0 -59
  26. data/spec/mongoid/criteria/queryable/optional_spec.rb +15 -0
  27. data/spec/mongoid/criteria/translator_spec.rb +132 -0
  28. data/spec/mongoid/shardable_models.rb +14 -0
  29. data/spec/mongoid/shardable_spec.rb +153 -61
  30. data.tar.gz.sig +0 -0
  31. metadata +656 -648
  32. metadata.gz.sig +0 -0
@@ -1862,51 +1862,56 @@ describe Mongoid::Association::Embedded::EmbedsMany::Proxy do
1862
1862
  end
1863
1863
  end
1864
1864
 
1865
- describe "#delete" do
1865
+ %i[ delete delete_one ].each do |method|
1866
+ describe "\##{method}" do
1867
+ let(:address_one) { Address.new(street: "first") }
1868
+ let(:address_two) { Address.new(street: "second") }
1866
1869
 
1867
- let(:person) do
1868
- Person.new
1869
- end
1870
-
1871
- let(:address_one) do
1872
- Address.new(street: "first")
1873
- end
1870
+ before do
1871
+ person.addresses << [ address_one, address_two ]
1872
+ end
1874
1873
 
1875
- let(:address_two) do
1876
- Address.new(street: "second")
1877
- end
1874
+ shared_examples_for 'deleting from the collection' do
1875
+ context 'when the document exists in the relation' do
1876
+ let!(:deleted) do
1877
+ person.addresses.send(method, address_one)
1878
+ end
1878
1879
 
1879
- before do
1880
- person.addresses << [ address_one, address_two ]
1881
- end
1880
+ it 'deletes the document' do
1881
+ expect(person.addresses).to eq([ address_two ])
1882
+ expect(person.reload.addresses).to eq([ address_two ]) if person.persisted?
1883
+ end
1882
1884
 
1883
- context "when the document exists in the relation" do
1885
+ it 'deletes the document from the unscoped' do
1886
+ expect(person.addresses.send(:_unscoped)).to eq([ address_two ])
1887
+ end
1884
1888
 
1885
- let!(:deleted) do
1886
- person.addresses.delete(address_one)
1887
- end
1889
+ it 'reindexes the relation' do
1890
+ expect(address_two._index).to eq(0)
1891
+ end
1888
1892
 
1889
- it "deletes the document" do
1890
- expect(person.addresses).to eq([ address_two ])
1891
- end
1893
+ it 'returns the document' do
1894
+ expect(deleted).to eq(address_one)
1895
+ end
1896
+ end
1892
1897
 
1893
- it "deletes the document from the unscoped" do
1894
- expect(person.addresses.send(:_unscoped)).to eq([ address_two ])
1898
+ context 'when the document does not exist' do
1899
+ it 'returns nil' do
1900
+ expect(person.addresses.send(method, Address.new)).to be_nil
1901
+ end
1902
+ end
1895
1903
  end
1896
1904
 
1897
- it "reindexes the relation" do
1898
- expect(address_two._index).to eq(0)
1899
- end
1905
+ context 'when the root document is unpersisted' do
1906
+ let(:person) { Person.new }
1900
1907
 
1901
- it "returns the document" do
1902
- expect(deleted).to eq(address_one)
1908
+ it_behaves_like 'deleting from the collection'
1903
1909
  end
1904
- end
1905
1910
 
1906
- context "when the document does not exist" do
1911
+ context 'when the root document is persisted' do
1912
+ let(:person) { Person.create }
1907
1913
 
1908
- it "returns nil" do
1909
- expect(person.addresses.delete(Address.new)).to be_nil
1914
+ it_behaves_like 'deleting from the collection'
1910
1915
  end
1911
1916
  end
1912
1917
  end
@@ -2087,283 +2087,229 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
2087
2087
  end
2088
2088
  end
2089
2089
 
2090
- describe "#delete" do
2090
+ %i[ delete delete_one ].each do |method|
2091
+ describe "\##{method}" do
2092
+ let(:person) { Person.create! }
2093
+ let(:preference_one) { Preference.create!(name: "Testing") }
2094
+ let(:preference_two) { Preference.create!(name: "Test") }
2091
2095
 
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
2096
+ before do
2097
+ person.preferences << [ preference_one, preference_two ]
2132
2098
  end
2133
2099
 
2134
- context "and person and preferences are reloaded" do
2135
-
2136
- before do
2137
- person.reload
2138
- preference_one.reload
2139
- preference_two.reload
2100
+ context 'when the document exists' do
2101
+ let!(:deleted) do
2102
+ person.preferences.send(method, preference_one)
2140
2103
  end
2141
2104
 
2142
- it "nullifies the deleted preference" do
2105
+ it 'removes the document from the relation' do
2143
2106
  expect(person.preferences).to eq([ preference_two ])
2144
2107
  end
2145
2108
 
2146
- it "retains the ids for one preference" do
2147
- expect(person.preference_ids).to eq([ preference_two.id ])
2109
+ it 'returns the document' do
2110
+ expect(deleted).to eq(preference_one)
2148
2111
  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
-
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
2112
 
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!
2113
+ it 'removes the document key from the foreign key' do
2114
+ expect(person.preference_ids).to eq([ preference_two.id ])
2177
2115
  end
2178
2116
 
2179
- before do
2180
- person.administrated_events << [ event ]
2117
+ it 'removes the inverse reference' do
2118
+ expect(deleted.reload.people).to be_empty
2181
2119
  end
2182
2120
 
2183
- it "deletes the document" do
2184
- expect(event.delete).to be true
2121
+ it 'removes the base id from the inverse keys' do
2122
+ expect(deleted.reload.person_ids).to be_empty
2185
2123
  end
2186
- end
2187
- end
2188
-
2189
- context "when the relationships are self referencing" do
2190
2124
 
2191
- let(:tag_one) do
2192
- Tag.create!(text: "one")
2193
- end
2125
+ context 'and person and preferences are reloaded' do
2126
+ before do
2127
+ person.reload
2128
+ preference_one.reload
2129
+ preference_two.reload
2130
+ end
2194
2131
 
2195
- let(:tag_two) do
2196
- Tag.create!(text: "two")
2197
- end
2132
+ it 'nullifies the deleted preference' do
2133
+ expect(person.preferences).to eq([ preference_two ])
2134
+ end
2198
2135
 
2199
- before do
2200
- tag_one.related << tag_two
2136
+ it 'retains the ids for one preference' do
2137
+ expect(person.preference_ids).to eq([ preference_two.id ])
2138
+ end
2139
+ end
2201
2140
  end
2202
2141
 
2203
- context "when deleting without reloading" do
2204
-
2142
+ context 'when the document does not exist' do
2205
2143
  let!(:deleted) do
2206
- tag_one.related.delete(tag_two)
2207
- end
2208
-
2209
- it "deletes the document from the relation" do
2210
- expect(tag_one.related).to be_empty
2144
+ person.preferences.send(method, Preference.new)
2211
2145
  end
2212
2146
 
2213
- it "deletes the foreign key from the relation" do
2214
- expect(tag_one.related_ids).to be_empty
2147
+ it 'returns nil' do
2148
+ expect(deleted).to be_nil
2215
2149
  end
2216
2150
 
2217
- it "removes the reference from the inverse" do
2218
- expect(deleted.related).to be_empty
2151
+ it 'does not modify the relation' do
2152
+ expect(person.preferences).to eq([ preference_one, preference_two ])
2219
2153
  end
2220
2154
 
2221
- it "removes the foreign keys from the inverse" do
2222
- expect(deleted.related_ids).to be_empty
2155
+ it 'does not modify the keys' do
2156
+ expect(person.preference_ids).to eq([ preference_one.id, preference_two.id ])
2223
2157
  end
2224
2158
  end
2225
2159
 
2226
- context "when deleting with reloading" do
2160
+ context 'when :dependent => :nullify is set' do
2161
+ context 'when :inverse_of is set' do
2162
+ let(:event) { Event.create! }
2227
2163
 
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
2237
-
2238
- it "deletes the document from the relation" do
2239
- expect(reloaded.related).to be_empty
2164
+ before do
2165
+ person.administrated_events << [ event ]
2240
2166
  end
2241
2167
 
2242
- it "deletes the foreign key from the relation" do
2243
- expect(reloaded.related_ids).to be_empty
2168
+ it 'deletes the document' do
2169
+ expect(event.delete).to be true
2244
2170
  end
2171
+ end
2172
+ end
2245
2173
 
2246
- it "removes the reference from the inverse" do
2247
- expect(deleted.related).to be_empty
2248
- end
2174
+ context 'when the relationships are self referencing' do
2175
+ let(:tag_one) { Tag.create!(text: "one") }
2176
+ let(:tag_two) { Tag.create!(text: "two") }
2249
2177
 
2250
- it "removes the foreign keys from the inverse" do
2251
- expect(deleted.related_ids).to be_empty
2252
- end
2178
+ before do
2179
+ tag_one.related << tag_two
2253
2180
  end
2254
2181
 
2255
- context "when deleting from the inverse side" do
2256
-
2257
- let(:reloaded) do
2258
- tag_two.reload
2259
- end
2182
+ context 'when deleting without reloading' do
2183
+ let!(:deleted) { tag_one.related.send(method, tag_two) }
2260
2184
 
2261
- let!(:deleted) do
2262
- reloaded.related.delete(tag_one)
2185
+ it 'deletes the document from the relation' do
2186
+ expect(tag_one.related).to be_empty
2263
2187
  end
2264
2188
 
2265
- it "deletes the document from the relation" do
2266
- expect(reloaded.related).to be_empty
2189
+ it 'deletes the foreign key from the relation' do
2190
+ expect(tag_one.related_ids).to be_empty
2267
2191
  end
2268
2192
 
2269
- it "deletes the foreign key from the relation" do
2270
- expect(reloaded.related_ids).to be_empty
2193
+ it 'removes the reference from the inverse' do
2194
+ expect(deleted.related).to be_empty
2271
2195
  end
2272
2196
 
2273
- it "removes the foreign keys from the inverse" do
2197
+ it 'removes the foreign keys from the inverse' do
2274
2198
  expect(deleted.related_ids).to be_empty
2275
2199
  end
2276
2200
  end
2277
- end
2278
- end
2279
2201
 
2280
- context "when the association has callbacks" do
2202
+ context 'when deleting with reloading' do
2203
+ context "when deleting from the front side" do
2204
+ let(:reloaded) { tag_one.reload }
2205
+ let!(:deleted) { reloaded.related.send(method, tag_two) }
2281
2206
 
2282
- let(:post) do
2283
- Post.new
2284
- end
2207
+ it 'deletes the document from the relation' do
2208
+ expect(reloaded.related).to be_empty
2209
+ end
2285
2210
 
2286
- let(:tag) do
2287
- Tag.new
2288
- end
2211
+ it 'deletes the foreign key from the relation' do
2212
+ expect(reloaded.related_ids).to be_empty
2213
+ end
2289
2214
 
2290
- before do
2291
- post.tags << tag
2292
- end
2215
+ it 'removes the reference from the inverse' do
2216
+ expect(deleted.related).to be_empty
2217
+ end
2293
2218
 
2294
- context "when the callback is a before_remove" do
2219
+ it 'removes the foreign keys from the inverse' do
2220
+ expect(deleted.related_ids).to be_empty
2221
+ end
2222
+ end
2295
2223
 
2296
- context "when there are no errors" do
2224
+ context 'when deleting from the inverse side' do
2225
+ let(:reloaded) { tag_two.reload }
2226
+ let!(:deleted) { reloaded.related.send(method, tag_one) }
2297
2227
 
2298
- before do
2299
- post.tags.delete tag
2300
- end
2228
+ it 'deletes the document from the relation' do
2229
+ expect(reloaded.related).to be_empty
2230
+ end
2301
2231
 
2302
- it "executes the callback" do
2303
- expect(post.before_remove_called).to be true
2304
- end
2232
+ it 'deletes the foreign key from the relation' do
2233
+ expect(reloaded.related_ids).to be_empty
2234
+ end
2305
2235
 
2306
- it "removes the document from the relation" do
2307
- expect(post.tags).to be_empty
2236
+ it 'removes the foreign keys from the inverse' do
2237
+ expect(deleted.related_ids).to be_empty
2238
+ end
2308
2239
  end
2309
2240
  end
2241
+ end
2310
2242
 
2311
- context "when errors are raised" do
2312
-
2313
- before do
2314
- expect(post).to receive(:before_remove_tag).and_raise
2315
- begin; post.tags.delete(tag); rescue; end
2316
- end
2243
+ context 'when the association has callbacks' do
2244
+ let(:post) { Post.new }
2245
+ let(:tag) { Tag.new }
2317
2246
 
2318
- it "does not remove the document from the relation" do
2319
- expect(post.tags).to eq([ tag ])
2320
- end
2247
+ before do
2248
+ post.tags << tag
2321
2249
  end
2322
- end
2323
2250
 
2324
- context "when the callback is an after_remove" do
2251
+ context 'when the callback is a before_remove' do
2252
+ context 'when there are no errors' do
2253
+ before do
2254
+ post.tags.send(method, tag)
2255
+ end
2325
2256
 
2326
- context "when no errors are raised" do
2257
+ it 'executes the callback' do
2258
+ expect(post.before_remove_called).to be true
2259
+ end
2327
2260
 
2328
- before do
2329
- post.tags.delete(tag)
2261
+ it 'removes the document from the relation' do
2262
+ expect(post.tags).to be_empty
2263
+ end
2330
2264
  end
2331
2265
 
2332
- it "executes the callback" do
2333
- expect(post.after_remove_called).to be true
2334
- end
2266
+ context "when errors are raised" do
2267
+ before do
2268
+ expect(post).to receive(:before_remove_tag).and_raise
2269
+ begin; post.tags.send(method, tag); rescue; end
2270
+ end
2335
2271
 
2336
- it "removes the document from the relation" do
2337
- expect(post.tags).to be_empty
2272
+ it 'does not remove the document from the relation' do
2273
+ expect(post.tags).to eq([ tag ])
2274
+ end
2338
2275
  end
2339
2276
  end
2340
2277
 
2341
- context "when errors are raised" do
2278
+ context 'when the callback is an after_remove' do
2279
+ context 'when no errors are raised' do
2280
+ before do
2281
+ post.tags.send(method, tag)
2282
+ end
2342
2283
 
2343
- before do
2344
- expect(post).to receive(:after_remove_tag).and_raise
2345
- begin; post.tags.delete(tag); rescue; end
2284
+ it 'executes the callback' do
2285
+ expect(post.after_remove_called).to be true
2286
+ end
2287
+
2288
+ it 'removes the document from the relation' do
2289
+ expect(post.tags).to be_empty
2290
+ end
2346
2291
  end
2347
2292
 
2348
- it "removes the document from the relation" do
2349
- expect(post.tags).to be_empty
2293
+ context 'when errors are raised' do
2294
+ before do
2295
+ expect(post).to receive(:after_remove_tag).and_raise
2296
+ begin; post.tags.send(method, tag); rescue; end
2297
+ end
2298
+
2299
+ it 'removes the document from the relation' do
2300
+ expect(post.tags).to be_empty
2301
+ end
2350
2302
  end
2351
2303
  end
2352
2304
  end
2353
2305
  end
2354
2306
  end
2355
2307
 
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
2308
+ %i[ delete_all destroy_all ].each do |method|
2309
+ describe "\##{method}" do
2310
+ context 'when the relation is not polymorphic' do
2311
+ context 'when conditions are provided' do
2312
+ let(:person) { Person.create! }
2367
2313
 
2368
2314
  let!(:preference_one) do
2369
2315
  person.preferences.create!(name: "Testing")