pg_search 2.1.2 → 2.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +1 -0
  3. data/.editorconfig +10 -0
  4. data/.github/dependabot.yml +11 -0
  5. data/.github/workflows/ci.yml +75 -0
  6. data/.jrubyrc +1 -0
  7. data/.rubocop.yml +95 -10
  8. data/.travis.yml +26 -48
  9. data/CHANGELOG.md +179 -112
  10. data/CODE_OF_CONDUCT.md +76 -0
  11. data/CONTRIBUTING.md +5 -3
  12. data/Gemfile +6 -4
  13. data/LICENSE +1 -1
  14. data/README.md +307 -198
  15. data/Rakefile +7 -3
  16. data/lib/pg_search/configuration/association.rb +2 -0
  17. data/lib/pg_search/configuration/column.rb +2 -0
  18. data/lib/pg_search/configuration/foreign_column.rb +2 -0
  19. data/lib/pg_search/configuration.rb +20 -6
  20. data/lib/pg_search/document.rb +7 -5
  21. data/lib/pg_search/features/dmetaphone.rb +7 -7
  22. data/lib/pg_search/features/feature.rb +4 -2
  23. data/lib/pg_search/features/trigram.rb +31 -5
  24. data/lib/pg_search/features/tsearch.rb +18 -14
  25. data/lib/pg_search/features.rb +2 -0
  26. data/lib/pg_search/migration/dmetaphone_generator.rb +3 -1
  27. data/lib/pg_search/migration/generator.rb +4 -2
  28. data/lib/pg_search/migration/multisearch_generator.rb +2 -1
  29. data/lib/pg_search/migration/templates/add_pg_search_dmetaphone_support_functions.rb.erb +6 -6
  30. data/lib/pg_search/migration/templates/create_pg_search_documents.rb.erb +3 -3
  31. data/lib/pg_search/model.rb +57 -0
  32. data/lib/pg_search/multisearch/rebuilder.rb +14 -6
  33. data/lib/pg_search/multisearch.rb +23 -6
  34. data/lib/pg_search/multisearchable.rb +10 -6
  35. data/lib/pg_search/normalizer.rb +2 -0
  36. data/lib/pg_search/railtie.rb +2 -0
  37. data/lib/pg_search/scope_options.rb +26 -49
  38. data/lib/pg_search/tasks.rb +5 -1
  39. data/lib/pg_search/version.rb +3 -1
  40. data/lib/pg_search.rb +17 -55
  41. data/pg_search.gemspec +19 -11
  42. data/spec/.rubocop.yml +2 -2
  43. data/spec/integration/.rubocop.yml +11 -0
  44. data/spec/integration/associations_spec.rb +125 -162
  45. data/spec/integration/deprecation_spec.rb +33 -0
  46. data/spec/integration/pagination_spec.rb +10 -8
  47. data/spec/integration/pg_search_spec.rb +359 -306
  48. data/spec/integration/single_table_inheritance_spec.rb +18 -17
  49. data/spec/lib/pg_search/configuration/association_spec.rb +17 -13
  50. data/spec/lib/pg_search/configuration/column_spec.rb +2 -0
  51. data/spec/lib/pg_search/configuration/foreign_column_spec.rb +6 -4
  52. data/spec/lib/pg_search/features/dmetaphone_spec.rb +6 -4
  53. data/spec/lib/pg_search/features/trigram_spec.rb +51 -20
  54. data/spec/lib/pg_search/features/tsearch_spec.rb +29 -21
  55. data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +151 -85
  56. data/spec/lib/pg_search/multisearch_spec.rb +67 -37
  57. data/spec/lib/pg_search/multisearchable_spec.rb +217 -123
  58. data/spec/lib/pg_search/normalizer_spec.rb +14 -10
  59. data/spec/lib/pg_search_spec.rb +102 -89
  60. data/spec/spec_helper.rb +25 -6
  61. data/spec/support/database.rb +19 -21
  62. data/spec/support/with_model.rb +2 -0
  63. metadata +106 -29
  64. data/.autotest +0 -5
  65. data/.rubocop_todo.yml +0 -163
  66. data/Guardfile +0 -6
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
- describe PgSearch do
4
- context "joining to another table" do
5
+ # rubocop:disable RSpec/NestedGroups
6
+ describe "a pg_search_scope" do
7
+ context "when joining to another table" do
5
8
  context "without an :against" do
6
9
  with_model :AssociatedModel do
7
10
  table do |t|
@@ -12,25 +15,25 @@ describe PgSearch do
12
15
  with_model :ModelWithoutAgainst do
13
16
  table do |t|
14
17
  t.string "title"
15
- t.belongs_to :another_model, :index => false
18
+ t.belongs_to :another_model, index: false
16
19
  end
17
20
 
18
21
  model do
19
- include PgSearch
20
- belongs_to :another_model, :class_name => 'AssociatedModel'
22
+ include PgSearch::Model
23
+ belongs_to :another_model, class_name: 'AssociatedModel'
21
24
 
22
- pg_search_scope :with_another, :associated_against => {:another_model => :title}
25
+ pg_search_scope :with_another, associated_against: { another_model: :title }
23
26
  end
24
27
  end
25
28
 
26
29
  it "returns rows that match the query in the columns of the associated model only" do
27
- associated = AssociatedModel.create!(:title => 'abcdef')
30
+ associated = AssociatedModel.create!(title: 'abcdef')
28
31
  included = [
29
- ModelWithoutAgainst.create!(:title => 'abcdef', :another_model => associated),
30
- ModelWithoutAgainst.create!(:title => 'ghijkl', :another_model => associated)
32
+ ModelWithoutAgainst.create!(title: 'abcdef', another_model: associated),
33
+ ModelWithoutAgainst.create!(title: 'ghijkl', another_model: associated)
31
34
  ]
32
35
  excluded = [
33
- ModelWithoutAgainst.create!(:title => 'abcdef')
36
+ ModelWithoutAgainst.create!(title: 'abcdef')
34
37
  ]
35
38
 
36
39
  results = ModelWithoutAgainst.with_another('abcdef')
@@ -39,7 +42,7 @@ describe PgSearch do
39
42
  end
40
43
  end
41
44
 
42
- context "through a belongs_to association" do
45
+ context "via a belongs_to association" do
43
46
  with_model :AssociatedModel do
44
47
  table do |t|
45
48
  t.string 'title'
@@ -53,21 +56,21 @@ describe PgSearch do
53
56
  end
54
57
 
55
58
  model do
56
- include PgSearch
57
- belongs_to :another_model, :class_name => 'AssociatedModel'
59
+ include PgSearch::Model
60
+ belongs_to :another_model, class_name: 'AssociatedModel'
58
61
 
59
- pg_search_scope :with_associated, :against => :title, :associated_against => {:another_model => :title}
62
+ pg_search_scope :with_associated, against: :title, associated_against: { another_model: :title }
60
63
  end
61
64
  end
62
65
 
63
66
  it "returns rows that match the query in either its own columns or the columns of the associated model" do
64
- associated = AssociatedModel.create!(:title => 'abcdef')
67
+ associated = AssociatedModel.create!(title: 'abcdef')
65
68
  included = [
66
- ModelWithBelongsTo.create!(:title => 'ghijkl', :another_model => associated),
67
- ModelWithBelongsTo.create!(:title => 'abcdef')
69
+ ModelWithBelongsTo.create!(title: 'ghijkl', another_model: associated),
70
+ ModelWithBelongsTo.create!(title: 'abcdef')
68
71
  ]
69
- excluded = ModelWithBelongsTo.create!(:title => 'mnopqr',
70
- :another_model => AssociatedModel.create!(:title => 'stuvwx'))
72
+ excluded = ModelWithBelongsTo.create!(title: 'mnopqr',
73
+ another_model: AssociatedModel.create!(title: 'stuvwx'))
71
74
 
72
75
  results = ModelWithBelongsTo.with_associated('abcdef')
73
76
  expect(results.map(&:title)).to match_array(included.map(&:title))
@@ -75,7 +78,7 @@ describe PgSearch do
75
78
  end
76
79
  end
77
80
 
78
- context "through a has_many association" do
81
+ context "via a has_many association" do
79
82
  with_model :AssociatedModelWithHasMany do
80
83
  table do |t|
81
84
  t.string 'title'
@@ -89,27 +92,27 @@ describe PgSearch do
89
92
  end
90
93
 
91
94
  model do
92
- include PgSearch
93
- has_many :other_models, :class_name => 'AssociatedModelWithHasMany', :foreign_key => 'ModelWithHasMany_id'
95
+ include PgSearch::Model
96
+ has_many :other_models, class_name: 'AssociatedModelWithHasMany', foreign_key: 'ModelWithHasMany_id'
94
97
 
95
- pg_search_scope :with_associated, :against => [:title], :associated_against => {:other_models => :title}
98
+ pg_search_scope :with_associated, against: [:title], associated_against: { other_models: :title }
96
99
  end
97
100
  end
98
101
 
99
102
  it "returns rows that match the query in either its own columns or the columns of the associated model" do
100
103
  included = [
101
- ModelWithHasMany.create!(:title => 'abcdef', :other_models => [
102
- AssociatedModelWithHasMany.create!(:title => 'foo'),
103
- AssociatedModelWithHasMany.create!(:title => 'bar')
104
+ ModelWithHasMany.create!(title: 'abcdef', other_models: [
105
+ AssociatedModelWithHasMany.create!(title: 'foo'),
106
+ AssociatedModelWithHasMany.create!(title: 'bar')
104
107
  ]),
105
- ModelWithHasMany.create!(:title => 'ghijkl', :other_models => [
106
- AssociatedModelWithHasMany.create!(:title => 'foo bar'),
107
- AssociatedModelWithHasMany.create!(:title => 'mnopqr')
108
+ ModelWithHasMany.create!(title: 'ghijkl', other_models: [
109
+ AssociatedModelWithHasMany.create!(title: 'foo bar'),
110
+ AssociatedModelWithHasMany.create!(title: 'mnopqr')
108
111
  ]),
109
- ModelWithHasMany.create!(:title => 'foo bar')
112
+ ModelWithHasMany.create!(title: 'foo bar')
110
113
  ]
111
- excluded = ModelWithHasMany.create!(:title => 'stuvwx', :other_models => [
112
- AssociatedModelWithHasMany.create!(:title => 'abcdef')
114
+ excluded = ModelWithHasMany.create!(title: 'stuvwx', other_models: [
115
+ AssociatedModelWithHasMany.create!(title: 'abcdef')
113
116
  ])
114
117
 
115
118
  results = ModelWithHasMany.with_associated('foo bar')
@@ -118,14 +121,14 @@ describe PgSearch do
118
121
  end
119
122
 
120
123
  it "uses an unscoped relation of the associated model" do
121
- excluded = ModelWithHasMany.create!(:title => 'abcdef', :other_models => [
122
- AssociatedModelWithHasMany.create!(:title => 'abcdef')
124
+ excluded = ModelWithHasMany.create!(title: 'abcdef', other_models: [
125
+ AssociatedModelWithHasMany.create!(title: 'abcdef')
123
126
  ])
124
127
 
125
128
  included = [
126
- ModelWithHasMany.create!(:title => 'abcdef', :other_models => [
127
- AssociatedModelWithHasMany.create!(:title => 'foo'),
128
- AssociatedModelWithHasMany.create!(:title => 'bar')
129
+ ModelWithHasMany.create!(title: 'abcdef', other_models: [
130
+ AssociatedModelWithHasMany.create!(title: 'foo'),
131
+ AssociatedModelWithHasMany.create!(title: 'bar')
129
132
  ])
130
133
  ]
131
134
 
@@ -139,8 +142,8 @@ describe PgSearch do
139
142
  end
140
143
  end
141
144
 
142
- context "across multiple associations" do
143
- context "on different tables" do
145
+ context "when across multiple associations" do
146
+ context "when on different tables" do
144
147
  with_model :FirstAssociatedModel do
145
148
  table do |t|
146
149
  t.string 'title'
@@ -161,42 +164,42 @@ describe PgSearch do
161
164
  end
162
165
 
163
166
  model do
164
- include PgSearch
167
+ include PgSearch::Model
165
168
 
166
169
  has_many :models_of_first_type,
167
- :class_name => 'FirstAssociatedModel',
168
- :foreign_key => 'ModelWithManyAssociations_id'
170
+ class_name: 'FirstAssociatedModel',
171
+ foreign_key: 'ModelWithManyAssociations_id'
169
172
 
170
173
  belongs_to :model_of_second_type,
171
- :class_name => 'SecondAssociatedModel'
174
+ class_name: 'SecondAssociatedModel'
172
175
 
173
176
  pg_search_scope :with_associated,
174
- :against => :title,
175
- :associated_against => {:models_of_first_type => :title, :model_of_second_type => :title}
177
+ against: :title,
178
+ associated_against: { models_of_first_type: :title, model_of_second_type: :title }
176
179
  end
177
180
  end
178
181
 
179
182
  it "returns rows that match the query in either its own columns or the columns of the associated model" do
180
- matching_second = SecondAssociatedModel.create!(:title => "foo bar")
181
- unmatching_second = SecondAssociatedModel.create!(:title => "uiop")
183
+ matching_second = SecondAssociatedModel.create!(title: "foo bar")
184
+ unmatching_second = SecondAssociatedModel.create!(title: "uiop")
182
185
 
183
186
  included = [
184
- ModelWithManyAssociations.create!(:title => 'abcdef', :models_of_first_type => [
185
- FirstAssociatedModel.create!(:title => 'foo'),
186
- FirstAssociatedModel.create!(:title => 'bar')
187
+ ModelWithManyAssociations.create!(title: 'abcdef', models_of_first_type: [
188
+ FirstAssociatedModel.create!(title: 'foo'),
189
+ FirstAssociatedModel.create!(title: 'bar')
187
190
  ]),
188
- ModelWithManyAssociations.create!(:title => 'ghijkl', :models_of_first_type => [
189
- FirstAssociatedModel.create!(:title => 'foo bar'),
190
- FirstAssociatedModel.create!(:title => 'mnopqr')
191
+ ModelWithManyAssociations.create!(title: 'ghijkl', models_of_first_type: [
192
+ FirstAssociatedModel.create!(title: 'foo bar'),
193
+ FirstAssociatedModel.create!(title: 'mnopqr')
191
194
  ]),
192
- ModelWithManyAssociations.create!(:title => 'foo bar'),
193
- ModelWithManyAssociations.create!(:title => 'qwerty', :model_of_second_type => matching_second)
195
+ ModelWithManyAssociations.create!(title: 'foo bar'),
196
+ ModelWithManyAssociations.create!(title: 'qwerty', model_of_second_type: matching_second)
194
197
  ]
195
198
  excluded = [
196
- ModelWithManyAssociations.create!(:title => 'stuvwx', :models_of_first_type => [
197
- FirstAssociatedModel.create!(:title => 'abcdef')
199
+ ModelWithManyAssociations.create!(title: 'stuvwx', models_of_first_type: [
200
+ FirstAssociatedModel.create!(title: 'abcdef')
198
201
  ]),
199
- ModelWithManyAssociations.create!(:title => 'qwerty', :model_of_second_type => unmatching_second)
202
+ ModelWithManyAssociations.create!(title: 'qwerty', model_of_second_type: unmatching_second)
200
203
  ]
201
204
 
202
205
  results = ModelWithManyAssociations.with_associated('foo bar')
@@ -205,7 +208,7 @@ describe PgSearch do
205
208
  end
206
209
  end
207
210
 
208
- context "on the same table" do
211
+ context "when on the same table" do
209
212
  with_model :DoublyAssociatedModel do
210
213
  table do |t|
211
214
  t.string 'title'
@@ -220,42 +223,42 @@ describe PgSearch do
220
223
  end
221
224
 
222
225
  model do
223
- include PgSearch
226
+ include PgSearch::Model
224
227
 
225
228
  has_many :things,
226
- :class_name => 'DoublyAssociatedModel',
227
- :foreign_key => 'ModelWithDoubleAssociation_id'
229
+ class_name: 'DoublyAssociatedModel',
230
+ foreign_key: 'ModelWithDoubleAssociation_id'
228
231
 
229
232
  has_many :thingamabobs,
230
- :class_name => 'DoublyAssociatedModel',
231
- :foreign_key => 'ModelWithDoubleAssociation_again_id'
233
+ class_name: 'DoublyAssociatedModel',
234
+ foreign_key: 'ModelWithDoubleAssociation_again_id'
232
235
 
233
- pg_search_scope :with_associated, :against => :title,
234
- :associated_against => {:things => :title, :thingamabobs => :title}
236
+ pg_search_scope :with_associated, against: :title,
237
+ associated_against: { things: :title, thingamabobs: :title }
235
238
  end
236
239
  end
237
240
 
238
241
  it "returns rows that match the query in either its own columns or the columns of the associated model" do
239
242
  included = [
240
- ModelWithDoubleAssociation.create!(:title => 'abcdef', :things => [
241
- DoublyAssociatedModel.create!(:title => 'foo'),
242
- DoublyAssociatedModel.create!(:title => 'bar')
243
+ ModelWithDoubleAssociation.create!(title: 'abcdef', things: [
244
+ DoublyAssociatedModel.create!(title: 'foo'),
245
+ DoublyAssociatedModel.create!(title: 'bar')
243
246
  ]),
244
- ModelWithDoubleAssociation.create!(:title => 'ghijkl', :things => [
245
- DoublyAssociatedModel.create!(:title => 'foo bar'),
246
- DoublyAssociatedModel.create!(:title => 'mnopqr')
247
+ ModelWithDoubleAssociation.create!(title: 'ghijkl', things: [
248
+ DoublyAssociatedModel.create!(title: 'foo bar'),
249
+ DoublyAssociatedModel.create!(title: 'mnopqr')
247
250
  ]),
248
- ModelWithDoubleAssociation.create!(:title => 'foo bar'),
249
- ModelWithDoubleAssociation.create!(:title => 'qwerty', :thingamabobs => [
250
- DoublyAssociatedModel.create!(:title => "foo bar")
251
+ ModelWithDoubleAssociation.create!(title: 'foo bar'),
252
+ ModelWithDoubleAssociation.create!(title: 'qwerty', thingamabobs: [
253
+ DoublyAssociatedModel.create!(title: "foo bar")
251
254
  ])
252
255
  ]
253
256
  excluded = [
254
- ModelWithDoubleAssociation.create!(:title => 'stuvwx', :things => [
255
- DoublyAssociatedModel.create!(:title => 'abcdef')
257
+ ModelWithDoubleAssociation.create!(title: 'stuvwx', things: [
258
+ DoublyAssociatedModel.create!(title: 'abcdef')
256
259
  ]),
257
- ModelWithDoubleAssociation.create!(:title => 'qwerty', :thingamabobs => [
258
- DoublyAssociatedModel.create!(:title => "uiop")
260
+ ModelWithDoubleAssociation.create!(title: 'qwerty', thingamabobs: [
261
+ DoublyAssociatedModel.create!(title: "uiop")
259
262
  ])
260
263
  ]
261
264
 
@@ -266,7 +269,7 @@ describe PgSearch do
266
269
  end
267
270
  end
268
271
 
269
- context "against multiple attributes on one association" do
272
+ context "when against multiple attributes on one association" do
270
273
  with_model :AssociatedModel do
271
274
  table do |t|
272
275
  t.string 'title'
@@ -280,33 +283,33 @@ describe PgSearch do
280
283
  end
281
284
 
282
285
  model do
283
- include PgSearch
284
- belongs_to :another_model, :class_name => 'AssociatedModel'
286
+ include PgSearch::Model
287
+ belongs_to :another_model, class_name: 'AssociatedModel'
285
288
 
286
- pg_search_scope :with_associated, :associated_against => {:another_model => %i[title author]}
289
+ pg_search_scope :with_associated, associated_against: { another_model: %i[title author] }
287
290
  end
288
291
  end
289
292
 
290
- it "should only do one join" do
293
+ it "joins only once" do
291
294
  included = [
292
295
  ModelWithAssociation.create!(
293
- :another_model => AssociatedModel.create!(
294
- :title => "foo",
295
- :author => "bar"
296
+ another_model: AssociatedModel.create!(
297
+ title: "foo",
298
+ author: "bar"
296
299
  )
297
300
  ),
298
301
  ModelWithAssociation.create!(
299
- :another_model => AssociatedModel.create!(
300
- :title => "foo bar",
301
- :author => "baz"
302
+ another_model: AssociatedModel.create!(
303
+ title: "foo bar",
304
+ author: "baz"
302
305
  )
303
306
  )
304
307
  ]
305
308
  excluded = [
306
309
  ModelWithAssociation.create!(
307
- :another_model => AssociatedModel.create!(
308
- :title => "foo",
309
- :author => "baz"
310
+ another_model: AssociatedModel.create!(
311
+ title: "foo",
312
+ author: "baz"
310
313
  )
311
314
  )
312
315
  ]
@@ -319,7 +322,7 @@ describe PgSearch do
319
322
  end
320
323
  end
321
324
 
322
- context "against non-text columns" do
325
+ context "when against non-text columns" do
323
326
  with_model :AssociatedModel do
324
327
  table do |t|
325
328
  t.integer 'number'
@@ -333,21 +336,21 @@ describe PgSearch do
333
336
  end
334
337
 
335
338
  model do
336
- include PgSearch
339
+ include PgSearch::Model
337
340
  belongs_to :another_model, class_name: 'AssociatedModel'
338
341
 
339
- pg_search_scope :with_associated, associated_against: {another_model: :number}
342
+ pg_search_scope :with_associated, associated_against: { another_model: :number }
340
343
  end
341
344
  end
342
345
 
343
- it "should cast the columns to text" do
344
- associated = AssociatedModel.create!(:number => 123)
346
+ it "casts the columns to text" do
347
+ associated = AssociatedModel.create!(number: 123)
345
348
  included = [
346
- Model.create!(:number => 123, :another_model => associated),
347
- Model.create!(:number => 456, :another_model => associated)
349
+ Model.create!(number: 123, another_model: associated),
350
+ Model.create!(number: 456, another_model: associated)
348
351
  ]
349
352
  excluded = [
350
- Model.create!(:number => 123)
353
+ Model.create!(number: 123)
351
354
  ]
352
355
 
353
356
  results = Model.with_associated('123')
@@ -364,8 +367,8 @@ describe PgSearch do
364
367
 
365
368
  model do
366
369
  has_many :children
367
- include PgSearch
368
- pg_search_scope :search_name, :against => :name
370
+ include PgSearch::Model
371
+ pg_search_scope :search_name, against: :name
369
372
  end
370
373
  end
371
374
 
@@ -381,8 +384,8 @@ describe PgSearch do
381
384
 
382
385
  # https://github.com/Casecommons/pg_search/issues/14
383
386
  it "supports queries with periods" do
384
- included = Parent.create!(:name => 'bar.foo')
385
- excluded = Parent.create!(:name => 'foo.bar')
387
+ included = Parent.create!(name: 'bar.foo')
388
+ excluded = Parent.create!(name: 'foo.bar')
386
389
 
387
390
  results = Parent.search_name('bar.foo').includes(:children)
388
391
  results.to_a
@@ -393,7 +396,7 @@ describe PgSearch do
393
396
  end
394
397
  end
395
398
 
396
- context "merging a pg_search_scope into another model's scope" do
399
+ context "when merging a pg_search_scope into another model's scope" do
397
400
  with_model :ModelWithAssociation do
398
401
  model do
399
402
  has_many :associated_models
@@ -407,27 +410,27 @@ describe PgSearch do
407
410
  end
408
411
 
409
412
  model do
410
- include PgSearch
413
+ include PgSearch::Model
411
414
  belongs_to :model_with_association
412
415
 
413
- pg_search_scope :search_content, :against => :content
416
+ pg_search_scope :search_content, against: :content
414
417
  end
415
418
  end
416
419
 
417
- it "should find records of the other model" do
418
- included_associated_1 = AssociatedModel.create(:content => "foo bar")
419
- included_associated_2 = AssociatedModel.create(:content => "foo baz")
420
- excluded_associated_1 = AssociatedModel.create(:content => "baz quux")
421
- excluded_associated_2 = AssociatedModel.create(:content => "baz bar")
420
+ it "finds records of the other model" do
421
+ included_associated_1 = AssociatedModel.create(content: "foo bar")
422
+ included_associated_2 = AssociatedModel.create(content: "foo baz")
423
+ excluded_associated_1 = AssociatedModel.create(content: "baz quux")
424
+ excluded_associated_2 = AssociatedModel.create(content: "baz bar")
422
425
 
423
426
  included = [
424
- ModelWithAssociation.create(:associated_models => [included_associated_1]),
425
- ModelWithAssociation.create(:associated_models => [included_associated_2, excluded_associated_1])
427
+ ModelWithAssociation.create(associated_models: [included_associated_1]),
428
+ ModelWithAssociation.create(associated_models: [included_associated_2, excluded_associated_1])
426
429
  ]
427
430
 
428
431
  excluded = [
429
- ModelWithAssociation.create(:associated_models => [excluded_associated_2]),
430
- ModelWithAssociation.create(:associated_models => [])
432
+ ModelWithAssociation.create(associated_models: [excluded_associated_2]),
433
+ ModelWithAssociation.create(associated_models: [])
431
434
  ]
432
435
 
433
436
  relation = AssociatedModel.search_content("foo")
@@ -439,48 +442,7 @@ describe PgSearch do
439
442
  end
440
443
  end
441
444
 
442
- context "chained onto a has_many association" do
443
- with_model :Company do
444
- model do
445
- has_many :positions
446
- end
447
- end
448
-
449
- with_model :Position do
450
- table do |t|
451
- t.string :title
452
- t.belongs_to :company
453
- end
454
-
455
- model do
456
- include PgSearch
457
- pg_search_scope :search, :against => :title, :using => %i[tsearch trigram]
458
- end
459
- end
460
-
461
- # https://github.com/Casecommons/pg_search/issues/106
462
- it "should handle numbers in a trigram query properly" do
463
- company = Company.create!
464
- another_company = Company.create!
465
-
466
- included = [
467
- Position.create!(:company_id => company.id, :title => "teller 1")
468
- ]
469
-
470
- excluded = [
471
- Position.create!(:company_id => nil, :title => "teller 1"),
472
- Position.create!(:company_id => another_company.id, :title => "teller 1"),
473
- Position.create!(:company_id => company.id, :title => "penn 1")
474
- ]
475
-
476
- results = company.positions.search('teller 1')
477
-
478
- expect(results).to include(*included)
479
- expect(results).not_to include(*excluded)
480
- end
481
- end
482
-
483
- context "chained onto a has_many association" do
445
+ context "when chained onto a has_many association" do
484
446
  with_model :Company do
485
447
  model do
486
448
  has_many :positions
@@ -494,13 +456,13 @@ describe PgSearch do
494
456
  end
495
457
 
496
458
  model do
497
- include PgSearch
498
- pg_search_scope :search, :against => :title, :using => %i[tsearch trigram]
459
+ include PgSearch::Model
460
+ pg_search_scope :search, against: :title, using: %i[tsearch trigram]
499
461
  end
500
462
  end
501
463
 
502
464
  # https://github.com/Casecommons/pg_search/issues/106
503
- it "should handle numbers in a trigram query properly" do
465
+ it "handles numbers in a trigram query properly" do
504
466
  company = Company.create!
505
467
  another_company = Company.create!
506
468
 
@@ -522,3 +484,4 @@ describe PgSearch do
522
484
  end
523
485
  end
524
486
  end
487
+ # rubocop:enable RSpec/NestedGroups
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe "Including the deprecated PgSearch module" do
6
+ with_model :SomeModel do
7
+ model do
8
+ ActiveSupport::Deprecation.silence do
9
+ include PgSearch
10
+ end
11
+ end
12
+ end
13
+
14
+ with_model :AnotherModel
15
+
16
+ it "includes PgSearch::Model" do
17
+ expect(SomeModel.ancestors).to include PgSearch::Model
18
+ end
19
+
20
+ it "prints a deprecation message" do
21
+ allow(ActiveSupport::Deprecation).to receive(:warn)
22
+
23
+ AnotherModel.include(PgSearch)
24
+
25
+ expect(ActiveSupport::Deprecation).to have_received(:warn).with(
26
+ <<~MESSAGE
27
+ Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.
28
+
29
+ Please replace `include PgSearch` with `include PgSearch::Model`.
30
+ MESSAGE
31
+ )
32
+ end
33
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe "pagination" do
@@ -8,8 +10,8 @@ describe "pagination" do
8
10
  end
9
11
 
10
12
  model do
11
- include PgSearch
12
- pg_search_scope :search_name, :against => :name
13
+ include PgSearch::Model
14
+ pg_search_scope :search_name, against: :name
13
15
 
14
16
  def self.page(page_number)
15
17
  offset = (page_number - 1) * 2
@@ -19,18 +21,18 @@ describe "pagination" do
19
21
  end
20
22
 
21
23
  it "is chainable before a search scope" do
22
- better = PaginatedModel.create!(:name => "foo foo bar")
23
- best = PaginatedModel.create!(:name => "foo foo foo")
24
- good = PaginatedModel.create!(:name => "foo bar bar")
24
+ better = PaginatedModel.create!(name: "foo foo bar")
25
+ best = PaginatedModel.create!(name: "foo foo foo")
26
+ good = PaginatedModel.create!(name: "foo bar bar")
25
27
 
26
28
  expect(PaginatedModel.page(1).search_name("foo")).to eq([best, better])
27
29
  expect(PaginatedModel.page(2).search_name("foo")).to eq([good])
28
30
  end
29
31
 
30
32
  it "is chainable after a search scope" do
31
- better = PaginatedModel.create!(:name => "foo foo bar")
32
- best = PaginatedModel.create!(:name => "foo foo foo")
33
- good = PaginatedModel.create!(:name => "foo bar bar")
33
+ better = PaginatedModel.create!(name: "foo foo bar")
34
+ best = PaginatedModel.create!(name: "foo foo foo")
35
+ good = PaginatedModel.create!(name: "foo bar bar")
34
36
 
35
37
  expect(PaginatedModel.search_name("foo").page(1)).to eq([best, better])
36
38
  expect(PaginatedModel.search_name("foo").page(2)).to eq([good])