pg_search 2.1.2 → 2.2.0
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 +5 -5
- data/.rubocop.yml +11 -7
- data/.travis.yml +33 -42
- data/CHANGELOG.md +140 -112
- data/CONTRIBUTING.md +5 -3
- data/Gemfile +6 -4
- data/LICENSE +1 -1
- data/README.md +221 -159
- data/Rakefile +3 -5
- data/lib/pg_search/configuration/association.rb +2 -0
- data/lib/pg_search/configuration/column.rb +2 -0
- data/lib/pg_search/configuration/foreign_column.rb +2 -0
- data/lib/pg_search/configuration.rb +8 -4
- data/lib/pg_search/document.rb +5 -3
- data/lib/pg_search/features/dmetaphone.rb +3 -1
- data/lib/pg_search/features/feature.rb +4 -2
- data/lib/pg_search/features/trigram.rb +31 -5
- data/lib/pg_search/features/tsearch.rb +4 -1
- data/lib/pg_search/features.rb +2 -0
- data/lib/pg_search/migration/dmetaphone_generator.rb +3 -1
- data/lib/pg_search/migration/generator.rb +4 -2
- data/lib/pg_search/migration/multisearch_generator.rb +2 -1
- data/lib/pg_search/migration/templates/create_pg_search_documents.rb.erb +1 -1
- data/lib/pg_search/multisearch/rebuilder.rb +7 -3
- data/lib/pg_search/multisearch.rb +4 -4
- data/lib/pg_search/multisearchable.rb +10 -6
- data/lib/pg_search/normalizer.rb +2 -0
- data/lib/pg_search/railtie.rb +2 -0
- data/lib/pg_search/scope_options.rb +21 -41
- data/lib/pg_search/tasks.rb +3 -0
- data/lib/pg_search/version.rb +3 -1
- data/lib/pg_search.rb +10 -5
- data/pg_search.gemspec +8 -7
- data/spec/integration/associations_spec.rb +103 -101
- data/spec/integration/pagination_spec.rb +9 -7
- data/spec/integration/pg_search_spec.rb +266 -255
- data/spec/integration/single_table_inheritance_spec.rb +16 -15
- data/spec/lib/pg_search/configuration/association_spec.rb +7 -5
- data/spec/lib/pg_search/configuration/column_spec.rb +2 -0
- data/spec/lib/pg_search/configuration/foreign_column_spec.rb +5 -3
- data/spec/lib/pg_search/features/dmetaphone_spec.rb +6 -4
- data/spec/lib/pg_search/features/trigram_spec.rb +39 -12
- data/spec/lib/pg_search/features/tsearch_spec.rb +23 -21
- data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +32 -11
- data/spec/lib/pg_search/multisearch_spec.rb +9 -7
- data/spec/lib/pg_search/multisearchable_spec.rb +68 -27
- data/spec/lib/pg_search/normalizer_spec.rb +7 -5
- data/spec/lib/pg_search_spec.rb +33 -31
- data/spec/spec_helper.rb +3 -1
- data/spec/support/database.rb +16 -20
- data/spec/support/with_model.rb +2 -0
- metadata +13 -30
- data/.rubocop_todo.yml +0 -163
- data/Guardfile +0 -6
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe "a pg_search_scope on an STI subclass" do
|
@@ -10,7 +12,7 @@ describe "a pg_search_scope on an STI subclass" do
|
|
10
12
|
|
11
13
|
model do
|
12
14
|
include PgSearch
|
13
|
-
pg_search_scope :search_content, :
|
15
|
+
pg_search_scope :search_content, against: :content
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
@@ -21,14 +23,14 @@ describe "a pg_search_scope on an STI subclass" do
|
|
21
23
|
|
22
24
|
it "returns only results for that subclass" do
|
23
25
|
included = [
|
24
|
-
SearchableSubclassModel.create!(:
|
26
|
+
SearchableSubclassModel.create!(content: "foo bar")
|
25
27
|
]
|
26
28
|
excluded = [
|
27
|
-
SearchableSubclassModel.create!(:
|
28
|
-
SuperclassModel.create!(:
|
29
|
-
SuperclassModel.create!(:
|
30
|
-
AnotherSearchableSubclassModel.create!(:
|
31
|
-
AnotherSearchableSubclassModel.create!(:
|
29
|
+
SearchableSubclassModel.create!(content: "baz"),
|
30
|
+
SuperclassModel.create!(content: "foo bar"),
|
31
|
+
SuperclassModel.create!(content: "baz"),
|
32
|
+
AnotherSearchableSubclassModel.create!(content: "foo bar"),
|
33
|
+
AnotherSearchableSubclassModel.create!(content: "baz")
|
32
34
|
]
|
33
35
|
|
34
36
|
expect(SuperclassModel.count).to eq(6)
|
@@ -51,7 +53,7 @@ describe "a pg_search_scope on an STI subclass" do
|
|
51
53
|
model do
|
52
54
|
include PgSearch
|
53
55
|
self.inheritance_column = 'custom_type'
|
54
|
-
pg_search_scope :search_content, :
|
56
|
+
pg_search_scope :search_content, against: :content
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
@@ -62,14 +64,14 @@ describe "a pg_search_scope on an STI subclass" do
|
|
62
64
|
|
63
65
|
it "returns only results for that subclass" do
|
64
66
|
included = [
|
65
|
-
SearchableSubclassModel.create!(:
|
67
|
+
SearchableSubclassModel.create!(content: "foo bar")
|
66
68
|
]
|
67
69
|
excluded = [
|
68
|
-
SearchableSubclassModel.create!(:
|
69
|
-
SuperclassModel.create!(:
|
70
|
-
SuperclassModel.create!(:
|
71
|
-
AnotherSearchableSubclassModel.create!(:
|
72
|
-
AnotherSearchableSubclassModel.create!(:
|
70
|
+
SearchableSubclassModel.create!(content: "baz"),
|
71
|
+
SuperclassModel.create!(content: "foo bar"),
|
72
|
+
SuperclassModel.create!(content: "baz"),
|
73
|
+
AnotherSearchableSubclassModel.create!(content: "foo bar"),
|
74
|
+
AnotherSearchableSubclassModel.create!(content: "baz")
|
73
75
|
]
|
74
76
|
|
75
77
|
expect(SuperclassModel.count).to eq(6)
|
@@ -82,4 +84,3 @@ describe "a pg_search_scope on an STI subclass" do
|
|
82
84
|
end
|
83
85
|
end
|
84
86
|
end
|
85
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe PgSearch::Configuration::Association do
|
@@ -16,11 +18,11 @@ describe PgSearch::Configuration::Association do
|
|
16
18
|
|
17
19
|
model do
|
18
20
|
include PgSearch
|
19
|
-
has_one :avatar, :
|
21
|
+
has_one :avatar, class_name: "Avatar"
|
20
22
|
belongs_to :site
|
21
23
|
|
22
|
-
pg_search_scope :with_avatar, :
|
23
|
-
pg_search_scope :with_site, :
|
24
|
+
pg_search_scope :with_avatar, associated_against: { avatar: :url }
|
25
|
+
pg_search_scope :with_site, associated_against: { site: :title }
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
@@ -31,9 +33,9 @@ describe PgSearch::Configuration::Association do
|
|
31
33
|
|
32
34
|
model do
|
33
35
|
include PgSearch
|
34
|
-
has_many :users, :
|
36
|
+
has_many :users, class_name: "User"
|
35
37
|
|
36
|
-
pg_search_scope :with_users, :
|
38
|
+
pg_search_scope :with_users, associated_against: { users: :name }
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe PgSearch::Configuration::ForeignColumn do
|
@@ -18,14 +20,14 @@ describe PgSearch::Configuration::ForeignColumn do
|
|
18
20
|
include PgSearch
|
19
21
|
belongs_to :another_model, class_name: 'AssociatedModel'
|
20
22
|
|
21
|
-
pg_search_scope :with_another, :
|
23
|
+
pg_search_scope :with_another, associated_against: { another_model: :title }
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
27
|
it "returns a consistent string" do
|
26
28
|
association = PgSearch::Configuration::Association.new(Model,
|
27
|
-
|
28
|
-
|
29
|
+
:another_model,
|
30
|
+
:title)
|
29
31
|
foreign_column = described_class.new("title", nil, Model, association)
|
30
32
|
|
31
33
|
column_alias = foreign_column.alias
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe PgSearch::Features::DMetaphone do
|
@@ -13,10 +15,10 @@ describe PgSearch::Features::DMetaphone do
|
|
13
15
|
query = "query"
|
14
16
|
columns = [
|
15
17
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
16
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
18
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
17
19
|
]
|
18
20
|
options = {}
|
19
|
-
config = double(:config, :
|
21
|
+
config = double(:config, ignore: [])
|
20
22
|
normalizer = PgSearch::Normalizer.new(config)
|
21
23
|
|
22
24
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -38,10 +40,10 @@ describe PgSearch::Features::DMetaphone do
|
|
38
40
|
query = "query"
|
39
41
|
columns = [
|
40
42
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
41
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
43
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
42
44
|
]
|
43
45
|
options = {}
|
44
|
-
config = double(:config, :
|
46
|
+
config = double(:config, ignore: [])
|
45
47
|
normalizer = PgSearch::Normalizer.new(config)
|
46
48
|
|
47
49
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'ostruct'
|
3
5
|
|
@@ -12,7 +14,7 @@ describe PgSearch::Features::Trigram do
|
|
12
14
|
]
|
13
15
|
}
|
14
16
|
let(:normalizer) { PgSearch::Normalizer.new(config) }
|
15
|
-
let(:config) { OpenStruct.new(:
|
17
|
+
let(:config) { OpenStruct.new(ignore: []) }
|
16
18
|
|
17
19
|
let(:coalesced_columns) do
|
18
20
|
<<-SQL.strip_heredoc.chomp
|
@@ -30,25 +32,50 @@ describe PgSearch::Features::Trigram do
|
|
30
32
|
describe 'conditions' do
|
31
33
|
it 'escapes the search document and query' do
|
32
34
|
config.ignore = []
|
33
|
-
expect(feature.conditions.to_sql).to eq("(
|
35
|
+
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_columns}))")
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'searching by word_similarity' do
|
39
|
+
let(:options) do
|
40
|
+
{ word_similarity: true }
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'uses the "<%" operator when searching by word_similarity' do
|
44
|
+
config.ignore = []
|
45
|
+
expect(feature.conditions.to_sql).to eq("('#{query}' <% (#{coalesced_columns}))")
|
46
|
+
end
|
34
47
|
end
|
35
48
|
|
36
49
|
context 'ignoring accents' do
|
37
50
|
it 'escapes the search document and query, but not the accent function' do
|
38
51
|
config.ignore = [:accents]
|
39
|
-
expect(feature.conditions.to_sql).to eq("(
|
52
|
+
expect(feature.conditions.to_sql).to eq("(unaccent('#{query}') % (unaccent(#{coalesced_columns})))")
|
40
53
|
end
|
41
54
|
end
|
42
55
|
|
43
56
|
context 'when a threshold is specified' do
|
44
|
-
|
45
|
-
|
57
|
+
context 'searching by similarity' do
|
58
|
+
let(:options) do
|
59
|
+
{ threshold: 0.5 }
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'uses a minimum similarity expression instead of the "%" operator' do
|
63
|
+
expect(feature.conditions.to_sql).to eq(
|
64
|
+
"(similarity('#{query}', (#{coalesced_columns})) >= 0.5)"
|
65
|
+
)
|
66
|
+
end
|
46
67
|
end
|
47
68
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
69
|
+
context 'searching by word_similarity' do
|
70
|
+
let(:options) do
|
71
|
+
{ threshold: 0.5, word_similarity: true }
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'uses a minimum similarity expression instead of the "<%" operator' do
|
75
|
+
expect(feature.conditions.to_sql).to eq(
|
76
|
+
"(word_similarity('#{query}', (#{coalesced_columns})) >= 0.5)"
|
77
|
+
)
|
78
|
+
end
|
52
79
|
end
|
53
80
|
end
|
54
81
|
|
@@ -59,7 +86,7 @@ describe PgSearch::Features::Trigram do
|
|
59
86
|
it 'only searches against the select column' do
|
60
87
|
options = { only: :name }
|
61
88
|
coalesced_column = "coalesce(#{Model.quoted_table_name}.\"name\"::text, '')"
|
62
|
-
expect(feature.conditions.to_sql).to eq("(
|
89
|
+
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_column}))")
|
63
90
|
end
|
64
91
|
end
|
65
92
|
context 'multiple columns' do
|
@@ -67,7 +94,7 @@ describe PgSearch::Features::Trigram do
|
|
67
94
|
|
68
95
|
it 'concatenates when multiples columns are selected' do
|
69
96
|
options = { only: %i[name content] }
|
70
|
-
expect(feature.conditions.to_sql).to eq("(
|
97
|
+
expect(feature.conditions.to_sql).to eq("('#{query}' % (#{coalesced_columns}))")
|
71
98
|
end
|
72
99
|
end
|
73
100
|
end
|
@@ -75,7 +102,7 @@ describe PgSearch::Features::Trigram do
|
|
75
102
|
|
76
103
|
describe '#rank' do
|
77
104
|
it 'returns an expression using the similarity() function' do
|
78
|
-
expect(feature.rank.to_sql).to eq("(similarity(
|
105
|
+
expect(feature.rank.to_sql).to eq("(similarity('#{query}', (#{coalesced_columns})))")
|
79
106
|
end
|
80
107
|
end
|
81
108
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
require "active_support/deprecation"
|
3
5
|
|
@@ -14,10 +16,10 @@ describe PgSearch::Features::TSearch do
|
|
14
16
|
query = "query"
|
15
17
|
columns = [
|
16
18
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
17
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
19
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
18
20
|
]
|
19
21
|
options = {}
|
20
|
-
config = double(:config, :
|
22
|
+
config = double(:config, ignore: [])
|
21
23
|
normalizer = PgSearch::Normalizer.new(config)
|
22
24
|
|
23
25
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -39,10 +41,10 @@ describe PgSearch::Features::TSearch do
|
|
39
41
|
query = "query"
|
40
42
|
columns = [
|
41
43
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
42
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
44
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
43
45
|
]
|
44
46
|
options = {}
|
45
|
-
config = double(:config, :
|
47
|
+
config = double(:config, ignore: [])
|
46
48
|
normalizer = PgSearch::Normalizer.new(config)
|
47
49
|
|
48
50
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -56,10 +58,10 @@ describe PgSearch::Features::TSearch do
|
|
56
58
|
query = "!query"
|
57
59
|
columns = [
|
58
60
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
59
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
61
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
60
62
|
]
|
61
|
-
options = {:
|
62
|
-
config = double(:config, :
|
63
|
+
options = { negation: true }
|
64
|
+
config = double(:config, ignore: [])
|
63
65
|
normalizer = PgSearch::Normalizer.new(config)
|
64
66
|
|
65
67
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -74,10 +76,10 @@ describe PgSearch::Features::TSearch do
|
|
74
76
|
query = "!query"
|
75
77
|
columns = [
|
76
78
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
77
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
79
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
78
80
|
]
|
79
|
-
options = {:
|
80
|
-
config = double(:config, :
|
81
|
+
options = { negation: false }
|
82
|
+
config = double(:config, ignore: [])
|
81
83
|
normalizer = PgSearch::Normalizer.new(config)
|
82
84
|
|
83
85
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -92,10 +94,10 @@ describe PgSearch::Features::TSearch do
|
|
92
94
|
query = "query"
|
93
95
|
columns = [
|
94
96
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
95
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
97
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
96
98
|
]
|
97
|
-
options = {tsvector_column: "my_tsvector"}
|
98
|
-
config = double(:config, :
|
99
|
+
options = { tsvector_column: "my_tsvector" }
|
100
|
+
config = double(:config, ignore: [])
|
99
101
|
normalizer = PgSearch::Normalizer.new(config)
|
100
102
|
|
101
103
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -110,10 +112,10 @@ describe PgSearch::Features::TSearch do
|
|
110
112
|
query = "query"
|
111
113
|
columns = [
|
112
114
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
113
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
115
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
114
116
|
]
|
115
|
-
options = {tsvector_column: ["tsvector1", "tsvector2"]}
|
116
|
-
config = double(:config, :
|
117
|
+
options = { tsvector_column: ["tsvector1", "tsvector2"] }
|
118
|
+
config = double(:config, ignore: [])
|
117
119
|
normalizer = PgSearch::Normalizer.new(config)
|
118
120
|
|
119
121
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -139,7 +141,7 @@ describe PgSearch::Features::TSearch do
|
|
139
141
|
]
|
140
142
|
options = {}
|
141
143
|
|
142
|
-
config = double(:config, :
|
144
|
+
config = double(:config, ignore: [])
|
143
145
|
normalizer = PgSearch::Normalizer.new(config)
|
144
146
|
|
145
147
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -153,7 +155,7 @@ describe PgSearch::Features::TSearch do
|
|
153
155
|
query = "query"
|
154
156
|
columns = [
|
155
157
|
PgSearch::Configuration::Column.new(:name, nil, Model),
|
156
|
-
PgSearch::Configuration::Column.new(:content, nil, Model)
|
158
|
+
PgSearch::Configuration::Column.new(:content, nil, Model)
|
157
159
|
]
|
158
160
|
options = {
|
159
161
|
dictionary: "spanish",
|
@@ -163,7 +165,7 @@ describe PgSearch::Features::TSearch do
|
|
163
165
|
}
|
164
166
|
}
|
165
167
|
|
166
|
-
config = double(:config, :
|
168
|
+
config = double(:config, ignore: [])
|
167
169
|
normalizer = PgSearch::Normalizer.new(config)
|
168
170
|
|
169
171
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -193,7 +195,7 @@ describe PgSearch::Features::TSearch do
|
|
193
195
|
}
|
194
196
|
}
|
195
197
|
|
196
|
-
config = double(:config, :
|
198
|
+
config = double(:config, ignore: [])
|
197
199
|
normalizer = PgSearch::Normalizer.new(config)
|
198
200
|
|
199
201
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -221,7 +223,7 @@ describe PgSearch::Features::TSearch do
|
|
221
223
|
}
|
222
224
|
}
|
223
225
|
|
224
|
-
config = double(:config, :
|
226
|
+
config = double(:config, ignore: [])
|
225
227
|
normalizer = PgSearch::Normalizer.new(config)
|
226
228
|
|
227
229
|
feature = described_class.new(query, options, columns, Model, normalizer)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
def has_microsecond_precision?
|
@@ -8,7 +10,7 @@ end
|
|
8
10
|
describe PgSearch::Multisearch::Rebuilder do
|
9
11
|
with_table "pg_search_documents", {}, &DOCUMENTS_SCHEMA
|
10
12
|
|
11
|
-
describe 'when
|
13
|
+
describe 'when initialized with a model that is not multisearchable' do
|
12
14
|
with_model :not_multisearchable
|
13
15
|
|
14
16
|
it 'raises an exception' do
|
@@ -78,7 +80,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
78
80
|
|
79
81
|
model do
|
80
82
|
include PgSearch
|
81
|
-
multisearchable :
|
83
|
+
multisearchable against: :name
|
82
84
|
end
|
83
85
|
end
|
84
86
|
|
@@ -112,7 +114,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
112
114
|
SELECT 'Model' AS searchable_type,
|
113
115
|
#{Model.quoted_table_name}.#{Model.primary_key} AS searchable_id,
|
114
116
|
(
|
115
|
-
coalesce(#{Model.quoted_table_name}.name::text, '')
|
117
|
+
coalesce(#{Model.quoted_table_name}."name"::text, '')
|
116
118
|
) AS content,
|
117
119
|
'#{expected_timestamp}' AS created_at,
|
118
120
|
'#{expected_timestamp}' AS updated_at
|
@@ -132,6 +134,25 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
132
134
|
expect(executed_sql.first.strip).to eq(expected_sql.strip)
|
133
135
|
end
|
134
136
|
|
137
|
+
context "for a model with a camel case column" do
|
138
|
+
with_model :ModelWithCamelCaseColumn do
|
139
|
+
table do |t|
|
140
|
+
t.string :camelName
|
141
|
+
end
|
142
|
+
|
143
|
+
model do
|
144
|
+
include PgSearch
|
145
|
+
multisearchable against: :name
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
it "creates search document without PG error" do
|
150
|
+
time = Time.utc(2001, 1, 1, 0, 0, 0)
|
151
|
+
rebuilder = PgSearch::Multisearch::Rebuilder.new(Model, -> { time })
|
152
|
+
rebuilder.rebuild
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
135
156
|
context "for a model with a non-standard primary key" do
|
136
157
|
with_model :ModelWithNonStandardPrimaryKey do
|
137
158
|
table primary_key: :non_standard_primary_key do |t|
|
@@ -140,7 +161,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
140
161
|
|
141
162
|
model do
|
142
163
|
include PgSearch
|
143
|
-
multisearchable :
|
164
|
+
multisearchable against: :name
|
144
165
|
end
|
145
166
|
end
|
146
167
|
|
@@ -157,7 +178,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
157
178
|
SELECT 'ModelWithNonStandardPrimaryKey' AS searchable_type,
|
158
179
|
#{ModelWithNonStandardPrimaryKey.quoted_table_name}.non_standard_primary_key AS searchable_id,
|
159
180
|
(
|
160
|
-
coalesce(#{ModelWithNonStandardPrimaryKey.quoted_table_name}.name::text, '')
|
181
|
+
coalesce(#{ModelWithNonStandardPrimaryKey.quoted_table_name}."name"::text, '')
|
161
182
|
) AS content,
|
162
183
|
'#{expected_timestamp}' AS created_at,
|
163
184
|
'#{expected_timestamp}' AS updated_at
|
@@ -226,13 +247,13 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
226
247
|
|
227
248
|
model do
|
228
249
|
include PgSearch
|
229
|
-
multisearchable :
|
250
|
+
multisearchable if: :active?
|
230
251
|
end
|
231
252
|
end
|
232
253
|
|
233
254
|
it "calls update_pg_search_document on each record" do
|
234
|
-
record_1 = Model.create!(:
|
235
|
-
record_2 = Model.create!(:
|
255
|
+
record_1 = Model.create!(active: true)
|
256
|
+
record_2 = Model.create!(active: false)
|
236
257
|
|
237
258
|
rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
|
238
259
|
|
@@ -262,13 +283,13 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
262
283
|
|
263
284
|
model do
|
264
285
|
include PgSearch
|
265
|
-
multisearchable :
|
286
|
+
multisearchable unless: :inactive?
|
266
287
|
end
|
267
288
|
end
|
268
289
|
|
269
290
|
it "calls update_pg_search_document on each record" do
|
270
|
-
record_1 = Model.create!(:
|
271
|
-
record_2 = Model.create!(:
|
291
|
+
record_1 = Model.create!(inactive: true)
|
292
|
+
record_2 = Model.create!(inactive: false)
|
272
293
|
|
273
294
|
rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
|
274
295
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe PgSearch::Multisearch do
|
@@ -20,7 +22,7 @@ describe PgSearch::Multisearch do
|
|
20
22
|
|
21
23
|
describe ".rebuild" do
|
22
24
|
before do
|
23
|
-
model.multisearchable :
|
25
|
+
model.multisearchable against: :title
|
24
26
|
end
|
25
27
|
|
26
28
|
it "should operate inside a transaction" do
|
@@ -96,8 +98,8 @@ describe PgSearch::Multisearch do
|
|
96
98
|
describe "inserting the new documents" do
|
97
99
|
let!(:new_models) { [] }
|
98
100
|
before do
|
99
|
-
new_models << model.create!(:
|
100
|
-
new_models << model.create!(:
|
101
|
+
new_models << model.create!(title: "Foo", content: "Bar")
|
102
|
+
new_models << model.create!(title: "Baz", content: "Bar")
|
101
103
|
end
|
102
104
|
|
103
105
|
it "should create new documents for the two models" do
|
@@ -112,7 +114,7 @@ describe PgSearch::Multisearch do
|
|
112
114
|
|
113
115
|
context "with one attribute" do
|
114
116
|
before do
|
115
|
-
model.multisearchable :
|
117
|
+
model.multisearchable against: [:title]
|
116
118
|
end
|
117
119
|
|
118
120
|
it "should generate the proper SQL code" do
|
@@ -121,7 +123,7 @@ describe PgSearch::Multisearch do
|
|
121
123
|
SELECT #{connection.quote(model.name)} AS searchable_type,
|
122
124
|
#{model.quoted_table_name}.id AS searchable_id,
|
123
125
|
(
|
124
|
-
coalesce(#{model.quoted_table_name}.title::text, '')
|
126
|
+
coalesce(#{model.quoted_table_name}."title"::text, '')
|
125
127
|
) AS content,
|
126
128
|
#{connection.quote(connection.quoted_date(now))} AS created_at,
|
127
129
|
#{connection.quote(connection.quoted_date(now))} AS updated_at
|
@@ -139,7 +141,7 @@ describe PgSearch::Multisearch do
|
|
139
141
|
|
140
142
|
context "with multiple attributes" do
|
141
143
|
before do
|
142
|
-
model.multisearchable :
|
144
|
+
model.multisearchable against: %i[title content]
|
143
145
|
end
|
144
146
|
|
145
147
|
it "should generate the proper SQL code" do
|
@@ -148,7 +150,7 @@ describe PgSearch::Multisearch do
|
|
148
150
|
SELECT #{connection.quote(model.name)} AS searchable_type,
|
149
151
|
#{model.quoted_table_name}.id AS searchable_id,
|
150
152
|
(
|
151
|
-
coalesce(#{model.quoted_table_name}.title::text, '') || ' ' || coalesce(#{model.quoted_table_name}.content::text, '')
|
153
|
+
coalesce(#{model.quoted_table_name}."title"::text, '') || ' ' || coalesce(#{model.quoted_table_name}."content"::text, '')
|
152
154
|
) AS content,
|
153
155
|
#{connection.quote(connection.quoted_date(now))} AS created_at,
|
154
156
|
#{connection.quote(connection.quoted_date(now))} AS updated_at
|