pg_search 2.3.0 → 2.3.5

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 (40) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -0
  3. data/.github/dependabot.yml +11 -0
  4. data/.jrubyrc +1 -0
  5. data/.rubocop.yml +85 -7
  6. data/.travis.yml +14 -22
  7. data/CHANGELOG.md +41 -16
  8. data/CODE_OF_CONDUCT.md +76 -0
  9. data/Gemfile +1 -1
  10. data/LICENSE +1 -1
  11. data/README.md +60 -18
  12. data/lib/pg_search.rb +4 -6
  13. data/lib/pg_search/document.rb +1 -1
  14. data/lib/pg_search/features/dmetaphone.rb +4 -6
  15. data/lib/pg_search/features/tsearch.rb +13 -12
  16. data/lib/pg_search/migration/templates/add_pg_search_dmetaphone_support_functions.rb.erb +6 -6
  17. data/lib/pg_search/migration/templates/create_pg_search_documents.rb.erb +2 -2
  18. data/lib/pg_search/multisearch.rb +10 -1
  19. data/lib/pg_search/multisearch/rebuilder.rb +7 -3
  20. data/lib/pg_search/scope_options.rb +3 -3
  21. data/lib/pg_search/tasks.rb +2 -1
  22. data/lib/pg_search/version.rb +1 -1
  23. data/pg_search.gemspec +11 -7
  24. data/spec/.rubocop.yml +2 -2
  25. data/spec/integration/.rubocop.yml +11 -0
  26. data/spec/integration/associations_spec.rb +17 -56
  27. data/spec/integration/deprecation_spec.rb +1 -1
  28. data/spec/integration/pg_search_spec.rb +62 -51
  29. data/spec/lib/pg_search/configuration/association_spec.rb +8 -6
  30. data/spec/lib/pg_search/features/dmetaphone_spec.rb +2 -2
  31. data/spec/lib/pg_search/features/trigram_spec.rb +15 -11
  32. data/spec/lib/pg_search/features/tsearch_spec.rb +16 -10
  33. data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +116 -71
  34. data/spec/lib/pg_search/multisearch_spec.rb +48 -29
  35. data/spec/lib/pg_search/multisearchable_spec.rb +150 -97
  36. data/spec/lib/pg_search/normalizer_spec.rb +12 -10
  37. data/spec/lib/pg_search_spec.rb +66 -55
  38. data/spec/spec_helper.rb +13 -4
  39. data/spec/support/database.rb +1 -1
  40. metadata +78 -17
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "spec_helper"
4
+ require "active_support/deprecation"
4
5
 
6
+ # rubocop:disable RSpec/NestedGroups
5
7
  describe PgSearch::Multisearch do
6
- with_table "pg_search_documents", {}, &DOCUMENTS_SCHEMA
8
+ with_table "pg_search_documents", &DOCUMENTS_SCHEMA
7
9
 
8
10
  with_model :MultisearchableModel do
9
11
  table do |t|
@@ -18,22 +20,22 @@ describe PgSearch::Multisearch do
18
20
 
19
21
  let(:model) { MultisearchableModel }
20
22
  let(:connection) { model.connection }
21
- let(:documents) { double(:documents) }
22
23
 
23
24
  describe ".rebuild" do
24
25
  before do
25
26
  model.multisearchable against: :title
26
27
  end
27
28
 
28
- it "should operate inside a transaction" do
29
- expect(model).to receive(:transaction).once
29
+ it "operates inside a transaction" do
30
+ allow(model).to receive(:transaction)
30
31
 
31
- PgSearch::Multisearch.rebuild(model)
32
+ described_class.rebuild(model)
33
+ expect(model).to have_received(:transaction).once
32
34
  end
33
35
 
34
36
  describe "cleaning up search documents for this model" do
35
37
  before do
36
- connection.execute <<-SQL.strip_heredoc
38
+ connection.execute <<~SQL.squish
37
39
  INSERT INTO pg_search_documents
38
40
  (searchable_type, searchable_id, content, created_at, updated_at)
39
41
  VALUES
@@ -47,28 +49,39 @@ describe PgSearch::Multisearch do
47
49
  end
48
50
 
49
51
  context "when clean_up is not passed" do
50
- it "should delete the document for the model" do
51
- PgSearch::Multisearch.rebuild(model)
52
+ it "deletes the document for the model" do
53
+ described_class.rebuild(model)
52
54
  expect(PgSearch::Document.count).to eq(1)
53
55
  expect(PgSearch::Document.first.searchable_type).to eq("Bar")
54
56
  end
55
57
  end
56
58
 
57
59
  context "when clean_up is true" do
58
- let(:clean_up) { true }
59
-
60
- it "should delete the document for the model" do
61
- PgSearch::Multisearch.rebuild(model, clean_up)
60
+ it "deletes the document for the model" do
61
+ described_class.rebuild(model, clean_up: true)
62
62
  expect(PgSearch::Document.count).to eq(1)
63
63
  expect(PgSearch::Document.first.searchable_type).to eq("Bar")
64
64
  end
65
65
  end
66
66
 
67
67
  context "when clean_up is false" do
68
- let(:clean_up) { false }
68
+ it "does not delete the document for the model" do
69
+ described_class.rebuild(model, clean_up: false)
70
+ expect(PgSearch::Document.count).to eq(2)
71
+ end
72
+ end
73
+
74
+ context "when deprecated_clean_up is true" do
75
+ it "deletes the document for the model" do
76
+ ActiveSupport::Deprecation.silence { described_class.rebuild(model, true) }
77
+ expect(PgSearch::Document.count).to eq(1)
78
+ expect(PgSearch::Document.first.searchable_type).to eq("Bar")
79
+ end
80
+ end
69
81
 
70
- it "should not delete the document for the model" do
71
- PgSearch::Multisearch.rebuild(model, clean_up)
82
+ context "when deprecated_clean_up is false" do
83
+ it "does not delete the document for the model" do
84
+ ActiveSupport::Deprecation.silence { described_class.rebuild(model, false) }
72
85
  expect(PgSearch::Document.count).to eq(2)
73
86
  end
74
87
  end
@@ -76,7 +89,7 @@ describe PgSearch::Multisearch do
76
89
  context "when the model implements .rebuild_pg_search_documents" do
77
90
  before do
78
91
  def model.rebuild_pg_search_documents
79
- connection.execute <<-SQL.strip_heredoc
92
+ connection.execute <<~SQL.squish
80
93
  INSERT INTO pg_search_documents
81
94
  (searchable_type, searchable_id, content, created_at, updated_at)
82
95
  VALUES
@@ -85,31 +98,36 @@ describe PgSearch::Multisearch do
85
98
  end
86
99
  end
87
100
 
88
- it "should call .rebuild_pg_search_documents and skip the default behavior" do
89
- expect(PgSearch::Multisearch).not_to receive(:rebuild_sql)
90
- PgSearch::Multisearch.rebuild(model)
101
+ it "calls .rebuild_pg_search_documents and skips the default behavior" do
102
+ without_partial_double_verification do
103
+ allow(model).to receive(:rebuild_sql)
104
+ described_class.rebuild(model)
91
105
 
92
- record = PgSearch::Document.find_by_searchable_type_and_searchable_id("Baz", 789)
93
- expect(record.content).to eq("baz")
106
+ record = PgSearch::Document.find_by(searchable_type: "Baz", searchable_id: 789)
107
+ expect(model).not_to have_received(:rebuild_sql)
108
+ expect(record.content).to eq("baz")
109
+ end
94
110
  end
95
111
  end
96
112
  end
97
113
 
98
114
  describe "inserting the new documents" do
99
115
  let!(:new_models) { [] }
116
+
100
117
  before do
101
118
  new_models << model.create!(title: "Foo", content: "Bar")
102
119
  new_models << model.create!(title: "Baz", content: "Bar")
103
120
  end
104
121
 
105
- it "should create new documents for the two models" do
106
- PgSearch::Multisearch.rebuild(model)
122
+ it "creates new documents for the two models" do
123
+ described_class.rebuild(model)
107
124
  expect(PgSearch::Document.last(2).map(&:searchable).map(&:title)).to match_array(new_models.map(&:title))
108
125
  end
109
126
  end
110
127
 
111
128
  describe "the generated SQL" do
112
129
  let(:now) { Time.now }
130
+
113
131
  before { allow(Time).to receive(:now).and_return(now) }
114
132
 
115
133
  context "with one attribute" do
@@ -117,8 +135,8 @@ describe PgSearch::Multisearch do
117
135
  model.multisearchable against: [:title]
118
136
  end
119
137
 
120
- it "should generate the proper SQL code" do
121
- expected_sql = <<-SQL.strip_heredoc
138
+ it "generates the proper SQL code" do
139
+ expected_sql = <<~SQL.squish
122
140
  INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
123
141
  SELECT #{connection.quote(model.name)} AS searchable_type,
124
142
  #{model.quoted_table_name}.id AS searchable_id,
@@ -133,7 +151,7 @@ describe PgSearch::Multisearch do
133
151
  statements = []
134
152
  allow(connection).to receive(:execute) { |sql| statements << sql.strip }
135
153
 
136
- PgSearch::Multisearch.rebuild(model)
154
+ described_class.rebuild(model)
137
155
 
138
156
  expect(statements).to include(expected_sql.strip)
139
157
  end
@@ -144,8 +162,8 @@ describe PgSearch::Multisearch do
144
162
  model.multisearchable against: %i[title content]
145
163
  end
146
164
 
147
- it "should generate the proper SQL code" do
148
- expected_sql = <<-SQL.strip_heredoc
165
+ it "generates the proper SQL code" do
166
+ expected_sql = <<~SQL.squish
149
167
  INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
150
168
  SELECT #{connection.quote(model.name)} AS searchable_type,
151
169
  #{model.quoted_table_name}.id AS searchable_id,
@@ -160,7 +178,7 @@ describe PgSearch::Multisearch do
160
178
  statements = []
161
179
  allow(connection).to receive(:execute) { |sql| statements << sql.strip }
162
180
 
163
- PgSearch::Multisearch.rebuild(model)
181
+ described_class.rebuild(model)
164
182
 
165
183
  expect(statements).to include(expected_sql.strip)
166
184
  end
@@ -168,3 +186,4 @@ describe PgSearch::Multisearch do
168
186
  end
169
187
  end
170
188
  end
189
+ # rubocop:enable RSpec/NestedGroups
@@ -2,8 +2,9 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
+ # rubocop:disable RSpec/NestedGroups
5
6
  describe PgSearch::Multisearchable do
6
- with_table "pg_search_documents", {}, &DOCUMENTS_SCHEMA
7
+ with_table "pg_search_documents", &DOCUMENTS_SCHEMA
7
8
 
8
9
  describe "a model that is multisearchable" do
9
10
  with_model :ModelThatIsMultisearchable do
@@ -35,7 +36,7 @@ describe PgSearch::Multisearchable do
35
36
  belongs_to :multisearchable_parent
36
37
 
37
38
  after_destroy do
38
- multisearchable_parent.update_attribute(:secret, rand(1000).to_s)
39
+ multisearchable_parent.update_attribute(:secret, rand(1000).to_s) # rubocop:disable Rails/SkipsModelValidations
39
40
  end
40
41
  end
41
42
  end
@@ -45,21 +46,21 @@ describe PgSearch::Multisearchable do
45
46
  let(:record) { ModelThatIsMultisearchable.new }
46
47
 
47
48
  describe "saving the record" do
48
- it "should create a PgSearch::Document record" do
49
+ it "creates a PgSearch::Document record" do
49
50
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
50
51
  end
51
52
 
52
53
  context "with multisearch disabled" do
53
54
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
54
55
 
55
- it "should not create a PgSearch::Document record" do
56
+ it "does not create a PgSearch::Document record" do
56
57
  expect { record.save! }.not_to change(PgSearch::Document, :count)
57
58
  end
58
59
  end
59
60
  end
60
61
 
61
62
  describe "the document" do
62
- it "should be associated to the record" do
63
+ it "is associated to the record" do
63
64
  record.save!
64
65
  newest_pg_search_document = PgSearch::Document.last
65
66
  expect(record.pg_search_document).to eq(newest_pg_search_document)
@@ -76,20 +77,22 @@ describe PgSearch::Multisearchable do
76
77
 
77
78
  describe "saving the record" do
78
79
  it "calls save on the pg_search_document" do
79
- expect(record.pg_search_document).to receive(:save)
80
+ allow(record.pg_search_document).to receive(:save)
80
81
  record.save!
82
+ expect(record.pg_search_document).to have_received(:save)
81
83
  end
82
84
 
83
- it "should not create a PgSearch::Document record" do
85
+ it "does not create a PgSearch::Document record" do
84
86
  expect { record.save! }.not_to change(PgSearch::Document, :count)
85
87
  end
86
88
 
87
89
  context "with multisearch disabled" do
88
90
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
89
91
 
90
- it "should not create a PgSearch::Document record" do
91
- expect(record.pg_search_document).not_to receive(:save)
92
+ it "does not create a PgSearch::Document record" do
93
+ allow(record.pg_search_document).to receive(:save)
92
94
  expect { record.save! }.not_to change(PgSearch::Document, :count)
95
+ expect(record.pg_search_document).not_to have_received(:save)
93
96
  end
94
97
  end
95
98
  end
@@ -99,14 +102,14 @@ describe PgSearch::Multisearchable do
99
102
  before { record.pg_search_document = nil }
100
103
 
101
104
  describe "saving the record" do
102
- it "should create a PgSearch::Document record" do
105
+ it "creates a PgSearch::Document record" do
103
106
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
104
107
  end
105
108
 
106
109
  context "with multisearch disabled" do
107
110
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
108
111
 
109
- it "should not create a PgSearch::Document record" do
112
+ it "does not create a PgSearch::Document record" do
110
113
  expect { record.save! }.not_to change(PgSearch::Document, :count)
111
114
  end
112
115
  end
@@ -115,14 +118,14 @@ describe PgSearch::Multisearchable do
115
118
  end
116
119
 
117
120
  describe "after_destroy" do
118
- it "should remove its document" do
121
+ it "removes its document" do
119
122
  record = ModelThatIsMultisearchable.create!
120
123
  document = record.pg_search_document
121
124
  expect { record.destroy }.to change(PgSearch::Document, :count).by(-1)
122
125
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
123
126
  end
124
127
 
125
- it "should remove its document in case of complex associations" do
128
+ it "removes its document in case of complex associations" do
126
129
  parent = MultisearchableParent.create!
127
130
 
128
131
  MultisearchableChild.create!(multisearchable_parent: parent)
@@ -137,9 +140,10 @@ describe PgSearch::Multisearchable do
137
140
  end
138
141
 
139
142
  describe "populating the searchable text" do
140
- let(:record) { ModelThatIsMultisearchable.new }
141
143
  subject { record }
142
144
 
145
+ let(:record) { ModelThatIsMultisearchable.new }
146
+
143
147
  before do
144
148
  ModelThatIsMultisearchable.multisearchable(multisearchable_options)
145
149
  end
@@ -147,36 +151,45 @@ describe PgSearch::Multisearchable do
147
151
  context "when searching against a single column" do
148
152
  let(:multisearchable_options) { { against: :some_content } }
149
153
  let(:text) { "foo bar" }
154
+
150
155
  before do
151
- allow(record).to receive(:some_content) { text }
156
+ without_partial_double_verification do
157
+ allow(record).to receive(:some_content) { text }
158
+ end
152
159
  record.save
153
160
  end
154
161
 
155
162
  describe '#content' do
156
163
  subject { super().pg_search_document.content }
164
+
157
165
  it { is_expected.to eq(text) }
158
166
  end
159
167
  end
160
168
 
161
169
  context "when searching against multiple columns" do
162
- let(:multisearchable_options) { { against: %i[attr1 attr2] } }
170
+ let(:multisearchable_options) { { against: %i[attr_1 attr_2] } }
171
+
163
172
  before do
164
- allow(record).to receive(:attr1) { '1' }
165
- allow(record).to receive(:attr2) { '2' }
173
+ without_partial_double_verification do
174
+ allow(record).to receive(:attr_1).and_return('1')
175
+ allow(record).to receive(:attr_2).and_return('2')
176
+ end
166
177
  record.save
167
178
  end
168
179
 
169
180
  describe '#content' do
170
181
  subject { super().pg_search_document.content }
182
+
171
183
  it { is_expected.to eq("1 2") }
172
184
  end
173
185
  end
174
186
  end
175
187
 
176
188
  describe "populating the searchable attributes" do
177
- let(:record) { ModelThatIsMultisearchable.new }
178
189
  subject { record }
179
190
 
191
+ let(:record) { ModelThatIsMultisearchable.new }
192
+
180
193
  before do
181
194
  ModelThatIsMultisearchable.multisearchable(multisearchable_options)
182
195
  end
@@ -184,27 +197,35 @@ describe PgSearch::Multisearchable do
184
197
  context "when searching against a single column" do
185
198
  let(:multisearchable_options) { { against: :some_content } }
186
199
  let(:text) { "foo bar" }
200
+
187
201
  before do
188
- allow(record).to receive(:some_content) { text }
202
+ without_partial_double_verification do
203
+ allow(record).to receive(:some_content) { text }
204
+ end
189
205
  record.save
190
206
  end
191
207
 
192
208
  describe '#content' do
193
209
  subject { super().pg_search_document.content }
210
+
194
211
  it { is_expected.to eq(text) }
195
212
  end
196
213
  end
197
214
 
198
215
  context "when searching against multiple columns" do
199
- let(:multisearchable_options) { { against: %i[attr1 attr2] } }
216
+ let(:multisearchable_options) { { against: %i[attr_1 attr_2] } }
217
+
200
218
  before do
201
- allow(record).to receive(:attr1) { '1' }
202
- allow(record).to receive(:attr2) { '2' }
219
+ without_partial_double_verification do
220
+ allow(record).to receive(:attr_1).and_return('1')
221
+ allow(record).to receive(:attr_2).and_return('2')
222
+ end
203
223
  record.save
204
224
  end
205
225
 
206
226
  describe '#content' do
207
227
  subject { super().pg_search_document.content }
228
+
208
229
  it { is_expected.to eq("1 2") }
209
230
  end
210
231
  end
@@ -220,11 +241,14 @@ describe PgSearch::Multisearchable do
220
241
  let(:text) { "foo bar" }
221
242
 
222
243
  it "sets the attributes" do
223
- allow(record).to receive(:bar).and_return(text)
224
- expect(record)
225
- .to receive(:create_pg_search_document)
226
- .with(content: '', foo: text)
227
- record.save
244
+ without_partial_double_verification do
245
+ allow(record).to receive(:bar).and_return(text)
246
+ allow(record).to receive(:create_pg_search_document)
247
+ record.save
248
+ expect(record)
249
+ .to have_received(:create_pg_search_document)
250
+ .with(content: '', foo: text)
251
+ end
228
252
  end
229
253
  end
230
254
 
@@ -239,39 +263,46 @@ describe PgSearch::Multisearchable do
239
263
  let(:text) { "foo bar" }
240
264
 
241
265
  it "creates the document" do
242
- allow(record).to receive(:bar?).and_return(false)
243
- expect(record)
244
- .to receive(:create_pg_search_document)
245
- .with(content: '')
246
- record.save
266
+ without_partial_double_verification do
267
+ allow(record).to receive(:bar?).and_return(false)
268
+ allow(record).to receive(:create_pg_search_document)
269
+ record.save
270
+ expect(record)
271
+ .to have_received(:create_pg_search_document)
272
+ .with(content: '')
273
+ end
247
274
  end
248
275
 
249
- context "the document is created" do
276
+ context "when the document is created" do
250
277
  before { record.save }
251
278
 
252
- context "update_if returns false" do
279
+ context "when update_if returns false" do
253
280
  before do
254
- allow(record).to receive(:bar?).and_return(false)
281
+ without_partial_double_verification do
282
+ allow(record).to receive(:bar?).and_return(false)
283
+ end
255
284
  end
256
285
 
257
286
  it "does not update the document" do
258
- expect_any_instance_of(PgSearch::Document)
259
- .to_not receive(:update)
260
-
261
- record.save
287
+ without_partial_double_verification do
288
+ allow(record.pg_search_document).to receive(:update)
289
+ record.save
290
+ expect(record.pg_search_document).not_to have_received(:update)
291
+ end
262
292
  end
263
293
  end
264
294
 
265
- context "update_if returns true" do
295
+ context "when update_if returns true" do
266
296
  before do
267
- allow(record).to receive(:bar?).and_return(true)
297
+ without_partial_double_verification do
298
+ allow(record).to receive(:bar?).and_return(true)
299
+ end
268
300
  end
269
301
 
270
302
  it "updates the document" do
271
- expect_any_instance_of(PgSearch::Document)
272
- .to receive(:update)
273
-
303
+ allow(record.pg_search_document).to receive(:update)
274
304
  record.save
305
+ expect(record.pg_search_document).to have_received(:update)
275
306
  end
276
307
  end
277
308
  end
@@ -298,14 +329,14 @@ describe PgSearch::Multisearchable do
298
329
  context "when the condition is true" do
299
330
  let(:record) { ModelThatIsMultisearchable.new(multisearchable: true) }
300
331
 
301
- it "should create a PgSearch::Document record" do
332
+ it "creates a PgSearch::Document record" do
302
333
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
303
334
  end
304
335
 
305
336
  context "with multisearch disabled" do
306
337
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
307
338
 
308
- it "should not create a PgSearch::Document record" do
339
+ it "does not create a PgSearch::Document record" do
309
340
  expect { record.save! }.not_to change(PgSearch::Document, :count)
310
341
  end
311
342
  end
@@ -314,7 +345,7 @@ describe PgSearch::Multisearchable do
314
345
  context "when the condition is false" do
315
346
  let(:record) { ModelThatIsMultisearchable.new(multisearchable: false) }
316
347
 
317
- it "should not create a PgSearch::Document record" do
348
+ it "does not create a PgSearch::Document record" do
318
349
  expect { record.save! }.not_to change(PgSearch::Document, :count)
319
350
  end
320
351
  end
@@ -330,11 +361,12 @@ describe PgSearch::Multisearchable do
330
361
  describe "saving the record" do
331
362
  context "when the condition is true" do
332
363
  it "calls save on the pg_search_document" do
333
- expect(record.pg_search_document).to receive(:save)
364
+ allow(record.pg_search_document).to receive(:save)
334
365
  record.save!
366
+ expect(record.pg_search_document).to have_received(:save)
335
367
  end
336
368
 
337
- it "should not create a PgSearch::Document record" do
369
+ it "does not create a PgSearch::Document record" do
338
370
  expect { record.save! }.not_to change(PgSearch::Document, :count)
339
371
  end
340
372
  end
@@ -343,11 +375,12 @@ describe PgSearch::Multisearchable do
343
375
  before { record.multisearchable = false }
344
376
 
345
377
  it "calls destroy on the pg_search_document" do
346
- expect(record.pg_search_document).to receive(:destroy)
378
+ allow(record.pg_search_document).to receive(:destroy)
347
379
  record.save!
380
+ expect(record.pg_search_document).to have_received(:destroy)
348
381
  end
349
382
 
350
- it "should remove its document" do
383
+ it "removes its document" do
351
384
  document = record.pg_search_document
352
385
  expect { record.save! }.to change(PgSearch::Document, :count).by(-1)
353
386
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -359,9 +392,10 @@ describe PgSearch::Multisearchable do
359
392
  allow(PgSearch).to receive(:multisearch_enabled?).and_return(false)
360
393
  end
361
394
 
362
- it "should not create a PgSearch::Document record" do
363
- expect(record.pg_search_document).not_to receive(:save)
395
+ it "does not create a PgSearch::Document record" do
396
+ allow(record.pg_search_document).to receive(:save)
364
397
  expect { record.save! }.not_to change(PgSearch::Document, :count)
398
+ expect(record.pg_search_document).not_to have_received(:save)
365
399
  end
366
400
  end
367
401
  end
@@ -372,14 +406,14 @@ describe PgSearch::Multisearchable do
372
406
 
373
407
  describe "saving the record" do
374
408
  context "when the condition is true" do
375
- it "should create a PgSearch::Document record" do
409
+ it "creates a PgSearch::Document record" do
376
410
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
377
411
  end
378
412
 
379
413
  context "with multisearch disabled" do
380
414
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
381
415
 
382
- it "should not create a PgSearch::Document record" do
416
+ it "does not create a PgSearch::Document record" do
383
417
  expect { record.save! }.not_to change(PgSearch::Document, :count)
384
418
  end
385
419
  end
@@ -388,7 +422,7 @@ describe PgSearch::Multisearchable do
388
422
  context "when the condition is false" do
389
423
  before { record.multisearchable = false }
390
424
 
391
- it "should not create a PgSearch::Document record" do
425
+ it "does not create a PgSearch::Document record" do
392
426
  expect { record.save! }.not_to change(PgSearch::Document, :count)
393
427
  end
394
428
  end
@@ -399,7 +433,7 @@ describe PgSearch::Multisearchable do
399
433
  describe "after_destroy" do
400
434
  let(:record) { ModelThatIsMultisearchable.create!(multisearchable: true) }
401
435
 
402
- it "should remove its document" do
436
+ it "removes its document" do
403
437
  document = record.pg_search_document
404
438
  expect { record.destroy }.to change(PgSearch::Document, :count).by(-1)
405
439
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -426,14 +460,14 @@ describe PgSearch::Multisearchable do
426
460
  context "when the condition is false" do
427
461
  let(:record) { ModelThatIsMultisearchable.new(not_multisearchable: false) }
428
462
 
429
- it "should create a PgSearch::Document record" do
463
+ it "creates a PgSearch::Document record" do
430
464
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
431
465
  end
432
466
 
433
467
  context "with multisearch disabled" do
434
468
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
435
469
 
436
- it "should not create a PgSearch::Document record" do
470
+ it "does not create a PgSearch::Document record" do
437
471
  expect { record.save! }.not_to change(PgSearch::Document, :count)
438
472
  end
439
473
  end
@@ -442,7 +476,7 @@ describe PgSearch::Multisearchable do
442
476
  context "when the condition is true" do
443
477
  let(:record) { ModelThatIsMultisearchable.new(not_multisearchable: true) }
444
478
 
445
- it "should not create a PgSearch::Document record" do
479
+ it "does not create a PgSearch::Document record" do
446
480
  expect { record.save! }.not_to change(PgSearch::Document, :count)
447
481
  end
448
482
  end
@@ -458,21 +492,26 @@ describe PgSearch::Multisearchable do
458
492
  describe "saving the record" do
459
493
  context "when the condition is false" do
460
494
  it "calls save on the pg_search_document" do
461
- expect(record.pg_search_document).to receive(:save)
495
+ allow(record.pg_search_document).to receive(:save)
462
496
  record.save!
497
+ expect(record.pg_search_document).to have_received(:save)
463
498
  end
464
499
 
465
- it "should not create a PgSearch::Document record" do
500
+ it "does not create a PgSearch::Document record" do
466
501
  expect { record.save! }.not_to change(PgSearch::Document, :count)
467
502
  end
468
503
 
469
504
  context "with multisearch disabled" do
470
505
  before do
471
506
  allow(PgSearch).to receive(:multisearch_enabled?).and_return(false)
472
- expect(record.pg_search_document).not_to receive(:save)
507
+ allow(record.pg_search_document).to receive(:save)
508
+ end
509
+
510
+ it "does not call save on the document" do
511
+ expect(record.pg_search_document).not_to have_received(:save)
473
512
  end
474
513
 
475
- it "should not create a PgSearch::Document record" do
514
+ it "does not create a PgSearch::Document record" do
476
515
  expect { record.save! }.not_to change(PgSearch::Document, :count)
477
516
  end
478
517
  end
@@ -482,11 +521,12 @@ describe PgSearch::Multisearchable do
482
521
  before { record.not_multisearchable = true }
483
522
 
484
523
  it "calls destroy on the pg_search_document" do
485
- expect(record.pg_search_document).to receive(:destroy)
524
+ allow(record.pg_search_document).to receive(:destroy)
486
525
  record.save!
526
+ expect(record.pg_search_document).to have_received(:destroy)
487
527
  end
488
528
 
489
- it "should remove its document" do
529
+ it "removes its document" do
490
530
  document = record.pg_search_document
491
531
  expect { record.save! }.to change(PgSearch::Document, :count).by(-1)
492
532
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -500,7 +540,7 @@ describe PgSearch::Multisearchable do
500
540
 
501
541
  describe "saving the record" do
502
542
  context "when the condition is false" do
503
- it "should create a PgSearch::Document record" do
543
+ it "creates a PgSearch::Document record" do
504
544
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
505
545
  end
506
546
  end
@@ -508,7 +548,7 @@ describe PgSearch::Multisearchable do
508
548
  context "when the condition is true" do
509
549
  before { record.not_multisearchable = true }
510
550
 
511
- it "should not create a PgSearch::Document record" do
551
+ it "does not create a PgSearch::Document record" do
512
552
  expect { record.save! }.not_to change(PgSearch::Document, :count)
513
553
  end
514
554
  end
@@ -516,7 +556,7 @@ describe PgSearch::Multisearchable do
516
556
  context "with multisearch disabled" do
517
557
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
518
558
 
519
- it "should not create a PgSearch::Document record" do
559
+ it "does not create a PgSearch::Document record" do
520
560
  expect { record.save! }.not_to change(PgSearch::Document, :count)
521
561
  end
522
562
  end
@@ -525,7 +565,7 @@ describe PgSearch::Multisearchable do
525
565
  end
526
566
 
527
567
  describe "after_destroy" do
528
- it "should remove its document" do
568
+ it "removes its document" do
529
569
  record = ModelThatIsMultisearchable.create!
530
570
  document = record.pg_search_document
531
571
  expect { record.destroy }.to change(PgSearch::Document, :count).by(-1)
@@ -555,14 +595,14 @@ describe PgSearch::Multisearchable do
555
595
  context "when the condition is true" do
556
596
  let(:record) { ModelThatIsMultisearchable.new(multisearchable: true) }
557
597
 
558
- it "should create a PgSearch::Document record" do
598
+ it "creates a PgSearch::Document record" do
559
599
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
560
600
  end
561
601
 
562
602
  context "with multisearch disabled" do
563
603
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
564
604
 
565
- it "should not create a PgSearch::Document record" do
605
+ it "does not create a PgSearch::Document record" do
566
606
  expect { record.save! }.not_to change(PgSearch::Document, :count)
567
607
  end
568
608
  end
@@ -571,7 +611,7 @@ describe PgSearch::Multisearchable do
571
611
  context "when the condition is false" do
572
612
  let(:record) { ModelThatIsMultisearchable.new(multisearchable: false) }
573
613
 
574
- it "should not create a PgSearch::Document record" do
614
+ it "does not create a PgSearch::Document record" do
575
615
  expect { record.save! }.not_to change(PgSearch::Document, :count)
576
616
  end
577
617
  end
@@ -587,21 +627,26 @@ describe PgSearch::Multisearchable do
587
627
  describe "saving the record" do
588
628
  context "when the condition is true" do
589
629
  it "calls save on the pg_search_document" do
590
- expect(record.pg_search_document).to receive(:save)
630
+ allow(record.pg_search_document).to receive(:save)
591
631
  record.save!
632
+ expect(record.pg_search_document).to have_received(:save)
592
633
  end
593
634
 
594
- it "should not create a PgSearch::Document record" do
635
+ it "does not create a PgSearch::Document record" do
595
636
  expect { record.save! }.not_to change(PgSearch::Document, :count)
596
637
  end
597
638
 
598
639
  context "with multisearch disabled" do
599
640
  before do
600
641
  allow(PgSearch).to receive(:multisearch_enabled?).and_return(false)
601
- expect(record.pg_search_document).not_to receive(:save)
642
+ allow(record.pg_search_document).to receive(:save)
602
643
  end
603
644
 
604
- it "should not create a PgSearch::Document record" do
645
+ it "does not call save on the document" do
646
+ expect(record.pg_search_document).not_to have_received(:save)
647
+ end
648
+
649
+ it "does not create a PgSearch::Document record" do
605
650
  expect { record.save! }.not_to change(PgSearch::Document, :count)
606
651
  end
607
652
  end
@@ -611,11 +656,12 @@ describe PgSearch::Multisearchable do
611
656
  before { record.multisearchable = false }
612
657
 
613
658
  it "calls destroy on the pg_search_document" do
614
- expect(record.pg_search_document).to receive(:destroy)
659
+ allow(record.pg_search_document).to receive(:destroy)
615
660
  record.save!
661
+ expect(record.pg_search_document).to have_received(:destroy)
616
662
  end
617
663
 
618
- it "should remove its document" do
664
+ it "removes its document" do
619
665
  document = record.pg_search_document
620
666
  expect { record.save! }.to change(PgSearch::Document, :count).by(-1)
621
667
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -632,7 +678,7 @@ describe PgSearch::Multisearchable do
632
678
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(true) }
633
679
 
634
680
  context "when the condition is true" do
635
- it "should create a PgSearch::Document record" do
681
+ it "creates a PgSearch::Document record" do
636
682
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
637
683
  end
638
684
  end
@@ -640,7 +686,7 @@ describe PgSearch::Multisearchable do
640
686
  context "when the condition is false" do
641
687
  before { record.multisearchable = false }
642
688
 
643
- it "should not create a PgSearch::Document record" do
689
+ it "does not create a PgSearch::Document record" do
644
690
  expect { record.save! }.not_to change(PgSearch::Document, :count)
645
691
  end
646
692
  end
@@ -649,7 +695,7 @@ describe PgSearch::Multisearchable do
649
695
  context "with multisearch disabled" do
650
696
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
651
697
 
652
- it "should not create a PgSearch::Document record" do
698
+ it "does not create a PgSearch::Document record" do
653
699
  expect { record.save! }.not_to change(PgSearch::Document, :count)
654
700
  end
655
701
  end
@@ -660,7 +706,7 @@ describe PgSearch::Multisearchable do
660
706
  describe "after_destroy" do
661
707
  let(:record) { ModelThatIsMultisearchable.create!(multisearchable: true) }
662
708
 
663
- it "should remove its document" do
709
+ it "removes its document" do
664
710
  document = record.pg_search_document
665
711
  expect { record.destroy }.to change(PgSearch::Document, :count).by(-1)
666
712
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -687,7 +733,7 @@ describe PgSearch::Multisearchable do
687
733
  context "when the condition is true" do
688
734
  let(:record) { ModelThatIsMultisearchable.new(not_multisearchable: true) }
689
735
 
690
- it "should not create a PgSearch::Document record" do
736
+ it "does not create a PgSearch::Document record" do
691
737
  expect { record.save! }.not_to change(PgSearch::Document, :count)
692
738
  end
693
739
  end
@@ -695,14 +741,14 @@ describe PgSearch::Multisearchable do
695
741
  context "when the condition is false" do
696
742
  let(:record) { ModelThatIsMultisearchable.new(not_multisearchable: false) }
697
743
 
698
- it "should create a PgSearch::Document record" do
744
+ it "creates a PgSearch::Document record" do
699
745
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
700
746
  end
701
747
 
702
748
  context "with multisearch disabled" do
703
749
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
704
750
 
705
- it "should not create a PgSearch::Document record" do
751
+ it "does not create a PgSearch::Document record" do
706
752
  expect { record.save! }.not_to change(PgSearch::Document, :count)
707
753
  end
708
754
  end
@@ -721,11 +767,12 @@ describe PgSearch::Multisearchable do
721
767
  before { record.not_multisearchable = true }
722
768
 
723
769
  it "calls destroy on the pg_search_document" do
724
- expect(record.pg_search_document).to receive(:destroy)
770
+ allow(record.pg_search_document).to receive(:destroy)
725
771
  record.save!
772
+ expect(record.pg_search_document).to have_received(:destroy)
726
773
  end
727
774
 
728
- it "should remove its document" do
775
+ it "removes its document" do
729
776
  document = record.pg_search_document
730
777
  expect { record.save! }.to change(PgSearch::Document, :count).by(-1)
731
778
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -734,21 +781,26 @@ describe PgSearch::Multisearchable do
734
781
 
735
782
  context "when the condition is false" do
736
783
  it "calls save on the pg_search_document" do
737
- expect(record.pg_search_document).to receive(:save)
784
+ allow(record.pg_search_document).to receive(:save)
738
785
  record.save!
786
+ expect(record.pg_search_document).to have_received(:save)
739
787
  end
740
788
 
741
- it "should not create a PgSearch::Document record" do
789
+ it "does not create a PgSearch::Document record" do
742
790
  expect { record.save! }.not_to change(PgSearch::Document, :count)
743
791
  end
744
792
 
745
793
  context "with multisearch disabled" do
746
794
  before do
747
795
  allow(PgSearch).to receive(:multisearch_enabled?).and_return(false)
748
- expect(record.pg_search_document).not_to receive(:save)
796
+ allow(record.pg_search_document).to receive(:save)
797
+ end
798
+
799
+ it "does not call save on the document" do
800
+ expect(record.pg_search_document).not_to have_received(:save)
749
801
  end
750
802
 
751
- it "should not create a PgSearch::Document record" do
803
+ it "does not create a PgSearch::Document record" do
752
804
  expect { record.save! }.not_to change(PgSearch::Document, :count)
753
805
  end
754
806
  end
@@ -766,20 +818,20 @@ describe PgSearch::Multisearchable do
766
818
  context "when the condition is true" do
767
819
  before { record.not_multisearchable = true }
768
820
 
769
- it "should not create a PgSearch::Document record" do
821
+ it "does not create a PgSearch::Document record" do
770
822
  expect { record.save! }.not_to change(PgSearch::Document, :count)
771
823
  end
772
824
  end
773
825
 
774
826
  context "when the condition is false" do
775
- it "should create a PgSearch::Document record" do
827
+ it "creates a PgSearch::Document record" do
776
828
  expect { record.save! }.to change(PgSearch::Document, :count).by(1)
777
829
  end
778
830
 
779
831
  context "with multisearch disabled" do
780
832
  before { allow(PgSearch).to receive(:multisearch_enabled?).and_return(false) }
781
833
 
782
- it "should not create a PgSearch::Document record" do
834
+ it "does not create a PgSearch::Document record" do
783
835
  expect { record.save! }.not_to change(PgSearch::Document, :count)
784
836
  end
785
837
  end
@@ -792,7 +844,7 @@ describe PgSearch::Multisearchable do
792
844
  describe "after_destroy" do
793
845
  let(:record) { ModelThatIsMultisearchable.create!(not_multisearchable: false) }
794
846
 
795
- it "should remove its document" do
847
+ it "removes its document" do
796
848
  document = record.pg_search_document
797
849
  expect { record.destroy }.to change(PgSearch::Document, :count).by(-1)
798
850
  expect { PgSearch::Document.find(document.id) }.to raise_error(ActiveRecord::RecordNotFound)
@@ -802,3 +854,4 @@ describe PgSearch::Multisearchable do
802
854
  end
803
855
  end
804
856
  end
857
+ # rubocop:enable RSpec/NestedGroups