pg_search 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +376 -307
- data/lib/pg_search/multisearch/rebuilder.rb +18 -12
- data/lib/pg_search/multisearchable.rb +4 -1
- data/lib/pg_search/version.rb +1 -1
- data/spec/integration/pg_search_spec.rb +43 -0
- data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +59 -3
- data/spec/spec_helper.rb +9 -9
- metadata +23 -23
@@ -28,20 +28,26 @@ module PgSearch
|
|
28
28
|
model.connection
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
def primary_key
|
32
|
+
model.primary_key
|
33
|
+
end
|
34
|
+
|
35
|
+
def rebuild_sql_template
|
36
|
+
<<-SQL.strip_heredoc
|
37
|
+
INSERT INTO :documents_table (searchable_type, searchable_id, content, created_at, updated_at)
|
38
|
+
SELECT :model_name AS searchable_type,
|
39
|
+
:model_table.#{primary_key} AS searchable_id,
|
40
|
+
(
|
41
|
+
:content_expressions
|
42
|
+
) AS content,
|
43
|
+
:current_time AS created_at,
|
44
|
+
:current_time AS updated_at
|
45
|
+
FROM :model_table
|
46
|
+
SQL
|
47
|
+
end
|
42
48
|
|
43
49
|
def rebuild_sql
|
44
|
-
replacements.inject(
|
50
|
+
replacements.inject(rebuild_sql_template) do |sql, key|
|
45
51
|
sql.gsub ":#{key}", send(key)
|
46
52
|
end
|
47
53
|
end
|
@@ -23,7 +23,10 @@ module PgSearch
|
|
23
23
|
unless_conditions.all? { |condition| !condition.to_proc.call(self) }
|
24
24
|
|
25
25
|
if should_have_document
|
26
|
-
|
26
|
+
unless pg_search_document.present?
|
27
|
+
build_pg_search_document.searchable_type = self.class.name
|
28
|
+
end
|
29
|
+
pg_search_document.save
|
27
30
|
else
|
28
31
|
pg_search_document.destroy if pg_search_document
|
29
32
|
end
|
data/lib/pg_search/version.rb
CHANGED
@@ -1052,6 +1052,49 @@ describe "an Active Record model which includes PgSearch" do
|
|
1052
1052
|
it { should_not include(soundalike_record) }
|
1053
1053
|
end
|
1054
1054
|
end
|
1055
|
+
|
1056
|
+
context "on an STI subclass" do
|
1057
|
+
with_model :ASuperclassModel do
|
1058
|
+
table do |t|
|
1059
|
+
t.text 'content'
|
1060
|
+
t.string 'type'
|
1061
|
+
end
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
before do
|
1065
|
+
|
1066
|
+
class SearchableSubclassModel < ASuperclassModel
|
1067
|
+
include PgSearch
|
1068
|
+
multisearchable :against => :content
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
class NonSearchableSubclassModel < ASuperclassModel
|
1072
|
+
end
|
1073
|
+
end
|
1074
|
+
|
1075
|
+
it "returns only results for that subclass" do
|
1076
|
+
included = [
|
1077
|
+
SearchableSubclassModel.create!(:content => "foo bar")
|
1078
|
+
]
|
1079
|
+
excluded = [
|
1080
|
+
SearchableSubclassModel.create!(:content => "baz"),
|
1081
|
+
ASuperclassModel.create!(:content => "foo bar"),
|
1082
|
+
ASuperclassModel.create!(:content => "baz"),
|
1083
|
+
NonSearchableSubclassModel.create!(:content => "foo bar"),
|
1084
|
+
NonSearchableSubclassModel.create!(:content => "baz")
|
1085
|
+
]
|
1086
|
+
|
1087
|
+
expect(ASuperclassModel.count).to be 6
|
1088
|
+
expect(SearchableSubclassModel.count).to be 2
|
1089
|
+
|
1090
|
+
results = PgSearch.multisearch("foo bar")
|
1091
|
+
|
1092
|
+
expect(results.map(&:searchable_id)).to include(*included.map(&:id))
|
1093
|
+
expect(results.map(&:searchable_id)).not_to include(*excluded.map(&:id))
|
1094
|
+
expect(results.map(&:searchable_type)).to include(*%w[SearchableSubclassModel])
|
1095
|
+
expect(results.map(&:searchable_type)).not_to include(*%w[ASuperclassModel NonSearchableSubclassModel])
|
1096
|
+
end
|
1097
|
+
end
|
1055
1098
|
end
|
1056
1099
|
|
1057
1100
|
describe ".disable_multisearch" do
|
@@ -99,11 +99,10 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
99
99
|
"2001-01-01 00:00:00"
|
100
100
|
end
|
101
101
|
|
102
|
-
|
103
102
|
expected_sql = <<-SQL.strip_heredoc
|
104
103
|
INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
|
105
104
|
SELECT 'Model' AS searchable_type,
|
106
|
-
#{Model.quoted_table_name}.
|
105
|
+
#{Model.quoted_table_name}.#{Model.primary_key} AS searchable_id,
|
107
106
|
(
|
108
107
|
coalesce(#{Model.quoted_table_name}.name::text, '')
|
109
108
|
) AS content,
|
@@ -115,7 +114,7 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
115
114
|
executed_sql = []
|
116
115
|
|
117
116
|
notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
|
118
|
-
executed_sql << payload[:sql]
|
117
|
+
executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
|
119
118
|
end
|
120
119
|
|
121
120
|
rebuilder.rebuild
|
@@ -124,6 +123,63 @@ describe PgSearch::Multisearch::Rebuilder do
|
|
124
123
|
executed_sql.length.should == 1
|
125
124
|
executed_sql.first.should == expected_sql
|
126
125
|
end
|
126
|
+
|
127
|
+
context "for a model with a non-standard primary key" do
|
128
|
+
with_model :ModelWithNonStandardPrimaryKey do
|
129
|
+
table primary_key: :non_standard_primary_key do |t|
|
130
|
+
t.string :name
|
131
|
+
end
|
132
|
+
|
133
|
+
model do
|
134
|
+
include PgSearch
|
135
|
+
multisearchable :against => :name
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
it "generates SQL with the correct primary key" do
|
140
|
+
time = DateTime.parse("2001-01-01")
|
141
|
+
rebuilder = PgSearch::Multisearch::Rebuilder.new(ModelWithNonStandardPrimaryKey, lambda { time } )
|
142
|
+
|
143
|
+
# Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
|
144
|
+
# https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
|
145
|
+
version_4_0_1_or_newer = (
|
146
|
+
(ActiveRecord::VERSION::MAJOR > 4) ||
|
147
|
+
(ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
|
148
|
+
(ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
|
149
|
+
)
|
150
|
+
|
151
|
+
expected_timestamp =
|
152
|
+
if version_4_0_1_or_newer
|
153
|
+
"2001-01-01 00:00:00.000000"
|
154
|
+
else
|
155
|
+
"2001-01-01 00:00:00"
|
156
|
+
end
|
157
|
+
|
158
|
+
expected_sql = <<-SQL.strip_heredoc
|
159
|
+
INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
|
160
|
+
SELECT 'ModelWithNonStandardPrimaryKey' AS searchable_type,
|
161
|
+
#{ModelWithNonStandardPrimaryKey.quoted_table_name}.non_standard_primary_key AS searchable_id,
|
162
|
+
(
|
163
|
+
coalesce(#{ModelWithNonStandardPrimaryKey.quoted_table_name}.name::text, '')
|
164
|
+
) AS content,
|
165
|
+
'#{expected_timestamp}' AS created_at,
|
166
|
+
'#{expected_timestamp}' AS updated_at
|
167
|
+
FROM #{ModelWithNonStandardPrimaryKey.quoted_table_name}
|
168
|
+
SQL
|
169
|
+
|
170
|
+
executed_sql = []
|
171
|
+
|
172
|
+
notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
|
173
|
+
executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
|
174
|
+
end
|
175
|
+
|
176
|
+
rebuilder.rebuild
|
177
|
+
ActiveSupport::Notifications.unsubscribe(notifier)
|
178
|
+
|
179
|
+
executed_sql.length.should == 1
|
180
|
+
executed_sql.first.should == expected_sql
|
181
|
+
end
|
182
|
+
end
|
127
183
|
end
|
128
184
|
|
129
185
|
context "when multisearchable is conditional" do
|
data/spec/spec_helper.rb
CHANGED
@@ -9,16 +9,16 @@ if ENV["TRAVIS"]
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
error_class = PGError
|
19
|
-
end
|
12
|
+
if defined? JRUBY_VERSION
|
13
|
+
require 'activerecord-jdbcpostgresql-adapter'
|
14
|
+
error_classes = [ActiveRecord::JDBCError]
|
15
|
+
else
|
16
|
+
require "pg"
|
17
|
+
error_classes = [PGError]
|
20
18
|
end
|
21
19
|
|
20
|
+
error_classes << ActiveRecord::NoDatabaseError if defined? ActiveRecord::NoDatabaseError
|
21
|
+
|
22
22
|
begin
|
23
23
|
database_user = if ENV["TRAVIS"]
|
24
24
|
"postgres"
|
@@ -33,7 +33,7 @@ begin
|
|
33
33
|
connection = ActiveRecord::Base.connection
|
34
34
|
postgresql_version = connection.send(:postgresql_version)
|
35
35
|
connection.execute("SELECT 1")
|
36
|
-
rescue
|
36
|
+
rescue *error_classes
|
37
37
|
at_exit do
|
38
38
|
puts "-" * 80
|
39
39
|
puts "Unable to connect to database. Please run:"
|
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: 0.7.
|
4
|
+
version: 0.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Grant Hutchins
|
@@ -9,104 +9,104 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '3.1'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '3.1'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: activesupport
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '3.1'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '3.1'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: arel
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
type: :runtime
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: rake
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: pry
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|
73
73
|
requirements:
|
74
|
-
- -
|
74
|
+
- - ">="
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
|
-
- -
|
81
|
+
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
85
|
name: rspec
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- -
|
88
|
+
- - ">="
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
version: '2.14'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
|
-
- -
|
95
|
+
- - ">="
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '2.14'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: with_model
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- -
|
102
|
+
- - ">="
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- -
|
109
|
+
- - ">="
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
description: PgSearch builds Active Record named scopes that take advantage of PostgreSQL's
|
@@ -118,10 +118,10 @@ executables: []
|
|
118
118
|
extensions: []
|
119
119
|
extra_rdoc_files: []
|
120
120
|
files:
|
121
|
-
- .autotest
|
122
|
-
- .gitignore
|
123
|
-
- .rspec
|
124
|
-
- .travis.yml
|
121
|
+
- ".autotest"
|
122
|
+
- ".gitignore"
|
123
|
+
- ".rspec"
|
124
|
+
- ".travis.yml"
|
125
125
|
- CHANGELOG.md
|
126
126
|
- CONTRIBUTING.md
|
127
127
|
- Gemfile
|
@@ -188,17 +188,17 @@ require_paths:
|
|
188
188
|
- lib
|
189
189
|
required_ruby_version: !ruby/object:Gem::Requirement
|
190
190
|
requirements:
|
191
|
-
- -
|
191
|
+
- - ">="
|
192
192
|
- !ruby/object:Gem::Version
|
193
193
|
version: 1.9.2
|
194
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
195
|
requirements:
|
196
|
-
- -
|
196
|
+
- - ">="
|
197
197
|
- !ruby/object:Gem::Version
|
198
198
|
version: '0'
|
199
199
|
requirements: []
|
200
200
|
rubyforge_project:
|
201
|
-
rubygems_version: 2.
|
201
|
+
rubygems_version: 2.2.2
|
202
202
|
signing_key:
|
203
203
|
specification_version: 4
|
204
204
|
summary: PgSearch builds Active Record named scopes that take advantage of PostgreSQL's
|