pg_search 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfa2efbbbc811da8b5c62690f0084c9ddd60615d
4
- data.tar.gz: 90e7078224516ca17fdc503b418f309fd72560ae
3
+ metadata.gz: 33f93dcf75cdfe31418fe81f7f4eb4a1f6bef1c7
4
+ data.tar.gz: 7358d769fbba0629a3212550a2838e3478b748ec
5
5
  SHA512:
6
- metadata.gz: 9c177eb231f004b44affdd5006ab5de2d75b13b4b4da0fbc0c4cb85b9d4394a1d96e26a27f28d23c4200638fc523e1dda1f414067a350c417f160ab0d2a96a2c
7
- data.tar.gz: 7e98e612c9d089f5bab6c19365485c6190ad0b6338f662bfeb8864516592618f7883508c019bb031c7472ff3b5793b51f35ec27aaafcee748b9b85a5006fffe0
6
+ metadata.gz: a60014add058e2749d7f13b4286016e9d1d30c503714e3c054d0ffb46937e04f63b6a2ac7b18e3510fb8cd6dd9d08786d93d8e4b8db1ebaab3b128e36ebd8614
7
+ data.tar.gz: bb56ae42146f2c5b2c345f4e7ea07716292dddf7899c9c034bd1dec02c53ff2082111b9c87525d048eeddf3a872c0b18d5809edd5a0143d1a73528f62d8295bc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # pg_search changelog
2
2
 
3
+ ## 1.0.3
4
+
5
+ * Support STI models using a custom inheritance column. (Nick Doiron)
6
+
3
7
  ## 1.0.2
4
8
 
5
9
  * Don’t use SQL to rebuild search documents when models are multisearchable against dynamic methods and not just columns. Iterate over each record with `find_each` instead.
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Gem Version](https://img.shields.io/gem/v/pg_search.svg?style=flat)](https://rubygems.org/gems/pg_search)
6
6
  [![Dependency Status](https://img.shields.io/gemnasium/Casecommons/pg_search.svg?style=flat)](https://gemnasium.com/Casecommons/pg_search)
7
7
  [![Inline docs](http://inch-ci.org/github/Casecommons/pg_search.svg?branch=master&style=flat)](http://inch-ci.org/github/Casecommons/pg_search)
8
- [![Join the chat at https://gitter.im/Casecommons/pg_search](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Casecommons/pg_search?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
8
+ [![Join the chat at https://gitter.im/Casecommons/pg_search](https://img.shields.io/badge/gitter-join%20chat-blue.svg)](https://gitter.im/Casecommons/pg_search?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
9
9
 
10
10
  ## DESCRIPTION
11
11
 
@@ -63,12 +63,12 @@ module PgSearch
63
63
 
64
64
  def sti_clause
65
65
  clause = ""
66
- if model.column_names.include? 'type'
66
+ if model.column_names.include? model.inheritance_column
67
67
  clause = "WHERE"
68
68
  if model.base_class == model
69
- clause = "#{clause} type IS NULL OR"
69
+ clause = "#{clause} #{model.inheritance_column} IS NULL OR"
70
70
  end
71
- clause = "#{clause} type = #{model_name}"
71
+ clause = "#{clause} #{model.inheritance_column} = #{model_name}"
72
72
  end
73
73
  clause
74
74
  end
@@ -1,3 +1,3 @@
1
1
  module PgSearch
2
- VERSION = "1.0.2".freeze
2
+ VERSION = "1.0.3".freeze
3
3
  end
@@ -1182,50 +1182,6 @@ describe "an Active Record model which includes PgSearch" do
1182
1182
  end
1183
1183
  end
1184
1184
 
1185
- context "on an STI subclass" do
1186
- with_model :SuperclassModel do
1187
- table do |t|
1188
- t.text 'content'
1189
- t.string 'type'
1190
- end
1191
-
1192
- model do
1193
- include PgSearch
1194
- end
1195
- end
1196
-
1197
- before do
1198
- SuperclassModel.pg_search_scope :search_content, :against => :content
1199
-
1200
- class SearchableSubclassModel < SuperclassModel
1201
- end
1202
-
1203
- class AnotherSearchableSubclassModel < SuperclassModel
1204
- end
1205
- end
1206
-
1207
- it "returns only results for that subclass" do
1208
- included = [
1209
- SearchableSubclassModel.create!(:content => "foo bar")
1210
- ]
1211
- excluded = [
1212
- SearchableSubclassModel.create!(:content => "baz"),
1213
- SuperclassModel.create!(:content => "foo bar"),
1214
- SuperclassModel.create!(:content => "baz"),
1215
- AnotherSearchableSubclassModel.create!(:content => "foo bar"),
1216
- AnotherSearchableSubclassModel.create!(:content => "baz")
1217
- ]
1218
-
1219
- expect(SuperclassModel.count).to eq(6)
1220
- expect(SearchableSubclassModel.count).to eq(2)
1221
-
1222
- results = SearchableSubclassModel.search_content("foo bar")
1223
-
1224
- expect(results).to include(*included)
1225
- expect(results).not_to include(*excluded)
1226
- end
1227
- end
1228
-
1229
1185
  context "when there is a sort only feature" do
1230
1186
  it "excludes that feature from the conditions, but uses it in the sorting" do
1231
1187
  ModelWithPgSearch.pg_search_scope :search_content_ranked_by_dmetaphone,
@@ -0,0 +1,85 @@
1
+ require "spec_helper"
2
+
3
+ describe "a pg_search_scope on an STI subclass" do
4
+ context "with the standard type column" do
5
+ with_model :SuperclassModel do
6
+ table do |t|
7
+ t.text 'content'
8
+ t.string 'type'
9
+ end
10
+
11
+ model do
12
+ include PgSearch
13
+ pg_search_scope :search_content, :against => :content
14
+ end
15
+ end
16
+
17
+ before do
18
+ stub_const("SearchableSubclassModel", Class.new(SuperclassModel))
19
+ stub_const("AnotherSearchableSubclassModel", Class.new(SuperclassModel))
20
+ end
21
+
22
+ it "returns only results for that subclass" do
23
+ included = [
24
+ SearchableSubclassModel.create!(:content => "foo bar")
25
+ ]
26
+ excluded = [
27
+ SearchableSubclassModel.create!(:content => "baz"),
28
+ SuperclassModel.create!(:content => "foo bar"),
29
+ SuperclassModel.create!(:content => "baz"),
30
+ AnotherSearchableSubclassModel.create!(:content => "foo bar"),
31
+ AnotherSearchableSubclassModel.create!(:content => "baz")
32
+ ]
33
+
34
+ expect(SuperclassModel.count).to eq(6)
35
+ expect(SearchableSubclassModel.count).to eq(2)
36
+
37
+ results = SearchableSubclassModel.search_content("foo bar")
38
+
39
+ expect(results).to include(*included)
40
+ expect(results).not_to include(*excluded)
41
+ end
42
+ end
43
+
44
+ context "with a custom type column" do
45
+ with_model :SuperclassModel do
46
+ table do |t|
47
+ t.text 'content'
48
+ t.string 'custom_type'
49
+ end
50
+
51
+ model do
52
+ include PgSearch
53
+ self.inheritance_column = 'custom_type'
54
+ pg_search_scope :search_content, :against => :content
55
+ end
56
+ end
57
+
58
+ before do
59
+ stub_const("SearchableSubclassModel", Class.new(SuperclassModel))
60
+ stub_const("AnotherSearchableSubclassModel", Class.new(SuperclassModel))
61
+ end
62
+
63
+ it "returns only results for that subclass" do
64
+ included = [
65
+ SearchableSubclassModel.create!(:content => "foo bar")
66
+ ]
67
+ excluded = [
68
+ SearchableSubclassModel.create!(:content => "baz"),
69
+ SuperclassModel.create!(:content => "foo bar"),
70
+ SuperclassModel.create!(:content => "baz"),
71
+ AnotherSearchableSubclassModel.create!(:content => "foo bar"),
72
+ AnotherSearchableSubclassModel.create!(:content => "baz")
73
+ ]
74
+
75
+ expect(SuperclassModel.count).to eq(6)
76
+ expect(SearchableSubclassModel.count).to eq(2)
77
+
78
+ results = SearchableSubclassModel.search_content("foo bar")
79
+
80
+ expect(results).to include(*included)
81
+ expect(results).not_to include(*excluded)
82
+ end
83
+ end
84
+ end
85
+
@@ -75,91 +75,140 @@ describe PgSearch do
75
75
  end
76
76
 
77
77
  context "on an STI subclass" do
78
- with_model :SuperclassModel do
79
- table do |t|
80
- t.text 'content'
81
- t.string 'type'
78
+ context "with standard type column" do
79
+ with_model :SuperclassModel do
80
+ table do |t|
81
+ t.text 'content'
82
+ t.string 'type'
83
+ end
82
84
  end
83
- end
84
85
 
85
- before do
86
- searchable_subclass_model = Class.new(SuperclassModel) do
87
- include PgSearch
88
- multisearchable :against => :content
86
+ before do
87
+ searchable_subclass_model = Class.new(SuperclassModel) do
88
+ include PgSearch
89
+ multisearchable :against => :content
90
+ end
91
+ stub_const("SearchableSubclassModel", searchable_subclass_model)
92
+ stub_const("AnotherSearchableSubclassModel", searchable_subclass_model)
93
+ stub_const("NonSearchableSubclassModel", Class.new(SuperclassModel))
89
94
  end
90
- stub_const("SearchableSubclassModel", searchable_subclass_model)
91
- stub_const("AnotherSearchableSubclassModel", searchable_subclass_model)
92
- stub_const("NonSearchableSubclassModel", Class.new(SuperclassModel))
93
- end
94
95
 
95
- it "returns only results for that subclass" do
96
- included = [
97
- SearchableSubclassModel.create!(:content => "foo bar")
98
- ]
99
- excluded = [
100
- SearchableSubclassModel.create!(:content => "baz"),
101
- SuperclassModel.create!(:content => "foo bar"),
102
- SuperclassModel.create!(:content => "baz"),
103
- NonSearchableSubclassModel.create!(:content => "foo bar"),
96
+ it "returns only results for that subclass" do
97
+ included = [
98
+ SearchableSubclassModel.create!(:content => "foo bar")
99
+ ]
100
+ excluded = [
101
+ SearchableSubclassModel.create!(:content => "baz"),
102
+ SuperclassModel.create!(:content => "foo bar"),
103
+ SuperclassModel.create!(:content => "baz"),
104
+ NonSearchableSubclassModel.create!(:content => "foo bar"),
105
+ NonSearchableSubclassModel.create!(:content => "baz")
106
+ ]
107
+
108
+ expect(SuperclassModel.count).to be 6
109
+ expect(SearchableSubclassModel.count).to be 2
110
+
111
+ expect(PgSearch::Document.count).to be 2
112
+
113
+ results = PgSearch.multisearch("foo bar")
114
+
115
+ expect(results.length).to be 1
116
+ expect(results.first.searchable.class).to be SearchableSubclassModel
117
+ expect(results.first.searchable).to eq included.first
118
+ end
119
+
120
+ it "updates an existing STI model does not create a new pg_search document" do
121
+ model = SearchableSubclassModel.create!(:content => "foo bar")
122
+ expect(SearchableSubclassModel.count).to eq(1)
123
+ # We fetch the model from the database again otherwise
124
+ # the pg_search_document from the cache is used.
125
+ model = SearchableSubclassModel.find(model.id)
126
+ model.content = "foo"
127
+ model.save!
128
+ results = PgSearch.multisearch("foo")
129
+ expect(results.size).to eq(SearchableSubclassModel.count)
130
+ end
131
+
132
+ it "reindexing works" do
133
+ NonSearchableSubclassModel.create!(:content => "foo bar")
104
134
  NonSearchableSubclassModel.create!(:content => "baz")
105
- ]
135
+ expected = SearchableSubclassModel.create!(:content => "baz")
136
+ SuperclassModel.create!(:content => "foo bar")
137
+ SuperclassModel.create!(:content => "baz")
138
+ SuperclassModel.create!(:content => "baz2")
106
139
 
107
- expect(SuperclassModel.count).to be 6
108
- expect(SearchableSubclassModel.count).to be 2
140
+ expect(SuperclassModel.count).to be 6
141
+ expect(NonSearchableSubclassModel.count).to be 2
142
+ expect(SearchableSubclassModel.count).to be 1
109
143
 
110
- expect(PgSearch::Document.count).to be 2
144
+ expect(PgSearch::Document.count).to be 1
111
145
 
112
- results = PgSearch.multisearch("foo bar")
146
+ PgSearch::Multisearch.rebuild(SearchableSubclassModel)
113
147
 
114
- expect(results.length).to be 1
115
- expect(results.first.searchable.class).to be SearchableSubclassModel
116
- expect(results.first.searchable).to eq included.first
117
- end
148
+ expect(PgSearch::Document.count).to be 1
149
+ expect(PgSearch::Document.first.searchable.class).to be SearchableSubclassModel
150
+ expect(PgSearch::Document.first.searchable).to eq expected
151
+ end
152
+
153
+ it "reindexing searchable STI doesn't clobber other related STI models" do
154
+ searchable_s = SearchableSubclassModel.create!(:content => "baz")
155
+ searchable_a = AnotherSearchableSubclassModel.create!(:content => "baz")
118
156
 
119
- it "updates an existing STI model does not create a new pg_search document" do
120
- model = SearchableSubclassModel.create!(:content => "foo bar")
121
- expect(SearchableSubclassModel.count).to eq(1)
122
- # We fetch the model from the database again otherwise
123
- # the pg_search_document from the cache is used.
124
- model = SearchableSubclassModel.find(model.id)
125
- model.content = "foo"
126
- model.save!
127
- results = PgSearch.multisearch("foo")
128
- expect(results.size).to eq(SearchableSubclassModel.count)
157
+ expect(PgSearch::Document.count).to be 2
158
+ PgSearch::Multisearch.rebuild(SearchableSubclassModel)
159
+ expect(PgSearch::Document.count).to be 2
160
+
161
+ classes = PgSearch::Document.all.collect {|d| d.searchable.class }
162
+ expect(classes).to include SearchableSubclassModel
163
+ expect(classes).to include AnotherSearchableSubclassModel
164
+ end
129
165
  end
130
166
 
131
- it "reindexing works" do
132
- NonSearchableSubclassModel.create!(:content => "foo bar")
133
- NonSearchableSubclassModel.create!(:content => "baz")
134
- expected = SearchableSubclassModel.create!(:content => "baz")
135
- SuperclassModel.create!(:content => "foo bar")
136
- SuperclassModel.create!(:content => "baz")
137
- SuperclassModel.create!(:content => "baz2")
167
+ context "with custom type column" do
168
+ with_model :SuperclassModel do
169
+ table do |t|
170
+ t.text 'content'
171
+ t.string 'inherit'
172
+ end
138
173
 
139
- expect(SuperclassModel.count).to be 6
140
- expect(NonSearchableSubclassModel.count).to be 2
141
- expect(SearchableSubclassModel.count).to be 1
174
+ model do
175
+ self.inheritance_column = 'inherit'
176
+ end
177
+ end
142
178
 
143
- expect(PgSearch::Document.count).to be 1
179
+ before do
180
+ searchable_subclass_model = Class.new(SuperclassModel) do
181
+ include PgSearch
182
+ multisearchable :against => :content
183
+ end
184
+ stub_const("SearchableSubclassModel", searchable_subclass_model)
185
+ stub_const("AnotherSearchableSubclassModel", searchable_subclass_model)
186
+ stub_const("NonSearchableSubclassModel", Class.new(SuperclassModel))
187
+ end
144
188
 
145
- PgSearch::Multisearch.rebuild(SearchableSubclassModel)
189
+ it "returns only results for that subclass" do
190
+ included = [
191
+ SearchableSubclassModel.create!(:content => "foo bar")
192
+ ]
193
+ excluded = [
194
+ SearchableSubclassModel.create!(:content => "baz"),
195
+ SuperclassModel.create!(:content => "foo bar"),
196
+ SuperclassModel.create!(:content => "baz"),
197
+ NonSearchableSubclassModel.create!(:content => "foo bar"),
198
+ NonSearchableSubclassModel.create!(:content => "baz")
199
+ ]
146
200
 
147
- expect(PgSearch::Document.count).to be 1
148
- expect(PgSearch::Document.first.searchable.class).to be SearchableSubclassModel
149
- expect(PgSearch::Document.first.searchable).to eq expected
150
- end
201
+ expect(SuperclassModel.count).to be 6
202
+ expect(SearchableSubclassModel.count).to be 2
151
203
 
152
- it "reindexing searchable STI doesn't clobber other related STI models" do
153
- searchable_s = SearchableSubclassModel.create!(:content => "baz")
154
- searchable_a = AnotherSearchableSubclassModel.create!(:content => "baz")
204
+ expect(PgSearch::Document.count).to be 2
155
205
 
156
- expect(PgSearch::Document.count).to be 2
157
- PgSearch::Multisearch.rebuild(SearchableSubclassModel)
158
- expect(PgSearch::Document.count).to be 2
206
+ results = PgSearch.multisearch("foo bar")
159
207
 
160
- classes = PgSearch::Document.all.collect {|d| d.searchable.class }
161
- expect(classes).to include SearchableSubclassModel
162
- expect(classes).to include AnotherSearchableSubclassModel
208
+ expect(results.length).to be 1
209
+ expect(results.first.searchable.class).to be SearchableSubclassModel
210
+ expect(results.first.searchable).to eq included.first
211
+ end
163
212
  end
164
213
  end
165
214
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Hutchins
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-04 00:00:00.000000000 Z
12
+ date: 2015-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -178,6 +178,7 @@ files:
178
178
  - spec/integration/associations_spec.rb
179
179
  - spec/integration/pagination_spec.rb
180
180
  - spec/integration/pg_search_spec.rb
181
+ - spec/integration/single_table_inheritance_spec.rb
181
182
  - spec/lib/pg_search/configuration/association_spec.rb
182
183
  - spec/lib/pg_search/configuration/column_spec.rb
183
184
  - spec/lib/pg_search/configuration/foreign_column_spec.rb
@@ -228,6 +229,7 @@ test_files:
228
229
  - spec/integration/associations_spec.rb
229
230
  - spec/integration/pagination_spec.rb
230
231
  - spec/integration/pg_search_spec.rb
232
+ - spec/integration/single_table_inheritance_spec.rb
231
233
  - spec/lib/pg_search/configuration/association_spec.rb
232
234
  - spec/lib/pg_search/configuration/column_spec.rb
233
235
  - spec/lib/pg_search/configuration/foreign_column_spec.rb