mongoid 7.6.2 → 7.6.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.
@@ -1235,6 +1235,24 @@ describe Mongoid::Changeable do
1235
1235
  expect(person.title).to be_nil
1236
1236
  end
1237
1237
  end
1238
+
1239
+ context 'when a resizable attribute was read but its value has not changed' do
1240
+ let(:person) do
1241
+ Person.create!(map: { 'enabled' => false })
1242
+ end
1243
+
1244
+ it 'keeps the current value object so held references stay attached' do
1245
+ map = person.map
1246
+ person.reset_map!
1247
+
1248
+ expect(person.attributes['map']).to equal(map)
1249
+
1250
+ map['enabled'] = true
1251
+ person.save!
1252
+
1253
+ expect(person.reload.map).to eq('enabled' => true)
1254
+ end
1255
+ end
1238
1256
  end
1239
1257
 
1240
1258
  describe "#reset_attribute_to_default!" do
@@ -53,12 +53,30 @@ describe Mongoid::Contextual::None do
53
53
  end
54
54
  end
55
55
 
56
+ describe '#destroy' do
57
+ it 'returns zero and destroys nothing' do
58
+ expect(context.destroy).to eq(0)
59
+ expect(context.destroy_all).to eq(0)
60
+ expect(Band.where(name: 'Depeche Mode').count).to eq(1)
61
+ end
62
+ end
63
+
56
64
  describe "#pluck" do
57
65
  it "returns an empty array" do
58
66
  expect(context.pluck(:id)).to eq([])
59
67
  end
60
68
  end
61
69
 
70
+ describe '#update' do
71
+ it 'returns false without attributes and nil with them, updating nothing' do
72
+ expect(context.update).to be(false)
73
+ expect(context.update(name: 'The Cure')).to be_nil
74
+ expect(context.update_all).to be(false)
75
+ expect(context.update_all(name: 'The Cure')).to be_nil
76
+ expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
77
+ end
78
+ end
79
+
62
80
  describe "#first" do
63
81
  it "returns nil" do
64
82
  expect(context.first).to be_nil
@@ -1344,6 +1344,33 @@ describe Mongoid::Criteria::Queryable::Selectable do
1344
1344
  )
1345
1345
  end
1346
1346
  end
1347
+
1348
+ context 'when any_of has multiple arguments' do
1349
+
1350
+ let(:selection) do
1351
+ query.or(field: [ 1, 2 ]).where(foo: 'bar').any_of({a: 1}, {b: 2})
1352
+ end
1353
+
1354
+ it 'adds the new conditions to the existing top-level $or' do
1355
+ expect(selection.selector).to eq(
1356
+ '$or' => [{'field' => [1, 2]}, {'a' => 1}, {'b' => 2}],
1357
+ 'foo' => 'bar',
1358
+ )
1359
+ end
1360
+
1361
+ context 'when query already has a top-level $and' do
1362
+ let(:selection) do
1363
+ query.or(field: [ 1, 2 ]).where('$and' => [foo: 'bar']).any_of({a: 1}, {b: 2})
1364
+ end
1365
+
1366
+ it 'adds the new conditions to the existing top-level $or' do
1367
+ expect(selection.selector).to eq(
1368
+ '$or' => [{'field' => [1, 2]}, {'a' => 1}, {'b' => 2}],
1369
+ '$and' => [{'foo' => 'bar'}],
1370
+ )
1371
+ end
1372
+ end
1373
+ end
1347
1374
  end
1348
1375
 
1349
1376
  context "when provided multiple criteria" do
@@ -2622,6 +2622,23 @@ describe Mongoid::Criteria do
2622
2622
  it "returns the same criteria" do
2623
2623
  expect(merged).to equal(criteria)
2624
2624
  end
2625
+
2626
+ context 'when merging a none criteria into a criteria with a cached context' do
2627
+ let(:criteria) { Band.where(name: 'Depeche Mode') }
2628
+ let(:mergeable) { Band.none }
2629
+
2630
+ before do
2631
+ Band.create!(name: 'Depeche Mode')
2632
+ end
2633
+
2634
+ it 'does not use the cached context' do
2635
+ expect(criteria.count).to eq(1)
2636
+
2637
+ merged
2638
+
2639
+ expect(criteria.count).to eq(0)
2640
+ end
2641
+ end
2625
2642
  end
2626
2643
 
2627
2644
  describe "#min" do
@@ -469,6 +469,20 @@ describe Mongoid::Findable do
469
469
  expect(criteria.size).to eq(0)
470
470
  end
471
471
  end
472
+
473
+ context 'when none is called on a criteria with a cached context' do
474
+ let(:criteria) do
475
+ Band.where(name: 'Depeche Mode')
476
+ end
477
+
478
+ it 'does not use the cached context' do
479
+ expect(criteria.count).to eq(1)
480
+
481
+ criteria.none
482
+
483
+ expect(criteria.count).to eq(0)
484
+ end
485
+ end
472
486
  end
473
487
 
474
488
  describe ".pluck" do
@@ -32,6 +32,12 @@ describe Mongoid::Matcher::RegexpBudget do
32
32
  # exercising one has to hand .open something to bound.
33
33
  let(:regexp_selector) { { 'name' => /\Aabc\z/ } }
34
34
 
35
+ describe 'REGEXP_BUDGET_KEY' do
36
+ it 'is namespaced so a foreign thread-local cannot collide with it' do
37
+ expect(Mongoid::Threaded::REGEXP_BUDGET_KEY).to start_with('[mongoid]:')
38
+ end
39
+ end
40
+
35
41
  describe '.open' do
36
42
  context 'when no limit is configured' do
37
43
  config_override :in_memory_regexp_time_limit, nil
@@ -2,6 +2,19 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
+ # Retrieve the singleton class for the given class.
6
+ def singleton_class_for(klass)
7
+ class <<klass; self; end
8
+ end
9
+
10
+ # Helper method for removing a declared scope
11
+ def remove_scope(klass, scope)
12
+ if klass._declared_scopes[scope]
13
+ singleton_class_for(klass).remove_method(scope)
14
+ klass._declared_scopes.delete(scope)
15
+ end
16
+ end
17
+
5
18
  describe Mongoid::Scopable do
6
19
 
7
20
  describe ".default_scope" do
@@ -111,9 +124,92 @@ describe Mongoid::Scopable do
111
124
  end
112
125
  end
113
126
 
114
- context "when parent class has default scope" do
127
+ context 'when the default scope is none' do
128
+ before do
129
+ Band.create!(name: 'Depeche Mode', active: true)
130
+ Band.default_scope -> { Band.none }
131
+ end
132
+
133
+ after do
134
+ Band.default_scoping = nil
135
+ end
136
+
137
+ it 'does not return any documents' do
138
+ expect(Band.count).to eq(0)
139
+ expect(Band.all.to_a).to be_empty
140
+ expect(Band.where(name: 'Depeche Mode').first).to be_nil
141
+ end
142
+
143
+ it 'does not update documents' do
144
+ Band.update_all(active: false)
145
+ expect(Band.unscoped.first.active).to be(true)
146
+ end
147
+
148
+ it 'does not delete documents' do
149
+ expect(Band.delete_all).to eq(0)
150
+ expect(Band.unscoped.count).to eq(1)
151
+ end
152
+
153
+ it 'removes the none from the default scope when unscoping' do
154
+ crit = Band.all
155
+ expect(crit.count).to eq(0)
156
+ expect(crit.unscoped.count).to eq(1)
157
+ expect(Band.all.merge(Band.all).unscoped.count).to eq(1)
158
+ end
159
+
160
+ it 'keeps an explicit none when unscoping' do
161
+ expect(Band.none.unscoped.count).to eq(0)
162
+ expect(Band.all.none.unscoped.count).to eq(0)
163
+ expect(Band.all.merge(Band.none).unscoped.count).to eq(0)
164
+ end
165
+ end
166
+
167
+ context 'when the default scope is conditionally none' do
168
+ before do
169
+ Band.create!(name: 'Depeche Mode', active: true)
170
+ Band.default_scope -> { allow_all ? Band.where(active: true) : Band.none }
171
+ end
172
+
173
+ after do
174
+ Band.default_scoping = nil
175
+ end
176
+
177
+ context 'when the condition restricts the scope' do
178
+ let(:allow_all) { false }
179
+
180
+ it 'does not return any documents' do
181
+ expect(Band.count).to eq(0)
182
+ end
183
+ end
184
+
185
+ context 'when the condition allows all documents' do
186
+ let(:allow_all) { true }
187
+
188
+ it 'returns the documents' do
189
+ expect(Band.count).to eq(1)
190
+ end
191
+ end
192
+ end
193
+
194
+ context 'when multiple default scopes are combined with none last' do
195
+ before do
196
+ Band.create!(name: 'Depeche Mode', active: true)
197
+ Band.default_scope -> { Band.where(active: true) }
198
+ Band.default_scope -> { Band.none }
199
+ end
115
200
 
116
- let (:selector) do
201
+ after do
202
+ Band.default_scoping = nil
203
+ end
204
+
205
+ it 'does not return any documents' do
206
+ expect(Band.count).to eq(0)
207
+ expect(Band.all.to_a).to be_empty
208
+ end
209
+ end
210
+
211
+ context 'when parent class has default scope' do
212
+ let(:selector) do
117
213
  AudibleSound.all.selector
118
214
  end
119
215
 
@@ -882,6 +978,24 @@ describe Mongoid::Scopable do
882
978
  expect(Circle.visible.selector).to eq("radius" => 5)
883
979
  end
884
980
  end
981
+
982
+ context 'when the scope name matches an internal criteria helper' do
983
+ before do
984
+ Band.scope(:none_state, -> { where(active: true) })
985
+ Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
986
+ end
987
+
988
+ after do
989
+ remove_scope(Band, :none_state)
990
+ remove_scope(Band, :clear_default_scope_none)
991
+ end
992
+
993
+ it 'applies both scopes when chained on a criteria' do
994
+ selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
995
+
996
+ expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
997
+ end
998
+ end
885
999
  end
886
1000
 
887
1001
  describe ".scoped" do
data/spec/spec_helper.rb CHANGED
@@ -28,6 +28,14 @@ def database_id_alt
28
28
  "mongoid_test_alt"
29
29
  end
30
30
 
31
+ def test_model(name: 'TestModel', &block)
32
+ Class.new.tap do |klass|
33
+ klass.define_singleton_method(:name) { name.to_s }
34
+ klass.include Mongoid::Document
35
+ klass.class_eval(&block) if block_given?
36
+ end
37
+ end
38
+
31
39
  begin
32
40
  require 'mrss/cluster_config'
33
41
  require 'support/client_registry'
@@ -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