pg_search 1.0.6 → 2.0.0

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: 55ed2cdc0eaecdb0dcb05ee5fcf7e7460556669d
4
- data.tar.gz: 8ff17add1a0a7c7a16dd6501b6335e5d364b07b4
3
+ metadata.gz: a541ca99e3c5e843e83463598bcde7b5feb486c3
4
+ data.tar.gz: 1160f97900fbe7db8d4d0a1b1198340a3c0ccc99
5
5
  SHA512:
6
- metadata.gz: 1acf5bc208de87a781c1c2ddaa3b44b33609afb8927cd8bbbb256a9a9caf099f3c2507232a0d9f2a9ad45acaf997e3590bbcbac6d3cb4936515e6874c3cb4ea1
7
- data.tar.gz: d569da3e3f8aa7b205596fbce5d1bd72e0db4dab9576ac9331b810b4dbcefd80f77918a8ce51cfb3e944f63520f4166e86bd5db883b01c0e2b8f97e3bff7018d
6
+ metadata.gz: a874920bde65876f83b3a7522bdc2ff0e11debc4852d9cebd186cb791c5ed3b6400dd0c891b7cd4cf3970251c06af4543cfe9e68503a423585e81247b6938857
7
+ data.tar.gz: d332008d1cc19b96004451112d9d7923d2af4a6fe1af6d5cfbd4535e1b8d1a80d9d20b3c977d2f409572ade279c17b4ae8697a9abc3ecc6e9738cdf87f433ae2
data/.codeclimate.yml CHANGED
@@ -1,2 +1,16 @@
1
- languages:
2
- Ruby: true
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ fixme:
9
+ enabled: true
10
+ rubocop:
11
+ enabled: true
12
+ ratings:
13
+ paths:
14
+ - "**.rb"
15
+ exclude_paths:
16
+ - "spec/"
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ .idea/
2
3
  .rbx
3
4
  .yardoc
4
5
  /bin/
data/.rubocop.yml CHANGED
@@ -23,7 +23,25 @@ Style/AlignParameters:
23
23
  Style/HashSyntax:
24
24
  Enabled: false
25
25
 
26
+ Style/NumericPredicate:
27
+ Enabled: false
28
+
26
29
  Style/PercentLiteralDelimiters:
27
30
  PreferredDelimiters:
28
31
  '%w': '[]'
29
32
  '%W': '[]'
33
+
34
+ Style/GuardClause:
35
+ Enabled: false
36
+
37
+ Style/VariableNumber:
38
+ EnforcedStyle: snake_case
39
+
40
+ Bundler/OrderedGems:
41
+ Enabled: false
42
+
43
+ Bundler/DuplicatedGem:
44
+ Enabled: false
45
+
46
+ Style/EmptyMethod:
47
+ EnforcedStyle: expanded
data/.travis.yml CHANGED
@@ -2,42 +2,36 @@ language: ruby
2
2
  sudo: false
3
3
 
4
4
  rvm:
5
- - "2.3.1"
6
- - "2.2"
7
- - "2.1"
8
- - "2.0"
9
- - "1.9"
10
- - jruby-19mode
11
- - jruby-9.0.0.0
5
+ - "2.3.3"
6
+ - "2.2.6"
7
+ - "2.1.10"
8
+ - jruby-9.1.6.0
12
9
 
13
10
  env:
14
11
  - ACTIVE_RECORD_BRANCH="master"
12
+ - ACTIVE_RECORD_BRANCH="5-0-stable"
15
13
  - ACTIVE_RECORD_BRANCH="4-2-stable"
16
- - ACTIVE_RECORD_BRANCH="4-1-stable"
17
- - ACTIVE_RECORD_BRANCH="4-0-stable"
14
+ - ACTIVE_RECORD_VERSION="~> 5.0.0"
18
15
  - ACTIVE_RECORD_VERSION="~> 4.2.0"
19
- - ACTIVE_RECORD_VERSION="~> 4.1.0"
20
- - ACTIVE_RECORD_VERSION="~> 4.0.0"
21
- - ACTIVE_RECORD_VERSION="~> 3.2.0"
22
- - ACTIVE_RECORD_VERSION="~> 3.1.0"
23
16
 
24
17
  matrix:
25
18
  allow_failures:
26
19
  - env: ACTIVE_RECORD_BRANCH="master"
20
+ - env: ACTIVE_RECORD_BRANCH="5-0-stable"
27
21
  - env: ACTIVE_RECORD_BRANCH="4-2-stable"
28
- - env: ACTIVE_RECORD_BRANCH="4-1-stable"
29
- - env: ACTIVE_RECORD_BRANCH="4-0-stable"
30
22
  exclude:
31
- - rvm: "1.9"
23
+ - rvm: "2.1.10"
32
24
  env: ACTIVE_RECORD_BRANCH="master"
33
- - rvm: "2.0"
34
- env: ACTIVE_RECORD_BRANCH="master"
35
- - rvm: "2.1"
36
- env: ACTIVE_RECORD_BRANCH="master"
37
- - rvm: jruby-19mode
38
- env: ACTIVE_RECORD_BRANCH="master"
39
- - rvm: jruby-9.0.0.0
25
+ - rvm: "2.1.10"
26
+ env: ACTIVE_RECORD_BRANCH="5-0-stable"
27
+ - rvm: "2.1.10"
28
+ env: ACTIVE_RECORD_VERSION="~> 5.0.0"
29
+ - rvm: jruby-9.1.6.0
40
30
  env: ACTIVE_RECORD_BRANCH="master"
31
+ - rvm: jruby-9.1.6.0
32
+ env: ACTIVE_RECORD_BRANCH="5-0-stable"
33
+ - rvm: jruby-9.1.6.0
34
+ env: ACTIVE_RECORD_VERSION="~> 5.0.0"
41
35
 
42
36
  before_script:
43
37
  - "psql -c 'create database pg_search_test;' -U postgres >/dev/null"
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # pg_search changelog
2
2
 
3
+ ## 2.0.0
4
+ * Drop support for PostgreSQL < 9.2.
5
+ * Drop support for Active Record < 4.2.
6
+ * Drop support for Ruby < 2.2.
7
+ * Improve performance of has_one and belongs_to associations. (Peter Postma)
8
+
3
9
  ## 1.0.6
10
+
4
11
  * Add support for highlighting the matching portion of a search result. (Jose Galisteo)
5
12
  * Add `:update_if` option to control when PgSearch::Document gets updated. (Adam Becker)
6
13
  * Add `:additional_attributes` option for adding additional attributes to PgSearch::Document
data/Gemfile CHANGED
@@ -11,7 +11,3 @@ if ENV['ACTIVE_RECORD_BRANCH']
11
11
  end
12
12
 
13
13
  gem 'activerecord', ENV['ACTIVE_RECORD_VERSION'] if ENV['ACTIVE_RECORD_VERSION']
14
-
15
- group :test do
16
- gem "codeclimate-test-reporter", require: nil
17
- end
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010–2015 Case Commons, LLC
1
+ Copyright (c) 2010-2016 Case Commons, Inc. <http://casecommons.org>
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -18,54 +18,24 @@ Read the blog post introducing PgSearch at http://blog.pivotal.io/labs/labs/pg-s
18
18
 
19
19
  ## REQUIREMENTS
20
20
 
21
- * Ruby 1.9.2, 2.0, or later
22
- * Active Record 3.1, 3.2, 4.0 or later
23
- * PostgreSQL
24
- * [PostgreSQL contrib packages for certain
25
- features](https://github.com/Casecommons/pg_search/wiki/Installing-Postgres-Contrib-Modules)
26
-
21
+ * Ruby 2.1+
22
+ * ActiveRecord 4.2+
23
+ * PostgreSQL 9.2+
24
+ * [PostgreSQL extensions](https://github.com/Casecommons/pg_search/wiki/Installing-PostgreSQL-Extensions) for certain features
27
25
 
28
26
  ## INSTALL
29
27
 
30
- ```bash
28
+ ```
31
29
  $ gem install pg_search
32
30
  ```
33
31
 
34
- ### Rails 3.1, 3.2, 4.0 or later, Ruby 1.9.2, 2.0, or later
35
-
36
- In Gemfile
32
+ or add this line to your Gemfile:
37
33
 
38
34
  ```ruby
39
35
  gem 'pg_search'
40
36
  ```
41
37
 
42
- ### Rails 3.0
43
-
44
- The newest versions of PgSearch no longer support Rails 3.0. However, the 0.5
45
- series still works. It's not actively maintained, but submissions are welcome
46
- for backports and bugfixes.
47
-
48
- ```ruby
49
- gem 'pg_search', "~> 0.5.7"
50
- ```
51
-
52
- The 0.5 branch lives at
53
- https://github.com/Casecommons/pg_search/tree/0.5-stable
54
-
55
- ### Rails 2
56
-
57
- The newest versions of PgSearch no longer support Rails 2. However, the 0.2
58
- series still works. It's not actively maintained, but submissions are welcome
59
- for backports and bugfixes.
60
-
61
- ```ruby
62
- gem 'pg_search', "~> 0.2.0"
63
- ```
64
-
65
- The 0.2 branch lives at
66
- https://github.com/Casecommons/pg_search/tree/0.2-stable
67
-
68
- ### Other Active Record projects
38
+ ### Non-Rails projects
69
39
 
70
40
  In addition to installing and requiring the gem, you may want to include the
71
41
  PgSearch rake tasks in your Rakefile. This isn't necessary for Rails projects,
@@ -75,19 +45,6 @@ which gain the Rake tasks via a Railtie.
75
45
  load "pg_search/tasks.rb"
76
46
  ```
77
47
 
78
- ### Ruby 1.8.7 or earlier
79
-
80
- The newest versions of PgSearch no longer support Ruby 1.8.7. However, the 0.6
81
- series still works. It's not actively maintained, but submissions are welcome
82
- for backports and bugfixes.
83
-
84
- ```ruby
85
- gem 'pg_search', "~> 0.6.4"
86
- ```
87
-
88
- The 0.6 branch lives at
89
- https://github.com/Casecommons/pg_search/tree/0.6-stable
90
-
91
48
  ## USAGE
92
49
 
93
50
  To add PgSearch to an Active Record model, simply include the PgSearch module.
@@ -287,7 +244,17 @@ To regenerate the documents for a given class, run:
287
244
  PgSearch::Multisearch.rebuild(Product)
288
245
  ```
289
246
 
290
- This is also available as a Rake task, for convenience.
247
+ The ```rebuild``` method will delete all the documents for the given class
248
+ before regenerating them. In some situations this may not be desirable,
249
+ such as when you're using single-table inheritance and ```searchable_type```
250
+ is your base class. You can prevent ```rebuild``` from deleting your records
251
+ like so:
252
+
253
+ ```ruby
254
+ PgSearch::Multisearch.rebuild(Product, false)
255
+ ```
256
+
257
+ Rebuild is also available as a Rake task, for convenience.
291
258
 
292
259
  $ rake pg_search:multisearch:rebuild[BlogPost]
293
260
 
@@ -439,15 +406,6 @@ It is possible to search columns on associated models. Note that if you do
439
406
  this, it will be impossible to speed up searches with database indexes.
440
407
  However, it is supported as a quick way to try out cross-model searching.
441
408
 
442
- In PostgreSQL 8.3 and earlier, you must install a utility function into your
443
- database. To generate and run a migration for this, run:
444
-
445
- $ rails g pg_search:migration:associated_against
446
- $ rake db:migrate
447
-
448
- This migration is safe to run against newer versions of PostgreSQL as well. It
449
- will essentially do nothing.
450
-
451
409
  You can pass a Hash into the :associated_against option to set up searching
452
410
  through associations. The keys are the names of the associations and the value
453
411
  works just like an :against option for the other model. Right now, searching
@@ -505,11 +463,10 @@ end
505
463
 
506
464
  The currently implemented features are
507
465
 
508
- * :tsearch - [Full text search](http://www.postgresql.org/docs/current/static/textsearch-intro.html)
509
- (built-in with 8.3 and later, available as a contrib package for some earlier versions)
466
+ * :tsearch - [Full text search](http://www.postgresql.org/docs/current/static/textsearch-intro.html), which is built-in to PostgreSQL
510
467
  * :trigram - [Trigram search](http://www.postgresql.org/docs/current/static/pgtrgm.html), which
511
- requires the trigram contrib package
512
- * :dmetaphone - [Double Metaphone search](http://www.postgresql.org/docs/9.0/static/fuzzystrmatch.html#AEN124771), which requires the fuzzystrmatch contrib package
468
+ requires the trigram extension
469
+ * :dmetaphone - [Double Metaphone search](http://www.postgresql.org/docs/current/static/fuzzystrmatch.html#AEN177521), which requires the fuzzystrmatch extension
513
470
 
514
471
 
515
472
  #### :tsearch (Full Text Search)
@@ -736,7 +693,7 @@ one_close = Person.create!(:name => 'leigh heinz')
736
693
  Person.search('ash hines') # => [exact, one_exact_one_close, one_exact]
737
694
  ```
738
695
 
739
- ##### :highlight (PostgreSQL 9.0 and newer only)
696
+ ##### :highlight
740
697
 
741
698
  Adding .with_pg_search_highlight after the pg_search_scope you can access to
742
699
  `pg_highlight` attribute for each object.
@@ -749,8 +706,10 @@ class Person < ActiveRecord::Base
749
706
  :against => :bio,
750
707
  :using => {
751
708
  :tsearch => {
752
- :start_sel => '<b>',
753
- :stop_sel => '</b>'
709
+ :highlight => {
710
+ :start_sel => '<b>',
711
+ :stop_sel => '</b>'
712
+ }
754
713
  }
755
714
  }
756
715
  end
@@ -773,10 +732,10 @@ Currently, this is not a true double-metaphone, as only the first metaphone is
773
732
  used for searching.
774
733
 
775
734
  Double Metaphone support is currently available as part of the [fuzzystrmatch
776
- contrib package](http://www.postgresql.org/docs/current/static/fuzzystrmatch.html)
735
+ extension](http://www.postgresql.org/docs/current/static/fuzzystrmatch.html)
777
736
  that must be installed before this feature can be used. In addition to the
778
- contrib package, you must install a utility function into your database. To
779
- generate and run a migration for this, run:
737
+ extension, you must install a utility function into your database. To generate
738
+ and run a migration for this, run:
780
739
 
781
740
  $ rails g pg_search:migration:dmetaphone
782
741
  $ rake db:migrate
@@ -810,9 +769,9 @@ Trigram search works by counting how many three-letter substrings (or
810
769
  Trigram search has some ability to work even with typos and misspellings in
811
770
  the query or text.
812
771
 
813
- Trigram support is currently available as part of the [pg_trgm contrib
814
- package](http://www.postgresql.org/docs/current/static/pgtrgm.html) that must
815
- be installed before this feature can be used.
772
+ Trigram support is currently available as part of the
773
+ [pg_trgm extension](http://www.postgresql.org/docs/current/static/pgtrgm.html) that must be installed before this
774
+ feature can be used.
816
775
 
817
776
  ```ruby
818
777
  class Website < ActiveRecord::Base
@@ -902,15 +861,14 @@ Image.combined_search('reasonable') # found with tsearch
902
861
  Image.combined_search('foo') # found with trigram
903
862
  ```
904
863
 
905
- ### Ignoring accent marks (PostgreSQL 9.0 and newer only)
864
+ ### Ignoring accent marks
906
865
 
907
866
  Most of the time you will want to ignore accent marks when searching. This
908
867
  makes it possible to find words like "piñata" when searching with the query
909
868
  "pinata". If you set a pg_search_scope to ignore accents, it will ignore
910
869
  accents in both the searchable text and the query terms.
911
870
 
912
- Ignoring accents uses the [unaccent contrib
913
- package](http://www.postgresql.org/docs/current/static/unaccent.html) that
871
+ Ignoring accents uses the [unaccent extension](http://www.postgresql.org/docs/current/static/unaccent.html) that
914
872
  must be installed before this feature can be used.
915
873
 
916
874
  ```ruby
@@ -930,8 +888,8 @@ SpanishQuestion.gringo_search("Cüåñtô") # => [how_many]
930
888
  ```
931
889
 
932
890
  Advanced users may wish to add indexes for the expressions that pg_search
933
- generates. Unfortunately, the unaccent function supplied by this contrib
934
- package is not indexable (as of PostgreSQL 9.1). Thus, you may want to write
891
+ generates. Unfortunately, the unaccent function supplied by this extension
892
+ is not indexable (as of PostgreSQL 9.1). Thus, you may want to write
935
893
  your own wrapper function and use it instead. This can be configured by
936
894
  calling the following code, perhaps in an initializer.
937
895
 
@@ -968,7 +926,7 @@ To use this functionality you'll need to do a few things:
968
926
  tsearch: {
969
927
  dictionary: 'english',
970
928
  tsvector_column: 'tsvector_content_tsearch'
971
- }
929
+ },
972
930
  trigram: {} # trigram does not use tsvectors
973
931
  }
974
932
  ```
@@ -1123,4 +1081,5 @@ Please read our [CONTRIBUTING guide](https://github.com/Casecommons/pg_search/bl
1123
1081
 
1124
1082
  ## LICENSE
1125
1083
 
1126
- MIT
1084
+ Copyright © 2010–2016 [Case Commons, Inc](http://casecommons.org).
1085
+ Licensed under the MIT license, see [LICENSE](/LICENSE) file.
data/Rakefile CHANGED
@@ -5,6 +5,12 @@ require 'rspec/core/rake_task'
5
5
  RSpec::Core::RakeTask.new(:spec)
6
6
 
7
7
  require "rubocop/rake_task"
8
- RuboCop::RakeTask.new
8
+ RuboCop::RakeTask.new do |t|
9
+ t.options = %w[--display-cop-names]
10
+ end
9
11
 
10
- task :default => %w[ spec rubocop ]
12
+ task :codeclimate do
13
+ sh 'bin/codeclimate-test-reporter' if ENV['CODECLIMATE_REPO_TOKEN']
14
+ end
15
+
16
+ task :default => %w[spec codeclimate rubocop]
data/lib/pg_search.rb CHANGED
@@ -3,9 +3,7 @@ require "active_support/concern"
3
3
  require "active_support/core_ext/module/attribute_accessors"
4
4
  require "active_support/core_ext/string/strip"
5
5
 
6
- require "pg_search/compatibility"
7
6
  require "pg_search/configuration"
8
- require "pg_search/extensions/arel"
9
7
  require "pg_search/features"
10
8
  require "pg_search/multisearch"
11
9
  require "pg_search/multisearchable"
@@ -92,8 +90,6 @@ module PgSearch
92
90
  end
93
91
  end
94
92
 
95
- class NotSupportedForPostgresqlVersion < StandardError; end
96
-
97
93
  class PgSearchRankNotSelected < StandardError
98
94
  def message
99
95
  "You must chain .with_pg_search_rank after the pg_search_scope to access the pg_search_rank attribute on returned records" # rubocop:disable Metrics/LineLength
@@ -71,10 +71,6 @@ module PgSearch
71
71
  options[:order_within_rank]
72
72
  end
73
73
 
74
- def postgresql_version
75
- model.connection.send(:postgresql_version)
76
- end
77
-
78
74
  private
79
75
 
80
76
  attr_reader :options
@@ -28,20 +28,33 @@ module PgSearch
28
28
  private
29
29
 
30
30
  def selects
31
- postgresql_version = @model.connection.send(:postgresql_version)
31
+ if singular_association?
32
+ selects_for_singular_association
33
+ else
34
+ selects_for_multiple_association
35
+ end
36
+ end
37
+
38
+ def selects_for_singular_association
39
+ columns.map do |column|
40
+ "#{column.full_name}::text AS #{column.alias}"
41
+ end.join(", ")
42
+ end
32
43
 
44
+ def selects_for_multiple_association
33
45
  columns.map do |column|
34
- case postgresql_version
35
- when 0..90000
36
- "array_to_string(array_agg(#{column.full_name}::text), ' ') AS #{column.alias}"
37
- else
38
- "string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
39
- end
46
+ "string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
40
47
  end.join(", ")
41
48
  end
42
49
 
43
50
  def relation(primary_key)
44
- @model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}").group(primary_key)
51
+ result = @model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}")
52
+ result = result.group(primary_key) unless singular_association?
53
+ result
54
+ end
55
+
56
+ def singular_association?
57
+ [:has_one, :belongs_to].include?(@model.reflect_on_association(@name).macro)
45
58
  end
46
59
  end
47
60
  end