pg_search 1.0.3 → 1.0.4

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: 33f93dcf75cdfe31418fe81f7f4eb4a1f6bef1c7
4
- data.tar.gz: 7358d769fbba0629a3212550a2838e3478b748ec
3
+ metadata.gz: d88e1b1a697e2b7c60254e229f05ef8a7e7290fd
4
+ data.tar.gz: 237578e6b7961e9662d513f246878ad07e27d52e
5
5
  SHA512:
6
- metadata.gz: a60014add058e2749d7f13b4286016e9d1d30c503714e3c054d0ffb46937e04f63b6a2ac7b18e3510fb8cd6dd9d08786d93d8e4b8db1ebaab3b128e36ebd8614
7
- data.tar.gz: bb56ae42146f2c5b2c345f4e7ea07716292dddf7899c9c034bd1dec02c53ff2082111b9c87525d048eeddf3a872c0b18d5809edd5a0143d1a73528f62d8295bc
6
+ metadata.gz: 8a20e4bccc363bd4625b97d4533fedbb4a2fe22d0be7f28b4498456c7189a4ee34209e6e3f81109a2285680dc693a6cf17946ba301b3d67c7e7c6a4a4dac0e28
7
+ data.tar.gz: 0e119a3b707bbb3b55272763893eacf8d2857f4171733e114797a68d8d1d506c7b491b6e27eccb9994276f0615911947748c4553dafd9f4a56df0f50c4c9e963
@@ -1,5 +1,10 @@
1
1
  # pg_search changelog
2
2
 
3
+ ## 1.0.4
4
+
5
+ * Assert valid options for features. (Janko Marohnić)
6
+ * Enable chaining of pg_search scopes. (Nicolas Buduroi)
7
+
3
8
  ## 1.0.3
4
9
 
5
10
  * Support STI models using a custom inheritance column. (Nick Doiron)
@@ -1,13 +1,18 @@
1
1
  require "active_support/core_ext/module/delegation"
2
+ require "active_support/core_ext/hash/keys"
2
3
 
3
4
  module PgSearch
4
5
  module Features
5
6
  class Feature
7
+ def self.valid_options
8
+ [:only, :sort_only]
9
+ end
10
+
6
11
  delegate :connection, :quoted_table_name, :to => :'@model'
7
12
 
8
13
  def initialize(query, options, all_columns, model, normalizer)
9
14
  @query = query
10
- @options = options || {}
15
+ @options = (options || {}).assert_valid_keys(self.class.valid_options)
11
16
  @all_columns = all_columns
12
17
  @model = model
13
18
  @normalizer = normalizer
@@ -1,6 +1,9 @@
1
1
  module PgSearch
2
2
  module Features
3
3
  class Trigram < Feature
4
+ def self.valid_options
5
+ super + [:threshold]
6
+ end
4
7
 
5
8
  def conditions
6
9
  if options[:threshold]
@@ -4,6 +4,10 @@ require "active_support/core_ext/module/delegation"
4
4
  module PgSearch
5
5
  module Features
6
6
  class TSearch < Feature
7
+ def self.valid_options
8
+ super + [:dictionary, :prefix, :negation, :any_word, :normalization, :tsvector_column]
9
+ end
10
+
7
11
  def initialize(*args)
8
12
  super
9
13
 
@@ -13,9 +13,22 @@ module PgSearch
13
13
  end
14
14
 
15
15
  def apply(scope)
16
+ unless scope.instance_variable_get(:@pg_search_scope_applied_count)
17
+ scope = if ::ActiveRecord::VERSION::STRING < "4.0.0"
18
+ scope.scoped
19
+ else
20
+ scope.all.spawn
21
+ end
22
+ end
23
+
24
+ alias_id = scope.instance_variable_get(:@pg_search_scope_applied_count) || 0
25
+ scope.instance_variable_set(:@pg_search_scope_applied_count, alias_id + 1)
26
+
27
+ aka = pg_search_alias scope, alias_id
28
+
16
29
  scope
17
- .joins(rank_join)
18
- .order("pg_search.rank DESC, #{order_within_rank}")
30
+ .joins(rank_join(aka))
31
+ .order("#{aka}.rank DESC, #{order_within_rank}")
19
32
  .extend(DisableEagerLoading)
20
33
  .extend(WithPgSearchRank)
21
34
  end
@@ -31,7 +44,10 @@ module PgSearch
31
44
  def with_pg_search_rank
32
45
  scope = self
33
46
  scope = scope.select("*") unless scope.select_values.any?
34
- scope.select("pg_search.rank AS pg_search_rank")
47
+ arel_table = scope.instance_variable_get(:@table)
48
+ aka = "pg_search_#{arel_table.name}"
49
+
50
+ scope.select("#{aka}.rank AS pg_search_rank")
35
51
  end
36
52
  end
37
53
 
@@ -105,8 +121,15 @@ module PgSearch
105
121
  end
106
122
  end
107
123
 
108
- def rank_join
109
- "INNER JOIN (#{subquery.to_sql}) pg_search ON #{primary_key} = pg_search.pg_search_id"
124
+ def pg_search_alias(scope, n)
125
+ arel_table = scope.instance_variable_get(:@table)
126
+ prefix = "pg_search_#{arel_table.name}"
127
+
128
+ 0 == n ? prefix : "#{prefix}_#{n}"
129
+ end
130
+
131
+ def rank_join(aka)
132
+ "INNER JOIN (#{subquery.to_sql}) #{aka} ON #{primary_key} = #{aka}.pg_search_id"
110
133
  end
111
134
  end
112
135
  end
@@ -1,3 +1,3 @@
1
1
  module PgSearch
2
- VERSION = "1.0.3".freeze
2
+ VERSION = "1.0.4".freeze
3
3
  end
@@ -194,7 +194,9 @@ describe "an Active Record model which includes PgSearch" do
194
194
  end
195
195
 
196
196
  model do
197
+ include PgSearch
197
198
  belongs_to :person
199
+ pg_search_scope :search_city, against: [:city]
198
200
  end
199
201
  end
200
202
 
@@ -210,6 +212,9 @@ describe "an Active Record model which includes PgSearch" do
210
212
  scope :with_house_in_city, ->(city) {
211
213
  joins(:houses).where(House.table_name.to_sym => {city: city})
212
214
  }
215
+ scope :house_search_city, ->(query) {
216
+ joins(:houses).merge(House.search_city(query))
217
+ }
213
218
  end
214
219
  end
215
220
 
@@ -246,6 +251,26 @@ describe "an Active Record model which includes PgSearch" do
246
251
  expect(results).to include bob_in_duluth
247
252
  expect(results).not_to include [bob_in_sheboygan, sally_in_duluth]
248
253
  end
254
+
255
+ context "when chaining merged scopes" do
256
+ it "does not raise an exception" do
257
+ relation = Person.named('foo').house_search_city('bar')
258
+
259
+ expect { relation.to_a }.to_not raise_error
260
+ end
261
+ end
262
+ end
263
+
264
+ context "when chaining scopes" do
265
+ before do
266
+ ModelWithPgSearch.pg_search_scope :search_title, against: :title
267
+ end
268
+
269
+ it "does not raise an exception" do
270
+ relation = ModelWithPgSearch.search_content('foo').search_title('bar')
271
+
272
+ expect { relation.to_a }.to_not raise_error
273
+ end
249
274
  end
250
275
 
251
276
  it "returns an empty array when a blank query is passed in" do
@@ -307,7 +332,7 @@ describe "an Active Record model which includes PgSearch" do
307
332
  twice = ModelWithPgSearch.create!(:content => 'foo foo')
308
333
 
309
334
  records = ModelWithPgSearch.search_content('foo')
310
- .where("pg_search.rank > 0.07")
335
+ .where("pg_search_#{ModelWithPgSearch.table_name}.rank > 0.07")
311
336
 
312
337
  expect(records).to eq [twice]
313
338
  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.3
4
+ version: 1.0.4
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-11 00:00:00.000000000 Z
12
+ date: 2015-06-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord