mongoid 9.0.13 → 9.0.14

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +13 -0
  3. data/lib/mongoid/association/referenced/belongs_to.rb +16 -8
  4. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  5. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  6. data/lib/mongoid/attributes/readonly.rb +19 -1
  7. data/lib/mongoid/changeable.rb +21 -1
  8. data/lib/mongoid/config/encryption.rb +5 -3
  9. data/lib/mongoid/contextual/none.rb +51 -0
  10. data/lib/mongoid/criteria/scopable.rb +7 -1
  11. data/lib/mongoid/criteria.rb +45 -1
  12. data/lib/mongoid/document.rb +10 -0
  13. data/lib/mongoid/encryptable.rb +1 -1
  14. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  15. data/lib/mongoid/errors.rb +1 -0
  16. data/lib/mongoid/fields/encrypted.rb +14 -3
  17. data/lib/mongoid/fields/foreign_key.rb +9 -0
  18. data/lib/mongoid/fields/standard.rb +10 -0
  19. data/lib/mongoid/fields/validators/macro.rb +29 -0
  20. data/lib/mongoid/fields.rb +42 -2
  21. data/lib/mongoid/reloadable.rb +1 -0
  22. data/lib/mongoid/version.rb +1 -1
  23. data/spec/integration/encryption_spec.rb +269 -0
  24. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +144 -0
  25. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  26. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
  27. data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
  28. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  29. data/spec/mongoid/changeable_spec.rb +18 -0
  30. data/spec/mongoid/config/encryption_spec.rb +227 -0
  31. data/spec/mongoid/contextual/none_spec.rb +22 -5
  32. data/spec/mongoid/criteria_spec.rb +17 -0
  33. data/spec/mongoid/fields_spec.rb +29 -0
  34. data/spec/mongoid/findable_spec.rb +14 -0
  35. data/spec/mongoid/scopable_spec.rb +103 -2
  36. data/spec/spec_helper.rb +8 -0
  37. data/spec/support/crypt/models.rb +42 -0
  38. data/spec/support/models/delete_all_account.rb +10 -0
  39. data/spec/support/models/delete_all_item.rb +9 -0
  40. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  41. data/spec/support/models/delete_all_tag.rb +7 -0
  42. metadata +1995 -1961
@@ -1489,6 +1489,23 @@ describe Mongoid::Criteria do
1489
1489
  expect(merged).to equal(criteria)
1490
1490
  end
1491
1491
  end
1492
+
1493
+ context 'when merging a none criteria into a criteria with a cached context' do
1494
+ let(:criteria) { Band.where(name: 'Depeche Mode') }
1495
+ let(:other) { Band.none }
1496
+
1497
+ before do
1498
+ Band.create!(name: 'Depeche Mode')
1499
+ end
1500
+
1501
+ it 'does not use the cached context' do
1502
+ expect(criteria.count).to eq(1)
1503
+
1504
+ merged
1505
+
1506
+ expect(criteria.count).to eq(0)
1507
+ end
1508
+ end
1492
1509
  end
1493
1510
 
1494
1511
  describe "#min" do
@@ -1281,6 +1281,35 @@ describe Mongoid::Fields do
1281
1281
  end
1282
1282
  end
1283
1283
 
1284
+ context 'when a subclass declares an encrypted field and the parent declares it as a plain field' do
1285
+ let(:parent) do
1286
+ klass = stub_const('PlainFieldParent', Class.new)
1287
+ klass.include(Mongoid::Document)
1288
+ klass
1289
+ end
1290
+
1291
+ let(:child) do
1292
+ parent
1293
+ klass = stub_const('PlainFieldChild', Class.new(parent))
1294
+ klass.field(:secret, type: String, encrypt: { deterministic: true })
1295
+ klass
1296
+ end
1297
+
1298
+ after do
1299
+ Mongoid.deregister_model(child)
1300
+ Mongoid.deregister_model(parent)
1301
+ end
1302
+
1303
+ it 'replaces the encrypted field on the subclass' do
1304
+ child
1305
+
1306
+ parent.field(:secret, type: String)
1307
+
1308
+ expect(parent.fields['secret']).not_to be_encrypted
1309
+ expect(child.fields['secret']).not_to be_encrypted
1310
+ end
1311
+ end
1312
+
1284
1313
  context "custom options" do
1285
1314
 
1286
1315
  let(:handler) do
@@ -818,6 +818,20 @@ describe Mongoid::Findable do
818
818
  expect(criteria.size).to eq(0)
819
819
  end
820
820
  end
821
+
822
+ context 'when none is called on a criteria with a cached context' do
823
+ let(:criteria) do
824
+ Band.where(name: 'Depeche Mode')
825
+ end
826
+
827
+ it 'does not use the cached context' do
828
+ expect(criteria.count).to eq(1)
829
+
830
+ criteria.none
831
+
832
+ expect(criteria.count).to eq(0)
833
+ end
834
+ end
821
835
  end
822
836
 
823
837
  describe ".pluck" do
@@ -156,9 +156,92 @@ describe Mongoid::Scopable do
156
156
  end
157
157
  end
158
158
 
159
- context "when parent class has default scope" do
159
+ context 'when the default scope is none' do
160
+ before do
161
+ Band.create!(name: 'Depeche Mode', active: true)
162
+ Band.default_scope -> { Band.none }
163
+ end
164
+
165
+ after do
166
+ Band.default_scoping = nil
167
+ end
168
+
169
+ it 'does not return any documents' do
170
+ expect(Band.count).to eq(0)
171
+ expect(Band.all.to_a).to be_empty
172
+ expect(Band.where(name: 'Depeche Mode').first).to be_nil
173
+ end
174
+
175
+ it 'does not update documents' do
176
+ Band.update_all(active: false)
177
+ expect(Band.unscoped.first.active).to be(true)
178
+ end
179
+
180
+ it 'does not delete documents' do
181
+ expect(Band.delete_all).to eq(0)
182
+ expect(Band.unscoped.count).to eq(1)
183
+ end
184
+
185
+ it 'removes the none from the default scope when unscoping' do
186
+ crit = Band.all
187
+ expect(crit.count).to eq(0)
188
+ expect(crit.unscoped.count).to eq(1)
189
+ expect(Band.all.merge(Band.all).unscoped.count).to eq(1)
190
+ end
191
+
192
+ it 'keeps an explicit none when unscoping' do
193
+ expect(Band.none.unscoped.count).to eq(0)
194
+ expect(Band.all.none.unscoped.count).to eq(0)
195
+ expect(Band.all.merge(Band.none).unscoped.count).to eq(0)
196
+ end
197
+ end
198
+
199
+ context 'when the default scope is conditionally none' do
200
+ before do
201
+ Band.create!(name: 'Depeche Mode', active: true)
202
+ Band.default_scope -> { allow_all ? Band.where(active: true) : Band.none }
203
+ end
160
204
 
161
- let (:selector) do
205
+ after do
206
+ Band.default_scoping = nil
207
+ end
208
+
209
+ context 'when the condition restricts the scope' do
210
+ let(:allow_all) { false }
211
+
212
+ it 'does not return any documents' do
213
+ expect(Band.count).to eq(0)
214
+ end
215
+ end
216
+
217
+ context 'when the condition allows all documents' do
218
+ let(:allow_all) { true }
219
+
220
+ it 'returns the documents' do
221
+ expect(Band.count).to eq(1)
222
+ end
223
+ end
224
+ end
225
+
226
+ context 'when multiple default scopes are combined with none last' do
227
+ before do
228
+ Band.create!(name: 'Depeche Mode', active: true)
229
+ Band.default_scope -> { Band.where(active: true) }
230
+ Band.default_scope -> { Band.none }
231
+ end
232
+
233
+ after do
234
+ Band.default_scoping = nil
235
+ end
236
+
237
+ it 'does not return any documents' do
238
+ expect(Band.count).to eq(0)
239
+ expect(Band.all.to_a).to be_empty
240
+ end
241
+ end
242
+
243
+ context 'when parent class has default scope' do
244
+ let(:selector) do
162
245
  AudibleSound.all.selector
163
246
  end
164
247
 
@@ -927,6 +1010,24 @@ describe Mongoid::Scopable do
927
1010
  expect(Circle.visible.selector).to eq("radius" => 5)
928
1011
  end
929
1012
  end
1013
+
1014
+ context 'when the scope name matches an internal criteria helper' do
1015
+ before do
1016
+ Band.scope(:none_state, -> { where(active: true) })
1017
+ Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
1018
+ end
1019
+
1020
+ after do
1021
+ remove_scope(Band, :none_state)
1022
+ remove_scope(Band, :clear_default_scope_none)
1023
+ end
1024
+
1025
+ it 'applies both scopes when chained on a criteria' do
1026
+ selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
1027
+
1028
+ expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
1029
+ end
1030
+ end
930
1031
  end
931
1032
 
932
1033
  describe ".scoped" do
data/spec/spec_helper.rb CHANGED
@@ -29,6 +29,14 @@ def database_id_alt
29
29
  "mongoid_test_alt"
30
30
  end
31
31
 
32
+ def test_model(name: 'TestModel', &block)
33
+ Class.new.tap do |klass|
34
+ klass.define_singleton_method(:name) { name.to_s }
35
+ klass.include Mongoid::Document
36
+ klass.class_eval(&block) if block_given?
37
+ end
38
+ end
39
+
32
40
  begin
33
41
  require 'mrss/cluster_config'
34
42
  require 'support/client_registry'
@@ -148,6 +148,48 @@ module Crypt
148
148
  embedded_in :account, class_name: 'Crypt::Account'
149
149
  end
150
150
 
151
+ # Embedded associations persisted under a store_as key that differs from
152
+ # the association name. The schema has to be keyed by the stored name.
153
+ class Dossier
154
+ include Mongoid::Document
155
+
156
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
157
+
158
+ embeds_one :secret, class_name: 'Crypt::DossierSecret', store_as: :s
159
+ end
160
+
161
+ class DossierSecret
162
+ include Mongoid::Document
163
+
164
+ field :token, type: String, encrypt: { deterministic: true }
165
+
166
+ embedded_in :dossier, class_name: 'Crypt::Dossier'
167
+ end
168
+
169
+ # The same, one level down: both associations use store_as.
170
+ class Archive
171
+ include Mongoid::Document
172
+
173
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
174
+
175
+ embeds_one :entry, class_name: 'Crypt::ArchiveEntry', store_as: :e
176
+ end
177
+
178
+ class ArchiveEntry
179
+ include Mongoid::Document
180
+
181
+ embedded_in :archive, class_name: 'Crypt::Archive'
182
+ embeds_one :payload, class_name: 'Crypt::ArchivePayload', store_as: :p
183
+ end
184
+
185
+ class ArchivePayload
186
+ include Mongoid::Document
187
+
188
+ field :token, type: String, encrypt: { deterministic: true }
189
+
190
+ embedded_in :entry, class_name: 'Crypt::ArchiveEntry'
191
+ end
192
+
151
193
  # A model that embeds itself. The walk over embedded relations has to stop
152
194
  # here, or generating the map never terminates.
153
195
  class Comment
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllAccount
4
+ include Mongoid::Document
5
+
6
+ has_many :items, class_name: 'DeleteAllItem', foreign_key: :account_id, inverse_of: :account,
7
+ scope: -> { where(:state.in => %w[draft open]) }
8
+ has_many :scoped_items, class_name: 'DeleteAllScopedItem', foreign_key: :account_id, inverse_of: :account
9
+ has_and_belongs_to_many :tags, class_name: 'DeleteAllTag', inverse_of: nil
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllItem
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+
8
+ belongs_to :account, class_name: 'DeleteAllAccount', inverse_of: :items
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllScopedItem
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+
8
+ belongs_to :account, class_name: 'DeleteAllAccount', inverse_of: :scoped_items
9
+
10
+ default_scope -> { where(:state.in => %w[draft open]) }
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllTag
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+ end