mongoid 9.1.2 → 9.1.3

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 (53) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +13 -0
  3. data/lib/mongoid/association/accessors.rb +4 -1
  4. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +19 -15
  5. data/lib/mongoid/association/referenced/belongs_to.rb +11 -3
  6. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +21 -7
  7. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  8. data/lib/mongoid/association/referenced/has_many/buildable.rb +17 -7
  9. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  10. data/lib/mongoid/association/referenced/has_one/buildable.rb +20 -6
  11. data/lib/mongoid/association.rb +15 -0
  12. data/lib/mongoid/attributes/readonly.rb +19 -1
  13. data/lib/mongoid/changeable.rb +19 -1
  14. data/lib/mongoid/config/encryption.rb +5 -3
  15. data/lib/mongoid/contextual/none.rb +51 -0
  16. data/lib/mongoid/criteria/scopable.rb +5 -0
  17. data/lib/mongoid/criteria.rb +45 -1
  18. data/lib/mongoid/document.rb +10 -0
  19. data/lib/mongoid/encryptable.rb +1 -1
  20. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  21. data/lib/mongoid/errors.rb +1 -0
  22. data/lib/mongoid/factory.rb +1 -1
  23. data/lib/mongoid/fields/encrypted.rb +14 -3
  24. data/lib/mongoid/fields/foreign_key.rb +9 -0
  25. data/lib/mongoid/fields/standard.rb +10 -0
  26. data/lib/mongoid/fields/validators/macro.rb +29 -0
  27. data/lib/mongoid/fields.rb +42 -2
  28. data/lib/mongoid/reloadable.rb +1 -0
  29. data/lib/mongoid/version.rb +1 -1
  30. data/spec/integration/encryption_spec.rb +269 -0
  31. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +35 -6
  32. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +142 -0
  33. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +25 -5
  34. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  35. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +24 -6
  36. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +81 -0
  37. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +18 -3
  38. data/spec/mongoid/attributes/readonly_dotted_spec.rb +334 -0
  39. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  40. data/spec/mongoid/changeable_spec.rb +18 -0
  41. data/spec/mongoid/config/encryption_spec.rb +227 -0
  42. data/spec/mongoid/contextual/none_spec.rb +18 -0
  43. data/spec/mongoid/criteria_spec.rb +17 -0
  44. data/spec/mongoid/factory_spec.rb +8 -0
  45. data/spec/mongoid/fields_spec.rb +29 -0
  46. data/spec/mongoid/findable_spec.rb +14 -0
  47. data/spec/mongoid/scopable_spec.rb +102 -0
  48. data/spec/support/crypt/models.rb +42 -0
  49. data/spec/support/models/delete_all_account.rb +10 -0
  50. data/spec/support/models/delete_all_item.rb +9 -0
  51. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  52. data/spec/support/models/delete_all_tag.rb +7 -0
  53. metadata +2045 -2011
@@ -1385,6 +1385,23 @@ describe Mongoid::Criteria do
1385
1385
  expect(merged).to equal(criteria)
1386
1386
  end
1387
1387
  end
1388
+
1389
+ context 'when merging a none criteria into a criteria with a cached context' do
1390
+ let(:criteria) { Band.where(name: 'Depeche Mode') }
1391
+ let(:other) { Band.none }
1392
+
1393
+ before do
1394
+ Band.create!(name: 'Depeche Mode')
1395
+ end
1396
+
1397
+ it 'does not use the cached context' do
1398
+ expect(criteria.count).to eq(1)
1399
+
1400
+ merged
1401
+
1402
+ expect(criteria.count).to eq(0)
1403
+ end
1404
+ end
1388
1405
  end
1389
1406
 
1390
1407
  describe '#min' do
@@ -374,6 +374,14 @@ describe Mongoid::Factory do
374
374
  end
375
375
  end
376
376
 
377
+ context 'when type resolves to a class that is not a Mongoid model' do
378
+ it 'raises an unknown model error' do
379
+ expect do
380
+ described_class.from_db(Person, { '_type' => 'String' })
381
+ end.to raise_error(Mongoid::Errors::UnknownModel)
382
+ end
383
+ end
384
+
377
385
  context 'when type does not correspond to a Class name with custom discriminator key' do
378
386
  before do
379
387
  Person.discriminator_key = 'dkey'
@@ -1208,6 +1208,35 @@ describe Mongoid::Fields do
1208
1208
  end
1209
1209
  end
1210
1210
 
1211
+ context 'when a subclass declares an encrypted field and the parent declares it as a plain field' do
1212
+ let(:parent) do
1213
+ klass = stub_const('PlainFieldParent', Class.new)
1214
+ klass.include(Mongoid::Document)
1215
+ klass
1216
+ end
1217
+
1218
+ let(:child) do
1219
+ parent
1220
+ klass = stub_const('PlainFieldChild', Class.new(parent))
1221
+ klass.field(:secret, type: String, encrypt: { deterministic: true })
1222
+ klass
1223
+ end
1224
+
1225
+ after do
1226
+ Mongoid.deregister_model(child)
1227
+ Mongoid.deregister_model(parent)
1228
+ end
1229
+
1230
+ it 'replaces the encrypted field on the subclass' do
1231
+ child
1232
+
1233
+ parent.field(:secret, type: String)
1234
+
1235
+ expect(parent.fields['secret']).not_to be_encrypted
1236
+ expect(child.fields['secret']).not_to be_encrypted
1237
+ end
1238
+ end
1239
+
1211
1240
  context 'custom options' do
1212
1241
  let(:handler) do
1213
1242
  proc {}
@@ -855,6 +855,20 @@ describe Mongoid::Findable do
855
855
  expect(criteria.size).to eq(0)
856
856
  end
857
857
  end
858
+
859
+ context 'when none is called on a criteria with a cached context' do
860
+ let(:criteria) do
861
+ Band.where(name: 'Depeche Mode')
862
+ end
863
+
864
+ it 'does not use the cached context' do
865
+ expect(criteria.count).to eq(1)
866
+
867
+ criteria.none
868
+
869
+ expect(criteria.count).to eq(0)
870
+ end
871
+ end
858
872
  end
859
873
 
860
874
  describe '.pluck' do
@@ -149,6 +149,90 @@ describe Mongoid::Scopable do
149
149
  end
150
150
  end
151
151
 
152
+ context 'when the default scope is none' do
153
+ before do
154
+ Band.create!(name: 'Depeche Mode', active: true)
155
+ Band.default_scope -> { Band.none }
156
+ end
157
+
158
+ after do
159
+ Band.default_scoping = nil
160
+ end
161
+
162
+ it 'does not return any documents' do
163
+ expect(Band.count).to eq(0)
164
+ expect(Band.all.to_a).to be_empty
165
+ expect(Band.where(name: 'Depeche Mode').first).to be_nil
166
+ end
167
+
168
+ it 'does not update documents' do
169
+ Band.update_all(active: false)
170
+ expect(Band.unscoped.first.active).to be(true)
171
+ end
172
+
173
+ it 'does not delete documents' do
174
+ expect(Band.delete_all).to eq(0)
175
+ expect(Band.unscoped.count).to eq(1)
176
+ end
177
+
178
+ it 'removes the none from the default scope when unscoping' do
179
+ crit = Band.all
180
+ expect(crit.count).to eq(0)
181
+ expect(crit.unscoped.count).to eq(1)
182
+ expect(Band.all.merge(Band.all).unscoped.count).to eq(1)
183
+ end
184
+
185
+ it 'keeps an explicit none when unscoping' do
186
+ expect(Band.none.unscoped.count).to eq(0)
187
+ expect(Band.all.none.unscoped.count).to eq(0)
188
+ expect(Band.all.merge(Band.none).unscoped.count).to eq(0)
189
+ end
190
+ end
191
+
192
+ context 'when the default scope is conditionally none' do
193
+ before do
194
+ Band.create!(name: 'Depeche Mode', active: true)
195
+ Band.default_scope -> { allow_all ? Band.where(active: true) : Band.none }
196
+ end
197
+
198
+ after do
199
+ Band.default_scoping = nil
200
+ end
201
+
202
+ context 'when the condition restricts the scope' do
203
+ let(:allow_all) { false }
204
+
205
+ it 'does not return any documents' do
206
+ expect(Band.count).to eq(0)
207
+ end
208
+ end
209
+
210
+ context 'when the condition allows all documents' do
211
+ let(:allow_all) { true }
212
+
213
+ it 'returns the documents' do
214
+ expect(Band.count).to eq(1)
215
+ end
216
+ end
217
+ end
218
+
219
+ context 'when multiple default scopes are combined with none last' do
220
+ before do
221
+ Band.create!(name: 'Depeche Mode', active: true)
222
+ Band.default_scope -> { Band.where(active: true) }
223
+ Band.default_scope -> { Band.none }
224
+ end
225
+
226
+ after do
227
+ Band.default_scoping = nil
228
+ end
229
+
230
+ it 'does not return any documents' do
231
+ expect(Band.count).to eq(0)
232
+ expect(Band.all.to_a).to be_empty
233
+ end
234
+ end
235
+
152
236
  context 'when parent class has default scope' do
153
237
  let(:selector) do
154
238
  AudibleSound.all.selector
@@ -872,6 +956,24 @@ describe Mongoid::Scopable do
872
956
  expect(Circle.visible.selector).to eq('radius' => 5)
873
957
  end
874
958
  end
959
+
960
+ context 'when the scope name matches an internal criteria helper' do
961
+ before do
962
+ Band.scope(:none_state, -> { where(active: true) })
963
+ Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
964
+ end
965
+
966
+ after do
967
+ remove_scope(Band, :none_state)
968
+ remove_scope(Band, :clear_default_scope_none)
969
+ end
970
+
971
+ it 'applies both scopes when chained on a criteria' do
972
+ selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
973
+
974
+ expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
975
+ end
976
+ end
875
977
  end
876
978
 
877
979
  describe '.scoped' do
@@ -147,6 +147,48 @@ module Crypt
147
147
  embedded_in :account, class_name: 'Crypt::Account'
148
148
  end
149
149
 
150
+ # Embedded associations persisted under a store_as key that differs from
151
+ # the association name. The schema has to be keyed by the stored name.
152
+ class Dossier
153
+ include Mongoid::Document
154
+
155
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
156
+
157
+ embeds_one :secret, class_name: 'Crypt::DossierSecret', store_as: :s
158
+ end
159
+
160
+ class DossierSecret
161
+ include Mongoid::Document
162
+
163
+ field :token, type: String, encrypt: { deterministic: true }
164
+
165
+ embedded_in :dossier, class_name: 'Crypt::Dossier'
166
+ end
167
+
168
+ # The same, one level down: both associations use store_as.
169
+ class Archive
170
+ include Mongoid::Document
171
+
172
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
173
+
174
+ embeds_one :entry, class_name: 'Crypt::ArchiveEntry', store_as: :e
175
+ end
176
+
177
+ class ArchiveEntry
178
+ include Mongoid::Document
179
+
180
+ embedded_in :archive, class_name: 'Crypt::Archive'
181
+ embeds_one :payload, class_name: 'Crypt::ArchivePayload', store_as: :p
182
+ end
183
+
184
+ class ArchivePayload
185
+ include Mongoid::Document
186
+
187
+ field :token, type: String, encrypt: { deterministic: true }
188
+
189
+ embedded_in :entry, class_name: 'Crypt::ArchiveEntry'
190
+ end
191
+
150
192
  # A model that embeds itself. The walk over embedded relations has to stop
151
193
  # here, or generating the map never terminates.
152
194
  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