mongoid 8.0.12 → 8.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.
- checksums.yaml +4 -4
- data/lib/config/locales/en.yml +26 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/nested/many.rb +38 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +103 -2
- data/lib/mongoid/attributes/readonly.rb +19 -1
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/config.rb +62 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
- data/lib/mongoid/contextual/memory.rb +19 -8
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
- data/lib/mongoid/matcher/regex.rb +8 -9
- data/lib/mongoid/matcher/regexp_budget.rb +389 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +3 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +1 -0
- data/spec/integration/app_spec.rb +8 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
- data/spec/integration/dots_and_dollars_spec.rb +12 -2
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
- data/spec/mongoid/attributes/nested_spec.rb +204 -10
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
- data/spec/mongoid/attributes/readonly_spec.rb +387 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/config_spec.rb +7 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
- data/spec/mongoid/contextual/memory_spec.rb +202 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
- data/spec/mongoid/criteria_spec.rb +19 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- data/spec/spec_helper.rb +8 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- metadata +1862 -1826
|
@@ -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
|
|
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
|
-
|
|
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
|
|
|
@@ -884,6 +980,24 @@ describe Mongoid::Scopable do
|
|
|
884
980
|
expect(Circle.visible.selector).to eq("radius" => 5)
|
|
885
981
|
end
|
|
886
982
|
end
|
|
983
|
+
|
|
984
|
+
context 'when the scope name matches an internal criteria helper' do
|
|
985
|
+
before do
|
|
986
|
+
Band.scope(:none_state, -> { where(active: true) })
|
|
987
|
+
Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
|
|
988
|
+
end
|
|
989
|
+
|
|
990
|
+
after do
|
|
991
|
+
remove_scope(Band, :none_state)
|
|
992
|
+
remove_scope(Band, :clear_default_scope_none)
|
|
993
|
+
end
|
|
994
|
+
|
|
995
|
+
it 'applies both scopes when chained on a criteria' do
|
|
996
|
+
selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
|
|
997
|
+
|
|
998
|
+
expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
|
|
999
|
+
end
|
|
1000
|
+
end
|
|
887
1001
|
end
|
|
888
1002
|
|
|
889
1003
|
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,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
|