pg_search 2.3.0 → 2.3.6

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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -0
  3. data/.github/dependabot.yml +11 -0
  4. data/.github/workflows/ci.yml +75 -0
  5. data/.jrubyrc +1 -0
  6. data/.rubocop.yml +88 -7
  7. data/.travis.yml +20 -33
  8. data/CHANGELOG.md +50 -16
  9. data/CODE_OF_CONDUCT.md +76 -0
  10. data/Gemfile +1 -1
  11. data/LICENSE +1 -1
  12. data/README.md +73 -26
  13. data/Rakefile +7 -1
  14. data/lib/pg_search/configuration.rb +12 -2
  15. data/lib/pg_search/document.rb +1 -1
  16. data/lib/pg_search/features/dmetaphone.rb +4 -6
  17. data/lib/pg_search/features/feature.rb +1 -1
  18. data/lib/pg_search/features/tsearch.rb +14 -13
  19. data/lib/pg_search/migration/templates/add_pg_search_dmetaphone_support_functions.rb.erb +6 -6
  20. data/lib/pg_search/migration/templates/create_pg_search_documents.rb.erb +2 -2
  21. data/lib/pg_search/multisearch/rebuilder.rb +7 -3
  22. data/lib/pg_search/multisearch.rb +21 -4
  23. data/lib/pg_search/scope_options.rb +5 -8
  24. data/lib/pg_search/tasks.rb +2 -1
  25. data/lib/pg_search/version.rb +1 -1
  26. data/lib/pg_search.rb +6 -8
  27. data/pg_search.gemspec +14 -7
  28. data/spec/.rubocop.yml +2 -2
  29. data/spec/integration/.rubocop.yml +11 -0
  30. data/spec/integration/associations_spec.rb +17 -56
  31. data/spec/integration/deprecation_spec.rb +1 -1
  32. data/spec/integration/pg_search_spec.rb +94 -52
  33. data/spec/lib/pg_search/configuration/association_spec.rb +8 -6
  34. data/spec/lib/pg_search/features/dmetaphone_spec.rb +2 -2
  35. data/spec/lib/pg_search/features/trigram_spec.rb +16 -12
  36. data/spec/lib/pg_search/features/tsearch_spec.rb +16 -10
  37. data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +116 -71
  38. data/spec/lib/pg_search/multisearch_spec.rb +57 -29
  39. data/spec/lib/pg_search/multisearchable_spec.rb +150 -97
  40. data/spec/lib/pg_search/normalizer_spec.rb +12 -10
  41. data/spec/lib/pg_search_spec.rb +66 -55
  42. data/spec/spec_helper.rb +22 -5
  43. data/spec/support/database.rb +7 -5
  44. metadata +109 -19
  45. data/.autotest +0 -5
@@ -2,8 +2,9 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe PgSearch do
6
- 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
7
8
  context "without an :against" do
8
9
  with_model :AssociatedModel do
9
10
  table do |t|
@@ -41,7 +42,7 @@ describe PgSearch do
41
42
  end
42
43
  end
43
44
 
44
- context "through a belongs_to association" do
45
+ context "via a belongs_to association" do
45
46
  with_model :AssociatedModel do
46
47
  table do |t|
47
48
  t.string 'title'
@@ -77,7 +78,7 @@ describe PgSearch do
77
78
  end
78
79
  end
79
80
 
80
- context "through a has_many association" do
81
+ context "via a has_many association" do
81
82
  with_model :AssociatedModelWithHasMany do
82
83
  table do |t|
83
84
  t.string 'title'
@@ -141,8 +142,8 @@ describe PgSearch do
141
142
  end
142
143
  end
143
144
 
144
- context "across multiple associations" do
145
- context "on different tables" do
145
+ context "when across multiple associations" do
146
+ context "when on different tables" do
146
147
  with_model :FirstAssociatedModel do
147
148
  table do |t|
148
149
  t.string 'title'
@@ -207,7 +208,7 @@ describe PgSearch do
207
208
  end
208
209
  end
209
210
 
210
- context "on the same table" do
211
+ context "when on the same table" do
211
212
  with_model :DoublyAssociatedModel do
212
213
  table do |t|
213
214
  t.string 'title'
@@ -268,7 +269,7 @@ describe PgSearch do
268
269
  end
269
270
  end
270
271
 
271
- context "against multiple attributes on one association" do
272
+ context "when against multiple attributes on one association" do
272
273
  with_model :AssociatedModel do
273
274
  table do |t|
274
275
  t.string 'title'
@@ -289,7 +290,7 @@ describe PgSearch do
289
290
  end
290
291
  end
291
292
 
292
- it "should only do one join" do
293
+ it "joins only once" do
293
294
  included = [
294
295
  ModelWithAssociation.create!(
295
296
  another_model: AssociatedModel.create!(
@@ -321,7 +322,7 @@ describe PgSearch do
321
322
  end
322
323
  end
323
324
 
324
- context "against non-text columns" do
325
+ context "when against non-text columns" do
325
326
  with_model :AssociatedModel do
326
327
  table do |t|
327
328
  t.integer 'number'
@@ -342,7 +343,7 @@ describe PgSearch do
342
343
  end
343
344
  end
344
345
 
345
- it "should cast the columns to text" do
346
+ it "casts the columns to text" do
346
347
  associated = AssociatedModel.create!(number: 123)
347
348
  included = [
348
349
  Model.create!(number: 123, another_model: associated),
@@ -395,7 +396,7 @@ describe PgSearch do
395
396
  end
396
397
  end
397
398
 
398
- 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
399
400
  with_model :ModelWithAssociation do
400
401
  model do
401
402
  has_many :associated_models
@@ -416,7 +417,7 @@ describe PgSearch do
416
417
  end
417
418
  end
418
419
 
419
- it "should find records of the other model" do
420
+ it "finds records of the other model" do
420
421
  included_associated_1 = AssociatedModel.create(content: "foo bar")
421
422
  included_associated_2 = AssociatedModel.create(content: "foo baz")
422
423
  excluded_associated_1 = AssociatedModel.create(content: "baz quux")
@@ -441,7 +442,7 @@ describe PgSearch do
441
442
  end
442
443
  end
443
444
 
444
- context "chained onto a has_many association" do
445
+ context "when chained onto a has_many association" do
445
446
  with_model :Company do
446
447
  model do
447
448
  has_many :positions
@@ -461,48 +462,7 @@ describe PgSearch do
461
462
  end
462
463
 
463
464
  # https://github.com/Casecommons/pg_search/issues/106
464
- it "should handle numbers in a trigram query properly" do
465
- company = Company.create!
466
- another_company = Company.create!
467
-
468
- included = [
469
- Position.create!(company_id: company.id, title: "teller 1")
470
- ]
471
-
472
- excluded = [
473
- Position.create!(company_id: nil, title: "teller 1"),
474
- Position.create!(company_id: another_company.id, title: "teller 1"),
475
- Position.create!(company_id: company.id, title: "penn 1")
476
- ]
477
-
478
- results = company.positions.search('teller 1')
479
-
480
- expect(results).to include(*included)
481
- expect(results).not_to include(*excluded)
482
- end
483
- end
484
-
485
- context "chained onto a has_many association" do
486
- with_model :Company do
487
- model do
488
- has_many :positions
489
- end
490
- end
491
-
492
- with_model :Position do
493
- table do |t|
494
- t.string :title
495
- t.belongs_to :company
496
- end
497
-
498
- model do
499
- include PgSearch::Model
500
- pg_search_scope :search, against: :title, using: %i[tsearch trigram]
501
- end
502
- end
503
-
504
- # https://github.com/Casecommons/pg_search/issues/106
505
- it "should handle numbers in a trigram query properly" do
465
+ it "handles numbers in a trigram query properly" do
506
466
  company = Company.create!
507
467
  another_company = Company.create!
508
468
 
@@ -524,3 +484,4 @@ describe PgSearch do
524
484
  end
525
485
  end
526
486
  end
487
+ # rubocop:enable RSpec/NestedGroups
@@ -23,7 +23,7 @@ describe "Including the deprecated PgSearch module" do
23
23
  AnotherModel.include(PgSearch)
24
24
 
25
25
  expect(ActiveSupport::Deprecation).to have_received(:warn).with(
26
- <<-MESSAGE.strip_heredoc
26
+ <<~MESSAGE
27
27
  Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.
28
28
 
29
29
  Please replace `include PgSearch` with `include PgSearch::Model`.
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
+ # rubocop:disable RSpec/NestedGroups
5
6
  describe "an Active Record model which includes PgSearch" do
6
7
  with_model :ModelWithPgSearch do
7
8
  table do |t|
@@ -64,7 +65,7 @@ describe "an Active Record model which includes PgSearch" do
64
65
  }.to raise_error(ArgumentError, /foo/)
65
66
  end
66
67
 
67
- context "dynamically" do
68
+ context "with a lambda" do
68
69
  it "raises an exception when invoked" do
69
70
  ModelWithPgSearch.pg_search_scope :with_unknown_option,
70
71
  ->(*) { { against: :content, foo: :bar } }
@@ -87,7 +88,7 @@ describe "an Active Record model which includes PgSearch" do
87
88
  }.to raise_error(ArgumentError, /foo/)
88
89
  end
89
90
 
90
- context "dynamically" do
91
+ context "with a lambda" do
91
92
  it "raises an exception when invoked" do
92
93
  ModelWithPgSearch.pg_search_scope :with_unknown_using,
93
94
  ->(*) { { against: :content, using: :foo } }
@@ -110,7 +111,7 @@ describe "an Active Record model which includes PgSearch" do
110
111
  }.to raise_error(ArgumentError, /ignoring.*foo/)
111
112
  end
112
113
 
113
- context "dynamically" do
114
+ context "with a lambda" do
114
115
  it "raises an exception when invoked" do
115
116
  ModelWithPgSearch.pg_search_scope :with_unknown_ignoring,
116
117
  ->(*) { { against: :content, ignoring: :foo } }
@@ -130,7 +131,7 @@ describe "an Active Record model which includes PgSearch" do
130
131
  }.to raise_error(ArgumentError, /against/)
131
132
  end
132
133
 
133
- context "dynamically" do
134
+ context "with a lambda" do
134
135
  it "raises an exception when invoked" do
135
136
  ModelWithPgSearch.pg_search_scope :with_unknown_ignoring, ->(*) { {} }
136
137
 
@@ -139,12 +140,28 @@ describe "an Active Record model which includes PgSearch" do
139
140
  }.to raise_error(ArgumentError, /against/)
140
141
  end
141
142
  end
143
+
144
+ context "when a tsvector column is specified" do
145
+ it "does not raise an exception when invoked" do
146
+ ModelWithPgSearch.pg_search_scope :with_unknown_ignoring, {
147
+ using: {
148
+ tsearch: {
149
+ tsvector_column: "tsv"
150
+ }
151
+ }
152
+ }
153
+
154
+ expect {
155
+ ModelWithPgSearch.with_unknown_ignoring("foo")
156
+ }.not_to raise_error
157
+ end
158
+ end
142
159
  end
143
160
  end
144
161
  end
145
162
 
146
163
  describe "a search scope" do
147
- context "against a single column" do
164
+ context "when against a single column" do
148
165
  before do
149
166
  ModelWithPgSearch.pg_search_scope :search_content, against: :content
150
167
  end
@@ -157,7 +174,7 @@ describe "an Active Record model which includes PgSearch" do
157
174
  results = ModelWithPgSearch.select('id, title').search_content('foo')
158
175
 
159
176
  expect(results).to include(included)
160
- expect(results).to_not include(excluded)
177
+ expect(results).not_to include(excluded)
161
178
 
162
179
  expect(results.first.attributes.key?('content')).to eq false
163
180
 
@@ -174,7 +191,7 @@ describe "an Active Record model which includes PgSearch" do
174
191
  results = ModelWithPgSearch.search_content('foo').select('id, title')
175
192
 
176
193
  expect(results).to include(included)
177
- expect(results).to_not include(excluded)
194
+ expect(results).not_to include(excluded)
178
195
 
179
196
  expect(results.first.attributes.key?('content')).to eq false
180
197
 
@@ -191,7 +208,7 @@ describe "an Active Record model which includes PgSearch" do
191
208
  results = ModelWithPgSearch.select('id').search_content('foo').select('title')
192
209
 
193
210
  expect(results).to include(included)
194
- expect(results).to_not include(excluded)
211
+ expect(results).not_to include(excluded)
195
212
 
196
213
  expect(results.first.attributes.key?('content')).to eq false
197
214
 
@@ -200,7 +217,7 @@ describe "an Active Record model which includes PgSearch" do
200
217
  end
201
218
  end
202
219
 
203
- context "chained to a cross-table scope" do
220
+ context "when chained to a cross-table scope" do
204
221
  with_model :House do
205
222
  table do |t|
206
223
  t.references :person
@@ -270,7 +287,7 @@ describe "an Active Record model which includes PgSearch" do
270
287
  it "does not raise an exception" do
271
288
  relation = Person.named('foo').house_search_city('bar')
272
289
 
273
- expect { relation.to_a }.to_not raise_error
290
+ expect { relation.to_a }.not_to raise_error
274
291
  end
275
292
  end
276
293
  end
@@ -283,7 +300,7 @@ describe "an Active Record model which includes PgSearch" do
283
300
  it "does not raise an exception" do
284
301
  relation = ModelWithPgSearch.search_content('foo').search_title('bar')
285
302
 
286
- expect { relation.to_a }.to_not raise_error
303
+ expect { relation.to_a }.not_to raise_error
287
304
  end
288
305
  end
289
306
 
@@ -406,6 +423,15 @@ describe "an Active Record model which includes PgSearch" do
406
423
  expect(results).to eq([winner, loser])
407
424
  end
408
425
 
426
+ it 'preserves column selection when with_pg_search_rank is chained after a select()' do
427
+ loser = ModelWithPgSearch.create!(title: 'foo', content: 'bar')
428
+
429
+ results = ModelWithPgSearch.search_content('bar').select(:content).with_pg_search_rank
430
+
431
+ expect(results.length).to be 1
432
+ expect(results.first.as_json.keys).to contain_exactly('id', 'content', 'pg_search_rank')
433
+ end
434
+
409
435
  it 'allows pg_search_rank along with a join' do
410
436
  parent_1 = ParentModel.create!(id: 98)
411
437
  parent_2 = ParentModel.create!(id: 99)
@@ -451,7 +477,7 @@ describe "an Active Record model which includes PgSearch" do
451
477
 
452
478
  it "accepts non-string queries and calls #to_s on them" do
453
479
  foo = ModelWithPgSearch.create!(content: "foo")
454
- not_a_string = double(to_s: "foo")
480
+ not_a_string = instance_double("Object", to_s: "foo")
455
481
  expect(ModelWithPgSearch.search_content(not_a_string)).to eq([foo])
456
482
  end
457
483
 
@@ -481,7 +507,7 @@ describe "an Active Record model which includes PgSearch" do
481
507
  end
482
508
  end
483
509
 
484
- context "against multiple columns" do
510
+ context "when against multiple columns" do
485
511
  before do
486
512
  ModelWithPgSearch.pg_search_scope :search_title_and_content, against: %i[title content]
487
513
  end
@@ -520,7 +546,7 @@ describe "an Active Record model which includes PgSearch" do
520
546
  end
521
547
  end
522
548
 
523
- context "using trigram" do
549
+ context "when using trigram" do
524
550
  before do
525
551
  ModelWithPgSearch.pg_search_scope :with_trigrams, against: %i[title content], using: :trigram
526
552
  end
@@ -562,7 +588,7 @@ describe "an Active Record model which includes PgSearch" do
562
588
  end
563
589
  end
564
590
 
565
- context "using tsearch" do
591
+ context "when using tsearch" do
566
592
  before do
567
593
  ModelWithPgSearch.pg_search_scope :search_title_with_prefixes,
568
594
  against: :title,
@@ -613,7 +639,7 @@ describe "an Active Record model which includes PgSearch" do
613
639
  describe "highlighting" do
614
640
  before do
615
641
  ["Strip Down", "Down", "Down and Out", "Won't Let You Down"].each do |name|
616
- ModelWithPgSearch.create! content: name
642
+ ModelWithPgSearch.create! title: 'Just a title', content: name
617
643
  end
618
644
  end
619
645
 
@@ -634,6 +660,12 @@ describe "an Active Record model which includes PgSearch" do
634
660
 
635
661
  expect(result.pg_search_highlight).to eq("Won't <b>Let</b> You Down")
636
662
  end
663
+
664
+ it 'preserves column selection when with_pg_search_highlight is chained after a select()' do
665
+ result = ModelWithPgSearch.search_content("Let").select(:content).with_pg_search_highlight.first
666
+
667
+ expect(result.as_json.keys).to contain_exactly('id', 'content', 'pg_search_highlight')
668
+ end
637
669
  end
638
670
 
639
671
  context "with custom highlighting options" do
@@ -712,7 +744,7 @@ describe "an Active Record model which includes PgSearch" do
712
744
  end
713
745
  end
714
746
 
715
- context "against columns ranked with arrays" do
747
+ context "when against columns ranked with arrays" do
716
748
  before do
717
749
  ModelWithPgSearch.pg_search_scope :search_weighted_by_array_of_arrays,
718
750
  against: [[:content, 'B'], [:title, 'A']]
@@ -728,7 +760,7 @@ describe "an Active Record model which includes PgSearch" do
728
760
  end
729
761
  end
730
762
 
731
- context "against columns ranked with a hash" do
763
+ context "when against columns ranked with a hash" do
732
764
  before do
733
765
  ModelWithPgSearch.pg_search_scope :search_weighted_by_hash,
734
766
  against: { content: 'B', title: 'A' }
@@ -744,7 +776,7 @@ describe "an Active Record model which includes PgSearch" do
744
776
  end
745
777
  end
746
778
 
747
- context "against columns of which only some are ranked" do
779
+ context "when against columns of which only some are ranked" do
748
780
  before do
749
781
  ModelWithPgSearch.pg_search_scope :search_weighted,
750
782
  against: [:content, [:title, 'A']]
@@ -760,7 +792,7 @@ describe "an Active Record model which includes PgSearch" do
760
792
  end
761
793
  end
762
794
 
763
- context "searching any_word option" do
795
+ context "when searching any_word option" do
764
796
  before do
765
797
  ModelWithPgSearch.pg_search_scope :search_title_with_any_word,
766
798
  against: :title,
@@ -838,7 +870,7 @@ describe "an Active Record model which includes PgSearch" do
838
870
  end
839
871
  end
840
872
 
841
- context "using dmetaphone" do
873
+ context "when using dmetaphone" do
842
874
  before do
843
875
  ModelWithPgSearch.pg_search_scope :with_dmetaphones,
844
876
  against: %i[title content],
@@ -877,7 +909,7 @@ describe "an Active Record model which includes PgSearch" do
877
909
  end
878
910
  end
879
911
 
880
- context "using multiple features" do
912
+ context "when using multiple features" do
881
913
  before do
882
914
  ModelWithPgSearch.pg_search_scope :with_tsearch,
883
915
  against: :title,
@@ -950,33 +982,40 @@ describe "an Active Record model which includes PgSearch" do
950
982
  end
951
983
 
952
984
  context "with feature-specific configuration" do
953
- before do
954
- @tsearch_config = tsearch_config = { dictionary: 'english' }
955
- @trigram_config = trigram_config = { foo: 'bar' }
985
+ let(:tsearch_config) { { dictionary: 'english' } }
986
+ let(:trigram_config) { { foo: 'bar' } }
956
987
 
988
+ before do
957
989
  ModelWithPgSearch.pg_search_scope :with_tsearch_and_trigram_using_hash,
958
990
  against: :title,
959
- using: {
960
- tsearch: tsearch_config,
961
- trigram: trigram_config
962
- }
991
+ using: { tsearch: tsearch_config, trigram: trigram_config }
963
992
  end
964
993
 
965
- it "should pass the custom configuration down to the specified feature" do
966
- stub_feature = double(
994
+ it "passes the custom configuration down to the specified feature" do
995
+ tsearch_feature = instance_double(
996
+ "PgSearch::Features::TSearch",
967
997
  conditions: Arel::Nodes::Grouping.new(Arel.sql("1 = 1")),
968
998
  rank: Arel::Nodes::Grouping.new(Arel.sql("1.0"))
969
999
  )
970
1000
 
971
- expect(PgSearch::Features::TSearch).to receive(:new).with(anything, @tsearch_config, anything, anything, anything).at_least(:once).and_return(stub_feature)
972
- expect(PgSearch::Features::Trigram).to receive(:new).with(anything, @trigram_config, anything, anything, anything).at_least(:once).and_return(stub_feature)
1001
+ trigram_feature = instance_double(
1002
+ "PgSearch::Features::Trigram",
1003
+ conditions: Arel::Nodes::Grouping.new(Arel.sql("1 = 1")),
1004
+ rank: Arel::Nodes::Grouping.new(Arel.sql("1.0"))
1005
+ )
1006
+
1007
+ allow(PgSearch::Features::TSearch).to receive(:new).with(anything, tsearch_config, anything, anything, anything).and_return(tsearch_feature)
1008
+ allow(PgSearch::Features::Trigram).to receive(:new).with(anything, trigram_config, anything, anything, anything).and_return(trigram_feature)
973
1009
 
974
1010
  ModelWithPgSearch.with_tsearch_and_trigram_using_hash("foo")
1011
+
1012
+ expect(PgSearch::Features::TSearch).to have_received(:new).with(anything, tsearch_config, anything, anything, anything).at_least(:once)
1013
+ expect(PgSearch::Features::Trigram).to have_received(:new).with(anything, trigram_config, anything, anything, anything).at_least(:once)
975
1014
  end
976
1015
  end
977
1016
  end
978
1017
 
979
- context "using a tsvector column and an association" do
1018
+ context "when using a tsvector column and an association" do
980
1019
  with_model :Comment do
981
1020
  table do |t|
982
1021
  t.integer :post_id
@@ -1004,7 +1043,7 @@ describe "an Active Record model which includes PgSearch" do
1004
1043
  let!(:unexpected) { Post.create!(content: 'longcat is looooooooong') }
1005
1044
 
1006
1045
  before do
1007
- ActiveRecord::Base.connection.execute <<-SQL.strip_heredoc
1046
+ ActiveRecord::Base.connection.execute <<~SQL.squish
1008
1047
  UPDATE #{Post.quoted_table_name}
1009
1048
  SET content_tsvector = to_tsvector('english'::regconfig, #{Post.quoted_table_name}."content")
1010
1049
  SQL
@@ -1022,20 +1061,20 @@ describe "an Active Record model which includes PgSearch" do
1022
1061
  }
1023
1062
  end
1024
1063
 
1025
- it "should find by the tsvector column" do
1064
+ it "finds by the tsvector column" do
1026
1065
  expect(Post.search_by_content_with_tsvector("phooey").map(&:id)).to eq([expected.id])
1027
1066
  end
1028
1067
 
1029
- it "should find by the associated record" do
1068
+ it "finds by the associated record" do
1030
1069
  expect(Post.search_by_content_with_tsvector("commentone").map(&:id)).to eq([expected.id])
1031
1070
  end
1032
1071
 
1033
- it 'should find by a combination of the two' do
1072
+ it 'finds by a combination of the two' do
1034
1073
  expect(Post.search_by_content_with_tsvector("phooey commentone").map(&:id)).to eq([expected.id])
1035
1074
  end
1036
1075
  end
1037
1076
 
1038
- context 'using multiple tsvector columns' do
1077
+ context 'when using multiple tsvector columns' do
1039
1078
  with_model :ModelWithTsvector do
1040
1079
  model do
1041
1080
  include PgSearch::Model
@@ -1059,7 +1098,7 @@ describe "an Active Record model which includes PgSearch" do
1059
1098
  end
1060
1099
  end
1061
1100
 
1062
- context "using a tsvector column with" do
1101
+ context "when using a tsvector column with" do
1063
1102
  with_model :ModelWithTsvector do
1064
1103
  table do |t|
1065
1104
  t.text 'content'
@@ -1070,10 +1109,11 @@ describe "an Active Record model which includes PgSearch" do
1070
1109
  end
1071
1110
 
1072
1111
  let!(:expected) { ModelWithTsvector.create!(content: 'tiling is grouty') }
1073
- let!(:unexpected) { ModelWithTsvector.create!(content: 'longcat is looooooooong') }
1074
1112
 
1075
1113
  before do
1076
- ActiveRecord::Base.connection.execute <<-SQL.strip_heredoc
1114
+ ModelWithTsvector.create!(content: 'longcat is looooooooong')
1115
+
1116
+ ActiveRecord::Base.connection.execute <<~SQL.squish
1077
1117
  UPDATE #{ModelWithTsvector.quoted_table_name}
1078
1118
  SET content_tsvector = to_tsvector('english'::regconfig, #{ModelWithTsvector.quoted_table_name}."content")
1079
1119
  SQL
@@ -1088,11 +1128,11 @@ describe "an Active Record model which includes PgSearch" do
1088
1128
  }
1089
1129
  end
1090
1130
 
1091
- it "should not use to_tsvector in the query" do
1131
+ it "does not use to_tsvector in the query" do
1092
1132
  expect(ModelWithTsvector.search_by_content_with_tsvector("tiles").to_sql).not_to match(/to_tsvector/)
1093
1133
  end
1094
1134
 
1095
- it "should find the expected result" do
1135
+ it "finds the expected result" do
1096
1136
  expect(ModelWithTsvector.search_by_content_with_tsvector("tiles").map(&:id)).to eq([expected.id])
1097
1137
  end
1098
1138
 
@@ -1108,7 +1148,7 @@ describe "an Active Record model which includes PgSearch" do
1108
1148
  ModelWithTsvector.has_many :another_models
1109
1149
  end
1110
1150
 
1111
- it "should refer to the tsvector column in the query unambiguously" do
1151
+ it "refers to the tsvector column in the query unambiguously" do
1112
1152
  expect {
1113
1153
  ModelWithTsvector.joins(:another_models).search_by_content_with_tsvector("test").to_a
1114
1154
  }.not_to raise_exception
@@ -1116,7 +1156,7 @@ describe "an Active Record model which includes PgSearch" do
1116
1156
  end
1117
1157
  end
1118
1158
 
1119
- context "ignoring accents" do
1159
+ context "when ignoring accents" do
1120
1160
  before do
1121
1161
  ModelWithPgSearch.pg_search_scope :search_title_without_accents,
1122
1162
  against: :title,
@@ -1157,13 +1197,13 @@ describe "an Active Record model which includes PgSearch" do
1157
1197
  ranked_by: ":tsearch * importance"
1158
1198
  end
1159
1199
 
1160
- it "should return records with a rank attribute equal to the :ranked_by expression" do
1200
+ it "returns records with a rank attribute equal to the :ranked_by expression" do
1161
1201
  ModelWithPgSearch.create!(content: 'foo', importance: 10)
1162
1202
  results = ModelWithPgSearch.search_content_with_importance_as_rank("foo").with_pg_search_rank
1163
1203
  expect(results.first.pg_search_rank).to eq(10)
1164
1204
  end
1165
1205
 
1166
- it "should substitute :tsearch with the tsearch rank expression in the :ranked_by expression" do
1206
+ it "substitutes :tsearch with the tsearch rank expression in the :ranked_by expression" do
1167
1207
  ModelWithPgSearch.create!(content: 'foo', importance: 10)
1168
1208
 
1169
1209
  tsearch_result =
@@ -1181,7 +1221,7 @@ describe "an Active Record model which includes PgSearch" do
1181
1221
  expect(multiplied_rank).to be_within(0.001).of(tsearch_rank * 10)
1182
1222
  end
1183
1223
 
1184
- it "should return results in descending order of the value of the rank expression" do
1224
+ it "returns results in descending order of the value of the rank expression" do
1185
1225
  records = [
1186
1226
  ModelWithPgSearch.create!(content: 'foo', importance: 1),
1187
1227
  ModelWithPgSearch.create!(content: 'foo', importance: 3),
@@ -1195,6 +1235,7 @@ describe "an Active Record model which includes PgSearch" do
1195
1235
  %w[tsearch trigram dmetaphone].each do |feature|
1196
1236
  context "using the #{feature} ranking algorithm" do
1197
1237
  let(:scope_name) { :"search_content_ranked_by_#{feature}" }
1238
+
1198
1239
  before do
1199
1240
  ModelWithPgSearch.pg_search_scope scope_name,
1200
1241
  against: :content,
@@ -1231,7 +1272,7 @@ describe "an Active Record model which includes PgSearch" do
1231
1272
  end
1232
1273
  end
1233
1274
 
1234
- context "using the tsearch ranking algorithm" do
1275
+ context "when using the tsearch ranking algorithm" do
1235
1276
  it "sorts results by the tsearch rank" do
1236
1277
  ModelWithPgSearch.pg_search_scope :search_content_ranked_by_tsearch,
1237
1278
  using: :tsearch,
@@ -1246,7 +1287,7 @@ describe "an Active Record model which includes PgSearch" do
1246
1287
  end
1247
1288
  end
1248
1289
 
1249
- context "using the trigram ranking algorithm" do
1290
+ context "when using the trigram ranking algorithm" do
1250
1291
  it "sorts results by the trigram rank" do
1251
1292
  ModelWithPgSearch.pg_search_scope :search_content_ranked_by_trigram,
1252
1293
  using: :trigram,
@@ -1261,7 +1302,7 @@ describe "an Active Record model which includes PgSearch" do
1261
1302
  end
1262
1303
  end
1263
1304
 
1264
- context "using the dmetaphone ranking algorithm" do
1305
+ context "when using the dmetaphone ranking algorithm" do
1265
1306
  it "sorts results by the dmetaphone rank" do
1266
1307
  ModelWithPgSearch.pg_search_scope :search_content_ranked_by_dmetaphone,
1267
1308
  using: :dmetaphone,
@@ -1298,3 +1339,4 @@ describe "an Active Record model which includes PgSearch" do
1298
1339
  end
1299
1340
  end
1300
1341
  end
1342
+ # rubocop:enable RSpec/NestedGroups