mongoid 8.1.12 → 8.1.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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +26 -0
  3. data/lib/mongoid/association/depending.rb +8 -4
  4. data/lib/mongoid/association/nested/many.rb +38 -3
  5. data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +103 -2
  7. data/lib/mongoid/attributes/readonly.rb +19 -1
  8. data/lib/mongoid/changeable.rb +22 -4
  9. data/lib/mongoid/config.rb +62 -0
  10. data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
  11. data/lib/mongoid/contextual/memory.rb +19 -8
  12. data/lib/mongoid/contextual/none.rb +51 -0
  13. data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
  14. data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
  15. data/lib/mongoid/criteria/scopable.rb +7 -1
  16. data/lib/mongoid/criteria.rb +45 -1
  17. data/lib/mongoid/document.rb +10 -0
  18. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  19. data/lib/mongoid/errors.rb +1 -0
  20. data/lib/mongoid/field_readable.rb +70 -0
  21. data/lib/mongoid/matchable.rb +6 -1
  22. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
  23. data/lib/mongoid/matcher/regex.rb +8 -9
  24. data/lib/mongoid/matcher/regexp_budget.rb +389 -0
  25. data/lib/mongoid/matcher.rb +1 -0
  26. data/lib/mongoid/reloadable.rb +1 -0
  27. data/lib/mongoid/threaded.rb +3 -0
  28. data/lib/mongoid/version.rb +1 -1
  29. data/lib/mongoid/warnings.rb +1 -0
  30. data/spec/integration/app_spec.rb +8 -0
  31. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
  32. data/spec/integration/dots_and_dollars_spec.rb +12 -2
  33. data/spec/integration/matcher_operator_data/regex.yml +21 -0
  34. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  35. data/spec/integration/query_operator_guard_spec.rb +89 -0
  36. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
  37. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
  38. data/spec/mongoid/attributes/nested_spec.rb +204 -10
  39. data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
  40. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  41. data/spec/mongoid/changeable_spec.rb +18 -0
  42. data/spec/mongoid/config_spec.rb +7 -0
  43. data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
  44. data/spec/mongoid/contextual/memory_spec.rb +202 -0
  45. data/spec/mongoid/contextual/none_spec.rb +22 -5
  46. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
  47. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
  48. data/spec/mongoid/criteria_spec.rb +19 -0
  49. data/spec/mongoid/findable_spec.rb +14 -0
  50. data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
  51. data/spec/mongoid/scopable_spec.rb +116 -2
  52. data/spec/spec_helper.rb +8 -0
  53. data/spec/support/models/delete_all_account.rb +10 -0
  54. data/spec/support/models/delete_all_item.rb +9 -0
  55. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  56. data/spec/support/models/delete_all_tag.rb +7 -0
  57. metadata +1900 -1860
@@ -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
200
+
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
115
210
 
116
- let (:selector) do
211
+ context 'when parent class has default scope' do
212
+ let(:selector) do
117
213
  AudibleSound.all.selector
118
214
  end
119
215
 
@@ -954,6 +1050,24 @@ describe Mongoid::Scopable do
954
1050
  expect(Circle.visible.selector).to eq("radius" => 5)
955
1051
  end
956
1052
  end
1053
+
1054
+ context 'when the scope name matches an internal criteria helper' do
1055
+ before do
1056
+ Band.scope(:none_state, -> { where(active: true) })
1057
+ Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
1058
+ end
1059
+
1060
+ after do
1061
+ remove_scope(Band, :none_state)
1062
+ remove_scope(Band, :clear_default_scope_none)
1063
+ end
1064
+
1065
+ it 'applies both scopes when chained on a criteria' do
1066
+ selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
1067
+
1068
+ expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
1069
+ end
1070
+ end
957
1071
  end
958
1072
 
959
1073
  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