pg_search 2.3.2 → 2.3.7
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.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +11 -0
- data/.github/workflows/ci.yml +80 -0
- data/.jrubyrc +1 -0
- data/.standard.yml +6 -0
- data/CHANGELOG.md +55 -20
- data/CODE_OF_CONDUCT.md +76 -0
- data/Gemfile +19 -6
- data/LICENSE +1 -1
- data/README.md +106 -43
- data/Rakefile +9 -6
- data/lib/pg_search/configuration/column.rb +6 -4
- data/lib/pg_search/configuration/foreign_column.rb +1 -1
- data/lib/pg_search/configuration.rb +13 -3
- data/lib/pg_search/document.rb +9 -9
- data/lib/pg_search/features/dmetaphone.rb +5 -7
- data/lib/pg_search/features/feature.rb +1 -1
- data/lib/pg_search/features/trigram.rb +4 -4
- data/lib/pg_search/features/tsearch.rb +26 -24
- data/lib/pg_search/migration/dmetaphone_generator.rb +2 -2
- data/lib/pg_search/migration/generator.rb +5 -5
- data/lib/pg_search/migration/multisearch_generator.rb +2 -2
- data/lib/pg_search/migration/templates/add_pg_search_dmetaphone_support_functions.rb.erb +6 -6
- data/lib/pg_search/migration/templates/create_pg_search_documents.rb.erb +2 -2
- data/lib/pg_search/model.rb +6 -6
- data/lib/pg_search/multisearch/rebuilder.rb +2 -2
- data/lib/pg_search/multisearch.rb +23 -4
- data/lib/pg_search/multisearchable.rb +7 -7
- data/lib/pg_search/normalizer.rb +5 -5
- data/lib/pg_search/scope_options.rb +31 -13
- data/lib/pg_search/tasks.rb +3 -3
- data/lib/pg_search/version.rb +1 -1
- data/lib/pg_search.rb +5 -5
- data/pg_search.gemspec +16 -24
- data/spec/.rubocop.yml +20 -7
- data/spec/integration/.rubocop.yml +11 -0
- data/spec/integration/associations_spec.rb +121 -160
- data/spec/integration/deprecation_spec.rb +7 -8
- data/spec/integration/pg_search_spec.rb +390 -332
- data/spec/integration/single_table_inheritance_spec.rb +5 -5
- data/spec/lib/pg_search/configuration/association_spec.rb +21 -19
- data/spec/lib/pg_search/configuration/column_spec.rb +13 -1
- data/spec/lib/pg_search/configuration/foreign_column_spec.rb +4 -4
- data/spec/lib/pg_search/features/dmetaphone_spec.rb +4 -4
- data/spec/lib/pg_search/features/trigram_spec.rb +32 -28
- data/spec/lib/pg_search/features/tsearch_spec.rb +57 -33
- data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +94 -63
- data/spec/lib/pg_search/multisearch_spec.rb +57 -29
- data/spec/lib/pg_search/multisearchable_spec.rb +160 -107
- data/spec/lib/pg_search/normalizer_spec.rb +12 -10
- data/spec/lib/pg_search_spec.rb +75 -64
- data/spec/spec_helper.rb +21 -9
- data/spec/support/database.rb +10 -8
- metadata +20 -134
- data/.autotest +0 -5
- data/.codeclimate.yml +0 -17
- data/.rubocop.yml +0 -56
- data/.travis.yml +0 -50
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
require "spec_helper"
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
# standard: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|
|
|
@@ -19,162 +20,162 @@ describe PgSearch do
|
|
|
19
20
|
|
|
20
21
|
model do
|
|
21
22
|
include PgSearch::Model
|
|
22
|
-
belongs_to :another_model, class_name:
|
|
23
|
+
belongs_to :another_model, class_name: "AssociatedModel"
|
|
23
24
|
|
|
24
|
-
pg_search_scope :with_another, associated_against: {
|
|
25
|
+
pg_search_scope :with_another, associated_against: {another_model: :title}
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
it "returns rows that match the query in the columns of the associated model only" do
|
|
29
|
-
associated = AssociatedModel.create!(title:
|
|
30
|
+
associated = AssociatedModel.create!(title: "abcdef")
|
|
30
31
|
included = [
|
|
31
|
-
ModelWithoutAgainst.create!(title:
|
|
32
|
-
ModelWithoutAgainst.create!(title:
|
|
32
|
+
ModelWithoutAgainst.create!(title: "abcdef", another_model: associated),
|
|
33
|
+
ModelWithoutAgainst.create!(title: "ghijkl", another_model: associated)
|
|
33
34
|
]
|
|
34
35
|
excluded = [
|
|
35
|
-
ModelWithoutAgainst.create!(title:
|
|
36
|
+
ModelWithoutAgainst.create!(title: "abcdef")
|
|
36
37
|
]
|
|
37
38
|
|
|
38
|
-
results = ModelWithoutAgainst.with_another(
|
|
39
|
+
results = ModelWithoutAgainst.with_another("abcdef")
|
|
39
40
|
expect(results.map(&:title)).to match_array(included.map(&:title))
|
|
40
41
|
expect(results).not_to include(excluded)
|
|
41
42
|
end
|
|
42
43
|
end
|
|
43
44
|
|
|
44
|
-
context "
|
|
45
|
+
context "via a belongs_to association" do
|
|
45
46
|
with_model :AssociatedModel do
|
|
46
47
|
table do |t|
|
|
47
|
-
t.string
|
|
48
|
+
t.string "title"
|
|
48
49
|
end
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
with_model :ModelWithBelongsTo do
|
|
52
53
|
table do |t|
|
|
53
|
-
t.string
|
|
54
|
-
t.belongs_to
|
|
54
|
+
t.string "title"
|
|
55
|
+
t.belongs_to "another_model", index: false
|
|
55
56
|
end
|
|
56
57
|
|
|
57
58
|
model do
|
|
58
59
|
include PgSearch::Model
|
|
59
|
-
belongs_to :another_model, class_name:
|
|
60
|
+
belongs_to :another_model, class_name: "AssociatedModel"
|
|
60
61
|
|
|
61
|
-
pg_search_scope :with_associated, against: :title, associated_against: {
|
|
62
|
+
pg_search_scope :with_associated, against: :title, associated_against: {another_model: :title}
|
|
62
63
|
end
|
|
63
64
|
end
|
|
64
65
|
|
|
65
66
|
it "returns rows that match the query in either its own columns or the columns of the associated model" do
|
|
66
|
-
associated = AssociatedModel.create!(title:
|
|
67
|
+
associated = AssociatedModel.create!(title: "abcdef")
|
|
67
68
|
included = [
|
|
68
|
-
ModelWithBelongsTo.create!(title:
|
|
69
|
-
ModelWithBelongsTo.create!(title:
|
|
69
|
+
ModelWithBelongsTo.create!(title: "ghijkl", another_model: associated),
|
|
70
|
+
ModelWithBelongsTo.create!(title: "abcdef")
|
|
70
71
|
]
|
|
71
|
-
excluded = ModelWithBelongsTo.create!(title:
|
|
72
|
-
|
|
72
|
+
excluded = ModelWithBelongsTo.create!(title: "mnopqr",
|
|
73
|
+
another_model: AssociatedModel.create!(title: "stuvwx"))
|
|
73
74
|
|
|
74
|
-
results = ModelWithBelongsTo.with_associated(
|
|
75
|
+
results = ModelWithBelongsTo.with_associated("abcdef")
|
|
75
76
|
expect(results.map(&:title)).to match_array(included.map(&:title))
|
|
76
77
|
expect(results).not_to include(excluded)
|
|
77
78
|
end
|
|
78
79
|
end
|
|
79
80
|
|
|
80
|
-
context "
|
|
81
|
+
context "via a has_many association" do
|
|
81
82
|
with_model :AssociatedModelWithHasMany do
|
|
82
83
|
table do |t|
|
|
83
|
-
t.string
|
|
84
|
-
t.belongs_to
|
|
84
|
+
t.string "title"
|
|
85
|
+
t.belongs_to "ModelWithHasMany", index: false
|
|
85
86
|
end
|
|
86
87
|
end
|
|
87
88
|
|
|
88
89
|
with_model :ModelWithHasMany do
|
|
89
90
|
table do |t|
|
|
90
|
-
t.string
|
|
91
|
+
t.string "title"
|
|
91
92
|
end
|
|
92
93
|
|
|
93
94
|
model do
|
|
94
95
|
include PgSearch::Model
|
|
95
|
-
has_many :other_models, class_name:
|
|
96
|
+
has_many :other_models, class_name: "AssociatedModelWithHasMany", foreign_key: "ModelWithHasMany_id"
|
|
96
97
|
|
|
97
|
-
pg_search_scope :with_associated, against: [:title], associated_against: {
|
|
98
|
+
pg_search_scope :with_associated, against: [:title], associated_against: {other_models: :title}
|
|
98
99
|
end
|
|
99
100
|
end
|
|
100
101
|
|
|
101
102
|
it "returns rows that match the query in either its own columns or the columns of the associated model" do
|
|
102
103
|
included = [
|
|
103
|
-
ModelWithHasMany.create!(title:
|
|
104
|
-
AssociatedModelWithHasMany.create!(title:
|
|
105
|
-
AssociatedModelWithHasMany.create!(title:
|
|
104
|
+
ModelWithHasMany.create!(title: "abcdef", other_models: [
|
|
105
|
+
AssociatedModelWithHasMany.create!(title: "foo"),
|
|
106
|
+
AssociatedModelWithHasMany.create!(title: "bar")
|
|
106
107
|
]),
|
|
107
|
-
ModelWithHasMany.create!(title:
|
|
108
|
-
AssociatedModelWithHasMany.create!(title:
|
|
109
|
-
AssociatedModelWithHasMany.create!(title:
|
|
108
|
+
ModelWithHasMany.create!(title: "ghijkl", other_models: [
|
|
109
|
+
AssociatedModelWithHasMany.create!(title: "foo bar"),
|
|
110
|
+
AssociatedModelWithHasMany.create!(title: "mnopqr")
|
|
110
111
|
]),
|
|
111
|
-
ModelWithHasMany.create!(title:
|
|
112
|
+
ModelWithHasMany.create!(title: "foo bar")
|
|
112
113
|
]
|
|
113
|
-
excluded = ModelWithHasMany.create!(title:
|
|
114
|
-
AssociatedModelWithHasMany.create!(title:
|
|
114
|
+
excluded = ModelWithHasMany.create!(title: "stuvwx", other_models: [
|
|
115
|
+
AssociatedModelWithHasMany.create!(title: "abcdef")
|
|
115
116
|
])
|
|
116
117
|
|
|
117
|
-
results = ModelWithHasMany.with_associated(
|
|
118
|
+
results = ModelWithHasMany.with_associated("foo bar")
|
|
118
119
|
expect(results.map(&:title)).to match_array(included.map(&:title))
|
|
119
120
|
expect(results).not_to include(excluded)
|
|
120
121
|
end
|
|
121
122
|
|
|
122
123
|
it "uses an unscoped relation of the associated model" do
|
|
123
|
-
excluded = ModelWithHasMany.create!(title:
|
|
124
|
-
AssociatedModelWithHasMany.create!(title:
|
|
124
|
+
excluded = ModelWithHasMany.create!(title: "abcdef", other_models: [
|
|
125
|
+
AssociatedModelWithHasMany.create!(title: "abcdef")
|
|
125
126
|
])
|
|
126
127
|
|
|
127
128
|
included = [
|
|
128
|
-
ModelWithHasMany.create!(title:
|
|
129
|
-
AssociatedModelWithHasMany.create!(title:
|
|
130
|
-
AssociatedModelWithHasMany.create!(title:
|
|
129
|
+
ModelWithHasMany.create!(title: "abcdef", other_models: [
|
|
130
|
+
AssociatedModelWithHasMany.create!(title: "foo"),
|
|
131
|
+
AssociatedModelWithHasMany.create!(title: "bar")
|
|
131
132
|
])
|
|
132
133
|
]
|
|
133
134
|
|
|
134
135
|
results = ModelWithHasMany
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
136
|
+
.limit(1)
|
|
137
|
+
.order(Arel.sql("#{ModelWithHasMany.quoted_table_name}.id ASC"))
|
|
138
|
+
.with_associated("foo bar")
|
|
138
139
|
|
|
139
140
|
expect(results.map(&:title)).to match_array(included.map(&:title))
|
|
140
141
|
expect(results).not_to include(excluded)
|
|
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
|
-
t.string
|
|
149
|
-
t.belongs_to
|
|
149
|
+
t.string "title"
|
|
150
|
+
t.belongs_to "ModelWithManyAssociations", index: false
|
|
150
151
|
end
|
|
151
152
|
end
|
|
152
153
|
|
|
153
154
|
with_model :SecondAssociatedModel do
|
|
154
155
|
table do |t|
|
|
155
|
-
t.string
|
|
156
|
+
t.string "title"
|
|
156
157
|
end
|
|
157
158
|
end
|
|
158
159
|
|
|
159
160
|
with_model :ModelWithManyAssociations do
|
|
160
161
|
table do |t|
|
|
161
|
-
t.string
|
|
162
|
-
t.belongs_to
|
|
162
|
+
t.string "title"
|
|
163
|
+
t.belongs_to "model_of_second_type", index: false
|
|
163
164
|
end
|
|
164
165
|
|
|
165
166
|
model do
|
|
166
167
|
include PgSearch::Model
|
|
167
168
|
|
|
168
169
|
has_many :models_of_first_type,
|
|
169
|
-
|
|
170
|
-
|
|
170
|
+
class_name: "FirstAssociatedModel",
|
|
171
|
+
foreign_key: "ModelWithManyAssociations_id"
|
|
171
172
|
|
|
172
173
|
belongs_to :model_of_second_type,
|
|
173
|
-
|
|
174
|
+
class_name: "SecondAssociatedModel"
|
|
174
175
|
|
|
175
176
|
pg_search_scope :with_associated,
|
|
176
|
-
|
|
177
|
-
|
|
177
|
+
against: :title,
|
|
178
|
+
associated_against: {models_of_first_type: :title, model_of_second_type: :title}
|
|
178
179
|
end
|
|
179
180
|
end
|
|
180
181
|
|
|
@@ -183,113 +184,113 @@ describe PgSearch do
|
|
|
183
184
|
unmatching_second = SecondAssociatedModel.create!(title: "uiop")
|
|
184
185
|
|
|
185
186
|
included = [
|
|
186
|
-
ModelWithManyAssociations.create!(title:
|
|
187
|
-
FirstAssociatedModel.create!(title:
|
|
188
|
-
FirstAssociatedModel.create!(title:
|
|
187
|
+
ModelWithManyAssociations.create!(title: "abcdef", models_of_first_type: [
|
|
188
|
+
FirstAssociatedModel.create!(title: "foo"),
|
|
189
|
+
FirstAssociatedModel.create!(title: "bar")
|
|
189
190
|
]),
|
|
190
|
-
ModelWithManyAssociations.create!(title:
|
|
191
|
-
FirstAssociatedModel.create!(title:
|
|
192
|
-
FirstAssociatedModel.create!(title:
|
|
191
|
+
ModelWithManyAssociations.create!(title: "ghijkl", models_of_first_type: [
|
|
192
|
+
FirstAssociatedModel.create!(title: "foo bar"),
|
|
193
|
+
FirstAssociatedModel.create!(title: "mnopqr")
|
|
193
194
|
]),
|
|
194
|
-
ModelWithManyAssociations.create!(title:
|
|
195
|
-
ModelWithManyAssociations.create!(title:
|
|
195
|
+
ModelWithManyAssociations.create!(title: "foo bar"),
|
|
196
|
+
ModelWithManyAssociations.create!(title: "qwerty", model_of_second_type: matching_second)
|
|
196
197
|
]
|
|
197
198
|
excluded = [
|
|
198
|
-
ModelWithManyAssociations.create!(title:
|
|
199
|
-
FirstAssociatedModel.create!(title:
|
|
199
|
+
ModelWithManyAssociations.create!(title: "stuvwx", models_of_first_type: [
|
|
200
|
+
FirstAssociatedModel.create!(title: "abcdef")
|
|
200
201
|
]),
|
|
201
|
-
ModelWithManyAssociations.create!(title:
|
|
202
|
+
ModelWithManyAssociations.create!(title: "qwerty", model_of_second_type: unmatching_second)
|
|
202
203
|
]
|
|
203
204
|
|
|
204
|
-
results = ModelWithManyAssociations.with_associated(
|
|
205
|
+
results = ModelWithManyAssociations.with_associated("foo bar")
|
|
205
206
|
expect(results.map(&:title)).to match_array(included.map(&:title))
|
|
206
207
|
excluded.each { |object| expect(results).not_to include(object) }
|
|
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
|
-
t.string
|
|
214
|
-
t.belongs_to
|
|
215
|
-
t.belongs_to
|
|
214
|
+
t.string "title"
|
|
215
|
+
t.belongs_to "ModelWithDoubleAssociation", index: false
|
|
216
|
+
t.belongs_to "ModelWithDoubleAssociation_again", index: false
|
|
216
217
|
end
|
|
217
218
|
end
|
|
218
219
|
|
|
219
220
|
with_model :ModelWithDoubleAssociation do
|
|
220
221
|
table do |t|
|
|
221
|
-
t.string
|
|
222
|
+
t.string "title"
|
|
222
223
|
end
|
|
223
224
|
|
|
224
225
|
model do
|
|
225
226
|
include PgSearch::Model
|
|
226
227
|
|
|
227
228
|
has_many :things,
|
|
228
|
-
|
|
229
|
-
|
|
229
|
+
class_name: "DoublyAssociatedModel",
|
|
230
|
+
foreign_key: "ModelWithDoubleAssociation_id"
|
|
230
231
|
|
|
231
232
|
has_many :thingamabobs,
|
|
232
|
-
|
|
233
|
-
|
|
233
|
+
class_name: "DoublyAssociatedModel",
|
|
234
|
+
foreign_key: "ModelWithDoubleAssociation_again_id"
|
|
234
235
|
|
|
235
236
|
pg_search_scope :with_associated, against: :title,
|
|
236
|
-
|
|
237
|
+
associated_against: {things: :title, thingamabobs: :title}
|
|
237
238
|
end
|
|
238
239
|
end
|
|
239
240
|
|
|
240
241
|
it "returns rows that match the query in either its own columns or the columns of the associated model" do
|
|
241
242
|
included = [
|
|
242
|
-
ModelWithDoubleAssociation.create!(title:
|
|
243
|
-
DoublyAssociatedModel.create!(title:
|
|
244
|
-
DoublyAssociatedModel.create!(title:
|
|
243
|
+
ModelWithDoubleAssociation.create!(title: "abcdef", things: [
|
|
244
|
+
DoublyAssociatedModel.create!(title: "foo"),
|
|
245
|
+
DoublyAssociatedModel.create!(title: "bar")
|
|
245
246
|
]),
|
|
246
|
-
ModelWithDoubleAssociation.create!(title:
|
|
247
|
-
DoublyAssociatedModel.create!(title:
|
|
248
|
-
DoublyAssociatedModel.create!(title:
|
|
247
|
+
ModelWithDoubleAssociation.create!(title: "ghijkl", things: [
|
|
248
|
+
DoublyAssociatedModel.create!(title: "foo bar"),
|
|
249
|
+
DoublyAssociatedModel.create!(title: "mnopqr")
|
|
249
250
|
]),
|
|
250
|
-
ModelWithDoubleAssociation.create!(title:
|
|
251
|
-
ModelWithDoubleAssociation.create!(title:
|
|
251
|
+
ModelWithDoubleAssociation.create!(title: "foo bar"),
|
|
252
|
+
ModelWithDoubleAssociation.create!(title: "qwerty", thingamabobs: [
|
|
252
253
|
DoublyAssociatedModel.create!(title: "foo bar")
|
|
253
254
|
])
|
|
254
255
|
]
|
|
255
256
|
excluded = [
|
|
256
|
-
ModelWithDoubleAssociation.create!(title:
|
|
257
|
-
DoublyAssociatedModel.create!(title:
|
|
257
|
+
ModelWithDoubleAssociation.create!(title: "stuvwx", things: [
|
|
258
|
+
DoublyAssociatedModel.create!(title: "abcdef")
|
|
258
259
|
]),
|
|
259
|
-
ModelWithDoubleAssociation.create!(title:
|
|
260
|
+
ModelWithDoubleAssociation.create!(title: "qwerty", thingamabobs: [
|
|
260
261
|
DoublyAssociatedModel.create!(title: "uiop")
|
|
261
262
|
])
|
|
262
263
|
]
|
|
263
264
|
|
|
264
|
-
results = ModelWithDoubleAssociation.with_associated(
|
|
265
|
+
results = ModelWithDoubleAssociation.with_associated("foo bar")
|
|
265
266
|
expect(results.map(&:title)).to match_array(included.map(&:title))
|
|
266
267
|
excluded.each { |object| expect(results).not_to include(object) }
|
|
267
268
|
end
|
|
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
|
-
t.string
|
|
275
|
-
t.text
|
|
275
|
+
t.string "title"
|
|
276
|
+
t.text "author"
|
|
276
277
|
end
|
|
277
278
|
end
|
|
278
279
|
|
|
279
280
|
with_model :ModelWithAssociation do
|
|
280
281
|
table do |t|
|
|
281
|
-
t.belongs_to
|
|
282
|
+
t.belongs_to "another_model", index: false
|
|
282
283
|
end
|
|
283
284
|
|
|
284
285
|
model do
|
|
285
286
|
include PgSearch::Model
|
|
286
|
-
belongs_to :another_model, class_name:
|
|
287
|
+
belongs_to :another_model, class_name: "AssociatedModel"
|
|
287
288
|
|
|
288
|
-
pg_search_scope :with_associated, associated_against: {
|
|
289
|
+
pg_search_scope :with_associated, associated_against: {another_model: %i[title author]}
|
|
289
290
|
end
|
|
290
291
|
end
|
|
291
292
|
|
|
292
|
-
it "
|
|
293
|
+
it "joins only once" do
|
|
293
294
|
included = [
|
|
294
295
|
ModelWithAssociation.create!(
|
|
295
296
|
another_model: AssociatedModel.create!(
|
|
@@ -313,7 +314,7 @@ describe PgSearch do
|
|
|
313
314
|
)
|
|
314
315
|
]
|
|
315
316
|
|
|
316
|
-
results = ModelWithAssociation.with_associated(
|
|
317
|
+
results = ModelWithAssociation.with_associated("foo bar")
|
|
317
318
|
|
|
318
319
|
expect(results.to_sql.scan("INNER JOIN #{AssociatedModel.quoted_table_name}").length).to eq(1)
|
|
319
320
|
included.each { |object| expect(results).to include(object) }
|
|
@@ -321,28 +322,28 @@ 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
|
-
t.integer
|
|
328
|
+
t.integer "number"
|
|
328
329
|
end
|
|
329
330
|
end
|
|
330
331
|
|
|
331
332
|
with_model :Model do
|
|
332
333
|
table do |t|
|
|
333
|
-
t.integer
|
|
334
|
-
t.belongs_to
|
|
334
|
+
t.integer "number"
|
|
335
|
+
t.belongs_to "another_model", index: false
|
|
335
336
|
end
|
|
336
337
|
|
|
337
338
|
model do
|
|
338
339
|
include PgSearch::Model
|
|
339
|
-
belongs_to :another_model, class_name:
|
|
340
|
+
belongs_to :another_model, class_name: "AssociatedModel"
|
|
340
341
|
|
|
341
|
-
pg_search_scope :with_associated, associated_against: {
|
|
342
|
+
pg_search_scope :with_associated, associated_against: {another_model: :number}
|
|
342
343
|
end
|
|
343
344
|
end
|
|
344
345
|
|
|
345
|
-
it "
|
|
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),
|
|
@@ -352,7 +353,7 @@ describe PgSearch do
|
|
|
352
353
|
Model.create!(number: 123)
|
|
353
354
|
]
|
|
354
355
|
|
|
355
|
-
results = Model.with_associated(
|
|
356
|
+
results = Model.with_associated("123")
|
|
356
357
|
expect(results.map(&:number)).to match_array(included.map(&:number))
|
|
357
358
|
expect(results).not_to include(excluded)
|
|
358
359
|
end
|
|
@@ -383,10 +384,10 @@ describe PgSearch do
|
|
|
383
384
|
|
|
384
385
|
# https://github.com/Casecommons/pg_search/issues/14
|
|
385
386
|
it "supports queries with periods" do
|
|
386
|
-
included = Parent.create!(name:
|
|
387
|
-
excluded = Parent.create!(name:
|
|
387
|
+
included = Parent.create!(name: "bar.foo")
|
|
388
|
+
excluded = Parent.create!(name: "foo.bar")
|
|
388
389
|
|
|
389
|
-
results = Parent.search_name(
|
|
390
|
+
results = Parent.search_name("bar.foo").includes(:children)
|
|
390
391
|
results.to_a
|
|
391
392
|
|
|
392
393
|
expect(results).to include(included)
|
|
@@ -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,11 +417,11 @@ describe PgSearch do
|
|
|
416
417
|
end
|
|
417
418
|
end
|
|
418
419
|
|
|
419
|
-
it "
|
|
420
|
-
included_associated_1 = AssociatedModel.create(content: "foo bar")
|
|
421
|
-
included_associated_2 = AssociatedModel.create(content: "foo baz")
|
|
422
|
-
excluded_associated_1 = AssociatedModel.create(content: "baz quux")
|
|
423
|
-
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")
|
|
424
425
|
|
|
425
426
|
included = [
|
|
426
427
|
ModelWithAssociation.create(associated_models: [included_associated_1]),
|
|
@@ -441,48 +442,7 @@ describe PgSearch do
|
|
|
441
442
|
end
|
|
442
443
|
end
|
|
443
444
|
|
|
444
|
-
context "chained onto a has_many association" do
|
|
445
|
-
with_model :Company do
|
|
446
|
-
model do
|
|
447
|
-
has_many :positions
|
|
448
|
-
end
|
|
449
|
-
end
|
|
450
|
-
|
|
451
|
-
with_model :Position do
|
|
452
|
-
table do |t|
|
|
453
|
-
t.string :title
|
|
454
|
-
t.belongs_to :company
|
|
455
|
-
end
|
|
456
|
-
|
|
457
|
-
model do
|
|
458
|
-
include PgSearch::Model
|
|
459
|
-
pg_search_scope :search, against: :title, using: %i[tsearch trigram]
|
|
460
|
-
end
|
|
461
|
-
end
|
|
462
|
-
|
|
463
|
-
# 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
|
|
445
|
+
context "when chained onto a has_many association" do
|
|
486
446
|
with_model :Company do
|
|
487
447
|
model do
|
|
488
448
|
has_many :positions
|
|
@@ -502,7 +462,7 @@ describe PgSearch do
|
|
|
502
462
|
end
|
|
503
463
|
|
|
504
464
|
# https://github.com/Casecommons/pg_search/issues/106
|
|
505
|
-
it "
|
|
465
|
+
it "handles numbers in a trigram query properly" do
|
|
506
466
|
company = Company.create!
|
|
507
467
|
another_company = Company.create!
|
|
508
468
|
|
|
@@ -517,10 +477,11 @@ describe PgSearch do
|
|
|
517
477
|
Position.create!(company_id: company.id, title: "penn 1")
|
|
518
478
|
]
|
|
519
479
|
|
|
520
|
-
results = company.positions.search(
|
|
480
|
+
results = company.positions.search("teller 1")
|
|
521
481
|
|
|
522
482
|
expect(results).to include(*included)
|
|
523
483
|
expect(results).not_to include(*excluded)
|
|
524
484
|
end
|
|
525
485
|
end
|
|
526
486
|
end
|
|
487
|
+
# standard:enable RSpec/NestedGroups
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "spec_helper"
|
|
4
|
+
require "active_support/core_ext/kernel/reporting"
|
|
4
5
|
|
|
5
6
|
describe "Including the deprecated PgSearch module" do
|
|
6
7
|
with_model :SomeModel do
|
|
7
8
|
model do
|
|
8
|
-
|
|
9
|
+
silence_warnings do
|
|
9
10
|
include PgSearch
|
|
10
11
|
end
|
|
11
12
|
end
|
|
@@ -18,16 +19,14 @@ describe "Including the deprecated PgSearch module" do
|
|
|
18
19
|
end
|
|
19
20
|
|
|
20
21
|
it "prints a deprecation message" do
|
|
21
|
-
allow(
|
|
22
|
+
allow(PgSearch).to receive(:warn)
|
|
22
23
|
|
|
23
24
|
AnotherModel.include(PgSearch)
|
|
24
25
|
|
|
25
|
-
expect(
|
|
26
|
-
|
|
27
|
-
Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.
|
|
26
|
+
expect(PgSearch).to have_received(:warn).with(<<~MESSAGE, category: :deprecated, uplevel: 1)
|
|
27
|
+
Directly including `PgSearch` into an Active Record model is deprecated and will be removed in pg_search 3.0.
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
)
|
|
29
|
+
Please replace `include PgSearch` with `include PgSearch::Model`.
|
|
30
|
+
MESSAGE
|
|
32
31
|
end
|
|
33
32
|
end
|