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
@@ -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,31 @@ 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
34
+ end
35
+
36
+ context "when transactional is false" do
37
+ it "does not operate inside a transaction" do
38
+ allow(model).to receive(:transaction)
39
+
40
+ described_class.rebuild(model, transactional: false)
41
+ expect(model).not_to have_received(:transaction)
42
+ end
32
43
  end
33
44
 
34
45
  describe "cleaning up search documents for this model" do
35
46
  before do
36
- connection.execute <<-SQL.strip_heredoc
47
+ connection.execute <<~SQL.squish
37
48
  INSERT INTO pg_search_documents
38
49
  (searchable_type, searchable_id, content, created_at, updated_at)
39
50
  VALUES
@@ -47,28 +58,39 @@ describe PgSearch::Multisearch do
47
58
  end
48
59
 
49
60
  context "when clean_up is not passed" do
50
- it "should delete the document for the model" do
51
- PgSearch::Multisearch.rebuild(model)
61
+ it "deletes the document for the model" do
62
+ described_class.rebuild(model)
52
63
  expect(PgSearch::Document.count).to eq(1)
53
64
  expect(PgSearch::Document.first.searchable_type).to eq("Bar")
54
65
  end
55
66
  end
56
67
 
57
68
  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)
69
+ it "deletes the document for the model" do
70
+ described_class.rebuild(model, clean_up: true)
62
71
  expect(PgSearch::Document.count).to eq(1)
63
72
  expect(PgSearch::Document.first.searchable_type).to eq("Bar")
64
73
  end
65
74
  end
66
75
 
67
76
  context "when clean_up is false" do
68
- let(:clean_up) { false }
77
+ it "does not delete the document for the model" do
78
+ described_class.rebuild(model, clean_up: false)
79
+ expect(PgSearch::Document.count).to eq(2)
80
+ end
81
+ end
69
82
 
70
- it "should not delete the document for the model" do
71
- PgSearch::Multisearch.rebuild(model, clean_up)
83
+ context "when deprecated_clean_up is true" do
84
+ it "deletes the document for the model" do
85
+ ActiveSupport::Deprecation.silence { described_class.rebuild(model, true) }
86
+ expect(PgSearch::Document.count).to eq(1)
87
+ expect(PgSearch::Document.first.searchable_type).to eq("Bar")
88
+ end
89
+ end
90
+
91
+ context "when deprecated_clean_up is false" do
92
+ it "does not delete the document for the model" do
93
+ ActiveSupport::Deprecation.silence { described_class.rebuild(model, false) }
72
94
  expect(PgSearch::Document.count).to eq(2)
73
95
  end
74
96
  end
@@ -76,7 +98,7 @@ describe PgSearch::Multisearch do
76
98
  context "when the model implements .rebuild_pg_search_documents" do
77
99
  before do
78
100
  def model.rebuild_pg_search_documents
79
- connection.execute <<-SQL.strip_heredoc
101
+ connection.execute <<~SQL.squish
80
102
  INSERT INTO pg_search_documents
81
103
  (searchable_type, searchable_id, content, created_at, updated_at)
82
104
  VALUES
@@ -85,31 +107,36 @@ describe PgSearch::Multisearch do
85
107
  end
86
108
  end
87
109
 
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)
110
+ it "calls .rebuild_pg_search_documents and skips the default behavior" do
111
+ without_partial_double_verification do
112
+ allow(model).to receive(:rebuild_sql)
113
+ described_class.rebuild(model)
91
114
 
92
- record = PgSearch::Document.find_by_searchable_type_and_searchable_id("Baz", 789)
93
- expect(record.content).to eq("baz")
115
+ record = PgSearch::Document.find_by(searchable_type: "Baz", searchable_id: 789)
116
+ expect(model).not_to have_received(:rebuild_sql)
117
+ expect(record.content).to eq("baz")
118
+ end
94
119
  end
95
120
  end
96
121
  end
97
122
 
98
123
  describe "inserting the new documents" do
99
124
  let!(:new_models) { [] }
125
+
100
126
  before do
101
127
  new_models << model.create!(title: "Foo", content: "Bar")
102
128
  new_models << model.create!(title: "Baz", content: "Bar")
103
129
  end
104
130
 
105
- it "should create new documents for the two models" do
106
- PgSearch::Multisearch.rebuild(model)
131
+ it "creates new documents for the two models" do
132
+ described_class.rebuild(model)
107
133
  expect(PgSearch::Document.last(2).map(&:searchable).map(&:title)).to match_array(new_models.map(&:title))
108
134
  end
109
135
  end
110
136
 
111
137
  describe "the generated SQL" do
112
138
  let(:now) { Time.now }
139
+
113
140
  before { allow(Time).to receive(:now).and_return(now) }
114
141
 
115
142
  context "with one attribute" do
@@ -117,8 +144,8 @@ describe PgSearch::Multisearch do
117
144
  model.multisearchable against: [:title]
118
145
  end
119
146
 
120
- it "should generate the proper SQL code" do
121
- expected_sql = <<-SQL.strip_heredoc
147
+ it "generates the proper SQL code" do
148
+ expected_sql = <<~SQL.squish
122
149
  INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
123
150
  SELECT #{connection.quote(model.name)} AS searchable_type,
124
151
  #{model.quoted_table_name}.id AS searchable_id,
@@ -133,7 +160,7 @@ describe PgSearch::Multisearch do
133
160
  statements = []
134
161
  allow(connection).to receive(:execute) { |sql| statements << sql.strip }
135
162
 
136
- PgSearch::Multisearch.rebuild(model)
163
+ described_class.rebuild(model)
137
164
 
138
165
  expect(statements).to include(expected_sql.strip)
139
166
  end
@@ -144,8 +171,8 @@ describe PgSearch::Multisearch do
144
171
  model.multisearchable against: %i[title content]
145
172
  end
146
173
 
147
- it "should generate the proper SQL code" do
148
- expected_sql = <<-SQL.strip_heredoc
174
+ it "generates the proper SQL code" do
175
+ expected_sql = <<~SQL.squish
149
176
  INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content, created_at, updated_at)
150
177
  SELECT #{connection.quote(model.name)} AS searchable_type,
151
178
  #{model.quoted_table_name}.id AS searchable_id,
@@ -160,7 +187,7 @@ describe PgSearch::Multisearch do
160
187
  statements = []
161
188
  allow(connection).to receive(:execute) { |sql| statements << sql.strip }
162
189
 
163
- PgSearch::Multisearch.rebuild(model)
190
+ described_class.rebuild(model)
164
191
 
165
192
  expect(statements).to include(expected_sql.strip)
166
193
  end
@@ -168,3 +195,4 @@ describe PgSearch::Multisearch do
168
195
  end
169
196
  end
170
197
  end
198
+ # rubocop:enable RSpec/NestedGroups