searchable-by 0.6.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +4 -1
- data/.rubocop.yml +1 -1
- data/Gemfile.lock +35 -38
- data/Gemfile.rails6 +4 -0
- data/Gemfile.rails6.lock +91 -0
- data/README.md +11 -0
- data/lib/searchable_by/column.rb +22 -22
- data/lib/searchable_by/concern.rb +8 -11
- data/lib/searchable_by/config.rb +7 -0
- data/lib/searchable_by/profiles.rb +23 -0
- data/lib/searchable_by/util.rb +0 -6
- data/lib/searchable_by.rb +1 -0
- data/searchable-by.gemspec +5 -2
- data/spec/searchable_by_spec.rb +19 -8
- data/spec/spec_helper.rb +6 -1
- metadata +9 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a91aa1b24f6ea20f4568629c1d6cc4010c2a6754932ad1251500a702c2f79165
|
4
|
+
data.tar.gz: bbe553d775212b8bb0c076fac08fe10ed7912931a150b3c93c6becd7fa298e7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 958e8ebc7f8ee60f4531f75fdf5780f667986985f0392af3a303ad0f82f3a810e2bd8aa23171b25ee9d906a111cc4be8cd95a83b9c302f3fe55390e95112835d
|
7
|
+
data.tar.gz: 393536f83f00c301b2a2bcf9ae10a158f6ddd9cb642170c893e68d9d1c8bb967131de058c48414f20111c1a5cecf7c0eb98240028e9858a2c788918e38fd3642
|
data/.github/workflows/test.yml
CHANGED
@@ -11,7 +11,10 @@ jobs:
|
|
11
11
|
runs-on: ubuntu-latest
|
12
12
|
strategy:
|
13
13
|
matrix:
|
14
|
-
ruby-version: ["2.
|
14
|
+
ruby-version: ["2.7", "3.0", "3.1"]
|
15
|
+
gemfiles: ["Gemfile", "Gemfile.rails6"]
|
16
|
+
env:
|
17
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
15
18
|
steps:
|
16
19
|
- uses: actions/checkout@v2
|
17
20
|
- uses: ruby/setup-ruby@v1
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,79 +1,76 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
searchable-by (0.
|
4
|
+
searchable-by (0.7.1)
|
5
5
|
activerecord
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (
|
11
|
-
activesupport (=
|
12
|
-
activerecord (
|
13
|
-
activemodel (=
|
14
|
-
activesupport (=
|
15
|
-
activesupport (
|
10
|
+
activemodel (7.0.2.3)
|
11
|
+
activesupport (= 7.0.2.3)
|
12
|
+
activerecord (7.0.2.3)
|
13
|
+
activemodel (= 7.0.2.3)
|
14
|
+
activesupport (= 7.0.2.3)
|
15
|
+
activesupport (7.0.2.3)
|
16
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
17
|
i18n (>= 1.6, < 2)
|
18
18
|
minitest (>= 5.1)
|
19
19
|
tzinfo (~> 2.0)
|
20
|
-
zeitwerk (~> 2.3)
|
21
20
|
ast (2.4.2)
|
22
21
|
concurrent-ruby (1.1.9)
|
23
|
-
diff-lcs (1.
|
24
|
-
i18n (1.
|
22
|
+
diff-lcs (1.5.0)
|
23
|
+
i18n (1.10.0)
|
25
24
|
concurrent-ruby (~> 1.0)
|
26
|
-
minitest (5.
|
27
|
-
parallel (1.
|
28
|
-
parser (3.
|
25
|
+
minitest (5.15.0)
|
26
|
+
parallel (1.21.0)
|
27
|
+
parser (3.1.1.0)
|
29
28
|
ast (~> 2.4.1)
|
30
|
-
rainbow (3.
|
29
|
+
rainbow (3.1.1)
|
31
30
|
rake (13.0.6)
|
32
|
-
regexp_parser (2.
|
31
|
+
regexp_parser (2.2.1)
|
33
32
|
rexml (3.2.5)
|
34
|
-
rspec (3.
|
35
|
-
rspec-core (~> 3.
|
36
|
-
rspec-expectations (~> 3.
|
37
|
-
rspec-mocks (~> 3.
|
38
|
-
rspec-core (3.
|
39
|
-
rspec-support (~> 3.
|
40
|
-
rspec-expectations (3.
|
33
|
+
rspec (3.11.0)
|
34
|
+
rspec-core (~> 3.11.0)
|
35
|
+
rspec-expectations (~> 3.11.0)
|
36
|
+
rspec-mocks (~> 3.11.0)
|
37
|
+
rspec-core (3.11.0)
|
38
|
+
rspec-support (~> 3.11.0)
|
39
|
+
rspec-expectations (3.11.0)
|
41
40
|
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
-
rspec-support (~> 3.
|
43
|
-
rspec-mocks (3.
|
41
|
+
rspec-support (~> 3.11.0)
|
42
|
+
rspec-mocks (3.11.0)
|
44
43
|
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
-
rspec-support (~> 3.
|
46
|
-
rspec-support (3.
|
47
|
-
rubocop (1.
|
44
|
+
rspec-support (~> 3.11.0)
|
45
|
+
rspec-support (3.11.0)
|
46
|
+
rubocop (1.26.0)
|
48
47
|
parallel (~> 1.10)
|
49
|
-
parser (>= 3.
|
48
|
+
parser (>= 3.1.0.0)
|
50
49
|
rainbow (>= 2.2.2, < 4.0)
|
51
50
|
regexp_parser (>= 1.8, < 3.0)
|
52
51
|
rexml
|
53
|
-
rubocop-ast (>= 1.
|
52
|
+
rubocop-ast (>= 1.16.0, < 2.0)
|
54
53
|
ruby-progressbar (~> 1.7)
|
55
54
|
unicode-display_width (>= 1.4.0, < 3.0)
|
56
|
-
rubocop-ast (1.
|
57
|
-
parser (>= 3.
|
55
|
+
rubocop-ast (1.16.0)
|
56
|
+
parser (>= 3.1.1.0)
|
58
57
|
rubocop-bsm (0.6.0)
|
59
58
|
rubocop (~> 1.0)
|
60
59
|
rubocop-performance
|
61
60
|
rubocop-rake
|
62
61
|
rubocop-rspec
|
63
|
-
rubocop-performance (1.
|
62
|
+
rubocop-performance (1.13.3)
|
64
63
|
rubocop (>= 1.7.0, < 2.0)
|
65
64
|
rubocop-ast (>= 0.4.0)
|
66
65
|
rubocop-rake (0.6.0)
|
67
66
|
rubocop (~> 1.0)
|
68
|
-
rubocop-rspec (2.
|
69
|
-
rubocop (~> 1.
|
70
|
-
rubocop-ast (>= 1.1.0)
|
67
|
+
rubocop-rspec (2.9.0)
|
68
|
+
rubocop (~> 1.19)
|
71
69
|
ruby-progressbar (1.11.0)
|
72
70
|
sqlite3 (1.4.2)
|
73
71
|
tzinfo (2.0.4)
|
74
72
|
concurrent-ruby (~> 1.0)
|
75
|
-
unicode-display_width (2.
|
76
|
-
zeitwerk (2.4.2)
|
73
|
+
unicode-display_width (2.1.0)
|
77
74
|
|
78
75
|
PLATFORMS
|
79
76
|
ruby
|
@@ -88,4 +85,4 @@ DEPENDENCIES
|
|
88
85
|
sqlite3
|
89
86
|
|
90
87
|
BUNDLED WITH
|
91
|
-
2.
|
88
|
+
2.3.9
|
data/Gemfile.rails6
ADDED
data/Gemfile.rails6.lock
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
searchable-by (0.7.1)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.1.4.7)
|
11
|
+
activesupport (= 6.1.4.7)
|
12
|
+
activerecord (6.1.4.7)
|
13
|
+
activemodel (= 6.1.4.7)
|
14
|
+
activesupport (= 6.1.4.7)
|
15
|
+
activesupport (6.1.4.7)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
zeitwerk (~> 2.3)
|
21
|
+
ast (2.4.2)
|
22
|
+
concurrent-ruby (1.1.9)
|
23
|
+
diff-lcs (1.5.0)
|
24
|
+
i18n (1.10.0)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.15.0)
|
27
|
+
parallel (1.21.0)
|
28
|
+
parser (3.1.1.0)
|
29
|
+
ast (~> 2.4.1)
|
30
|
+
rainbow (3.1.1)
|
31
|
+
rake (13.0.6)
|
32
|
+
regexp_parser (2.2.1)
|
33
|
+
rexml (3.2.5)
|
34
|
+
rspec (3.11.0)
|
35
|
+
rspec-core (~> 3.11.0)
|
36
|
+
rspec-expectations (~> 3.11.0)
|
37
|
+
rspec-mocks (~> 3.11.0)
|
38
|
+
rspec-core (3.11.0)
|
39
|
+
rspec-support (~> 3.11.0)
|
40
|
+
rspec-expectations (3.11.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.11.0)
|
43
|
+
rspec-mocks (3.11.0)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.11.0)
|
46
|
+
rspec-support (3.11.0)
|
47
|
+
rubocop (1.26.0)
|
48
|
+
parallel (~> 1.10)
|
49
|
+
parser (>= 3.1.0.0)
|
50
|
+
rainbow (>= 2.2.2, < 4.0)
|
51
|
+
regexp_parser (>= 1.8, < 3.0)
|
52
|
+
rexml
|
53
|
+
rubocop-ast (>= 1.16.0, < 2.0)
|
54
|
+
ruby-progressbar (~> 1.7)
|
55
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
56
|
+
rubocop-ast (1.16.0)
|
57
|
+
parser (>= 3.1.1.0)
|
58
|
+
rubocop-bsm (0.6.0)
|
59
|
+
rubocop (~> 1.0)
|
60
|
+
rubocop-performance
|
61
|
+
rubocop-rake
|
62
|
+
rubocop-rspec
|
63
|
+
rubocop-performance (1.13.3)
|
64
|
+
rubocop (>= 1.7.0, < 2.0)
|
65
|
+
rubocop-ast (>= 0.4.0)
|
66
|
+
rubocop-rake (0.6.0)
|
67
|
+
rubocop (~> 1.0)
|
68
|
+
rubocop-rspec (2.9.0)
|
69
|
+
rubocop (~> 1.19)
|
70
|
+
ruby-progressbar (1.11.0)
|
71
|
+
sqlite3 (1.4.2)
|
72
|
+
tzinfo (2.0.4)
|
73
|
+
concurrent-ruby (~> 1.0)
|
74
|
+
unicode-display_width (2.1.0)
|
75
|
+
zeitwerk (2.5.4)
|
76
|
+
|
77
|
+
PLATFORMS
|
78
|
+
x86_64-linux
|
79
|
+
|
80
|
+
DEPENDENCIES
|
81
|
+
activerecord (~> 6.1)
|
82
|
+
bundler
|
83
|
+
rake
|
84
|
+
rspec
|
85
|
+
rubocop
|
86
|
+
rubocop-bsm
|
87
|
+
searchable-by!
|
88
|
+
sqlite3
|
89
|
+
|
90
|
+
BUNDLED WITH
|
91
|
+
2.3.9
|
data/README.md
CHANGED
@@ -34,6 +34,14 @@ class Post < ActiveRecord::Base
|
|
34
34
|
joins(:author)
|
35
35
|
end
|
36
36
|
end
|
37
|
+
|
38
|
+
# Multiple profiles can be defined. This one only searches title and metadata.
|
39
|
+
searchable_by :metadata do
|
40
|
+
column :title, match: :prefix
|
41
|
+
|
42
|
+
# Allow custom wildcard replacement using a match e.g. searching for `"My*Post"` will query `ILIKE 'My%Post'`.
|
43
|
+
column :metadata, match: :prefix, wildcard: '*'
|
44
|
+
end
|
37
45
|
end
|
38
46
|
|
39
47
|
# Search for 'alice'
|
@@ -44,4 +52,7 @@ Post.search_by('alice "pie recipe"')
|
|
44
52
|
|
45
53
|
# Search for 'alice' but NOT for 'pie recipe'
|
46
54
|
Post.search_by('alice -"pie recipe"')
|
55
|
+
|
56
|
+
# Search using metadata profile
|
57
|
+
Post.search_by('meta', profile: :metadata)
|
47
58
|
```
|
data/lib/searchable_by/column.rb
CHANGED
@@ -1,22 +1,19 @@
|
|
1
1
|
module SearchableBy
|
2
2
|
class Column
|
3
|
+
VALID_MATCH_TYPES = %i[all prefix exact].freeze
|
4
|
+
|
3
5
|
attr_reader :attr, :type, :match, :match_phrase, :wildcard
|
4
6
|
attr_accessor :node
|
5
7
|
|
6
|
-
def initialize(attr, type: :string, match: :all, match_phrase: nil, wildcard: nil
|
7
|
-
if opts.key?(:min_length)
|
8
|
-
ActiveSupport::Deprecation.warn(
|
9
|
-
'Setting min_length for individual columns is deprecated and will be removed in the next release.' \
|
10
|
-
'Please pass it as an option to searchable_by instead',
|
11
|
-
)
|
12
|
-
end
|
13
|
-
|
8
|
+
def initialize(attr, type: :string, match: :all, match_phrase: nil, wildcard: nil)
|
14
9
|
@attr = attr
|
15
10
|
@type = type.to_sym
|
16
11
|
@match = match
|
17
12
|
@match_phrase = match_phrase || match
|
18
|
-
@min_length = opts[:min_length].to_i
|
19
13
|
@wildcard = wildcard
|
14
|
+
|
15
|
+
raise ArgumentError, "invalid match option #{@match.inspect}" unless VALID_MATCH_TYPES.include? @match
|
16
|
+
raise ArgumentError, "invalid match_phrase option #{@match_phrase.inspect}" unless VALID_MATCH_TYPES.include? @match_phrase
|
20
17
|
end
|
21
18
|
|
22
19
|
def build_condition(value)
|
@@ -32,11 +29,6 @@ module SearchableBy
|
|
32
29
|
|
33
30
|
private
|
34
31
|
|
35
|
-
# TODO: remove when removing min_length option from columns
|
36
|
-
def usable?(value)
|
37
|
-
value.term.length >= @min_length
|
38
|
-
end
|
39
|
-
|
40
32
|
def int_condition(scope, value)
|
41
33
|
scope.and(node.eq(Integer(value.term)))
|
42
34
|
rescue ArgumentError
|
@@ -49,18 +41,26 @@ module SearchableBy
|
|
49
41
|
|
50
42
|
case type
|
51
43
|
when :exact
|
52
|
-
|
53
|
-
|
44
|
+
if wildcard
|
45
|
+
escape_term!(term)
|
46
|
+
scope.and(node.matches(term))
|
47
|
+
else
|
48
|
+
term.downcase!
|
49
|
+
scope.and(node.lower.eq(term))
|
50
|
+
end
|
54
51
|
when :prefix
|
55
|
-
|
56
|
-
term.gsub!('_', '\_')
|
52
|
+
escape_term!(term)
|
57
53
|
scope.and(node.matches("#{term}%"))
|
58
|
-
else
|
59
|
-
|
60
|
-
term.gsub!('_', '\_')
|
61
|
-
term.gsub!(wildcard, '%') if wildcard
|
54
|
+
else # :all (wraps term in wildcards)
|
55
|
+
escape_term!(term)
|
62
56
|
scope.and(node.matches("%#{term}%"))
|
63
57
|
end
|
64
58
|
end
|
59
|
+
|
60
|
+
def escape_term!(term)
|
61
|
+
term.gsub!('%', '\%')
|
62
|
+
term.gsub!('_', '\_')
|
63
|
+
term.gsub!(wildcard, '%') if wildcard
|
64
|
+
end
|
65
65
|
end
|
66
66
|
end
|
@@ -1,28 +1,25 @@
|
|
1
1
|
module SearchableBy
|
2
2
|
module Concern
|
3
3
|
def self.extended(base) # :nodoc:
|
4
|
-
base.class_attribute :
|
5
|
-
base.
|
4
|
+
base.class_attribute :_searchable_by_profiles, instance_accessor: false, instance_predicate: false
|
5
|
+
base._searchable_by_profiles = Profiles.new
|
6
6
|
super
|
7
7
|
end
|
8
8
|
|
9
9
|
def inherited(base) # :nodoc:
|
10
|
-
base.
|
10
|
+
base._searchable_by_profiles = _searchable_by_profiles.dup
|
11
11
|
super
|
12
12
|
end
|
13
13
|
|
14
|
-
def searchable_by(max_terms: nil, min_length: 0, **options, &block)
|
15
|
-
|
16
|
-
|
17
|
-
_searchable_by_config.min_length = min_length
|
18
|
-
_searchable_by_config.options.update(options) unless options.empty?
|
19
|
-
_searchable_by_config
|
14
|
+
def searchable_by(profile = :default, max_terms: nil, min_length: 0, **options, &block)
|
15
|
+
_searchable_by_profiles[profile].configure(max_terms, min_length, **options, &block)
|
16
|
+
_searchable_by_profiles
|
20
17
|
end
|
21
18
|
|
22
19
|
# @param [String] query the search query
|
23
20
|
# @return [ActiveRecord::Relation] the scoped relation
|
24
|
-
def search_by(query)
|
25
|
-
config =
|
21
|
+
def search_by(query, profile: :default)
|
22
|
+
config = _searchable_by_profiles[profile]
|
26
23
|
columns = config.columns
|
27
24
|
return all if columns.empty?
|
28
25
|
|
data/lib/searchable_by/config.rb
CHANGED
@@ -11,6 +11,13 @@ module SearchableBy
|
|
11
11
|
scope { all }
|
12
12
|
end
|
13
13
|
|
14
|
+
def configure(max_terms, min_length, **options, &block)
|
15
|
+
instance_eval(&block)
|
16
|
+
self.max_terms = max_terms if max_terms
|
17
|
+
self.min_length = min_length
|
18
|
+
self.options.update(options) unless options.empty?
|
19
|
+
end
|
20
|
+
|
14
21
|
def initialize_copy(other)
|
15
22
|
@columns = other.columns.dup
|
16
23
|
super
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SearchableBy
|
2
|
+
class Profiles
|
3
|
+
def initialize
|
4
|
+
@profiles = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def [](name)
|
8
|
+
name = name.to_sym
|
9
|
+
@profiles[name] ||= Config.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def each(&block)
|
13
|
+
@profiles.each(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize_copy(other)
|
17
|
+
@profiles = {}
|
18
|
+
other.each do |name, config|
|
19
|
+
@profiles[name] = config.dup
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/searchable_by/util.rb
CHANGED
@@ -29,12 +29,6 @@ module SearchableBy
|
|
29
29
|
|
30
30
|
def self.build_clauses(columns, values)
|
31
31
|
values.map do |value|
|
32
|
-
# TODO: remove when removing min_length option from columns
|
33
|
-
usable = columns.all? do |column|
|
34
|
-
column.send(:usable?, value)
|
35
|
-
end
|
36
|
-
next unless usable
|
37
|
-
|
38
32
|
group = columns.map do |column|
|
39
33
|
column.build_condition(value)
|
40
34
|
end.tap(&:compact!)
|
data/lib/searchable_by.rb
CHANGED
@@ -4,6 +4,7 @@ module SearchableBy
|
|
4
4
|
autoload :Column, 'searchable_by/column'
|
5
5
|
autoload :Concern, 'searchable_by/concern'
|
6
6
|
autoload :Config, 'searchable_by/config'
|
7
|
+
autoload :Profiles, 'searchable_by/profiles'
|
7
8
|
autoload :Util, 'searchable_by/util'
|
8
9
|
|
9
10
|
Value = Struct.new(:term, :negate, :phrase)
|
data/searchable-by.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'searchable-by'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.7.1'
|
4
4
|
s.authors = ['Dimitrij Denissenko']
|
5
5
|
s.email = ['dimitrij@blacksquaremedia.com']
|
6
6
|
s.summary = 'Generate search scopes'
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.files = `git ls-files -z`.split("\x0").reject {|f| f.start_with?('spec/') }
|
12
12
|
s.test_files = `git ls-files -z -- spec/*`.split("\x0")
|
13
13
|
s.require_paths = ['lib']
|
14
|
-
s.required_ruby_version = '>= 2.
|
14
|
+
s.required_ruby_version = '>= 2.7'
|
15
15
|
|
16
16
|
s.add_dependency 'activerecord'
|
17
17
|
s.add_development_dependency 'bundler'
|
@@ -20,4 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.add_development_dependency 'rubocop'
|
21
21
|
s.add_development_dependency 'rubocop-bsm'
|
22
22
|
s.add_development_dependency 'sqlite3'
|
23
|
+
s.metadata = {
|
24
|
+
'rubygems_mfa_required' => 'true',
|
25
|
+
}
|
23
26
|
end
|
data/spec/searchable_by_spec.rb
CHANGED
@@ -7,8 +7,9 @@ describe SearchableBy do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'configures correctly' do
|
10
|
-
expect(AbstractModel.
|
11
|
-
expect(Post.
|
10
|
+
expect(AbstractModel._searchable_by_profiles[:default].columns.size).to eq(1)
|
11
|
+
expect(Post._searchable_by_profiles[:default].columns.size).to eq(6)
|
12
|
+
expect(User._searchable_by_profiles[:default].min_length).to eq(3)
|
12
13
|
end
|
13
14
|
|
14
15
|
it 'generates SQL' do
|
@@ -24,10 +25,12 @@ describe SearchableBy do
|
|
24
25
|
expect(sql).to include(%("posts"."body" LIKE '%foo\\%bar%'))
|
25
26
|
|
26
27
|
sql = User.search_by('uni*dom').to_sql
|
27
|
-
expect(sql).to include(%("users"."country" LIKE '
|
28
|
+
expect(sql).to include(%("users"."country" LIKE 'uni%dom'))
|
29
|
+
expect(sql).to include(%("users"."bio" LIKE '%uni*dom%'))
|
28
30
|
|
29
31
|
sql = User.search_by('"uni * dom"').to_sql
|
30
|
-
expect(sql).to include(%("users"."country" LIKE '
|
32
|
+
expect(sql).to include(%("users"."country" LIKE 'uni % dom'))
|
33
|
+
expect(sql).to include(%("users"."bio" LIKE '%uni * dom%'))
|
31
34
|
end
|
32
35
|
|
33
36
|
it 'searches' do
|
@@ -63,6 +66,9 @@ describe SearchableBy do
|
|
63
66
|
expect(Post.search_by('"ab"').pluck(:title)).to be_empty
|
64
67
|
expect(Post.search_by('"ab1"').pluck(:title)).to match_array(%w[ab1])
|
65
68
|
|
69
|
+
# country uses match: :full in combination with wildcard: '*'
|
70
|
+
expect(Post.search_by('*kingdom').pluck(:title)).to match_array(%w[ax1 ax2 ab1])
|
71
|
+
|
66
72
|
# body uses match: :all (default)
|
67
73
|
expect(Post.search_by('recip').pluck(:title)).to match_array(%w[ax1 ax2 bx1 bx2 ab1])
|
68
74
|
end
|
@@ -88,9 +94,14 @@ describe SearchableBy do
|
|
88
94
|
expect(Post.search_by(POSTS[:ab1].id.to_s).count).to eq(1)
|
89
95
|
end
|
90
96
|
|
91
|
-
it 'supports
|
92
|
-
expect(User.search_by('uni*dom')).to match_array(USERS.values_at(:a))
|
93
|
-
expect(User.search_by('uni*o')).to match_array(USERS.values_at(:a, :b))
|
94
|
-
expect(User.search_by('uni*of*dom')).to be_empty
|
97
|
+
it 'supports wildcards' do
|
98
|
+
expect(User.search_by('*uni*dom')).to match_array(USERS.values_at(:a))
|
99
|
+
expect(User.search_by('*uni*o*')).to match_array(USERS.values_at(:a, :b))
|
100
|
+
expect(User.search_by('*uni*of*dom')).to be_empty
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'supports profiles' do
|
104
|
+
expect(User.search_by('king', profile: :country_only)).to match_array(USERS.values_at(:a))
|
105
|
+
expect(User.search_by('Norris', profile: :country_only)).to be_empty
|
95
106
|
end
|
96
107
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -32,7 +32,11 @@ class User < AbstractModel
|
|
32
32
|
|
33
33
|
searchable_by min_length: 3 do
|
34
34
|
column :bio
|
35
|
-
column :country, wildcard: '*'
|
35
|
+
column :country, wildcard: '*', match: :exact
|
36
|
+
end
|
37
|
+
|
38
|
+
searchable_by :country_only do
|
39
|
+
column :country
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
@@ -44,6 +48,7 @@ class Post < AbstractModel
|
|
44
48
|
column :title, match: :prefix, match_phrase: :exact
|
45
49
|
column :body
|
46
50
|
column proc { User.arel_table[:name] }, match: :exact
|
51
|
+
column proc { User.arel_table[:country] }, wildcard: '*', match: :exact
|
47
52
|
column { User.arel_table.alias('reviewers_posts')[:name] }
|
48
53
|
|
49
54
|
scope do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: searchable-by
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimitrij Denissenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -120,6 +120,8 @@ files:
|
|
120
120
|
- ".rubocop.yml"
|
121
121
|
- Gemfile
|
122
122
|
- Gemfile.lock
|
123
|
+
- Gemfile.rails6
|
124
|
+
- Gemfile.rails6.lock
|
123
125
|
- LICENSE
|
124
126
|
- README.md
|
125
127
|
- Rakefile
|
@@ -128,6 +130,7 @@ files:
|
|
128
130
|
- lib/searchable_by/column.rb
|
129
131
|
- lib/searchable_by/concern.rb
|
130
132
|
- lib/searchable_by/config.rb
|
133
|
+
- lib/searchable_by/profiles.rb
|
131
134
|
- lib/searchable_by/util.rb
|
132
135
|
- searchable-by.gemspec
|
133
136
|
- spec/searchable_by/util_spec.rb
|
@@ -136,7 +139,8 @@ files:
|
|
136
139
|
homepage: https://github.com/bsm/sortable-by
|
137
140
|
licenses:
|
138
141
|
- Apache-2.0
|
139
|
-
metadata:
|
142
|
+
metadata:
|
143
|
+
rubygems_mfa_required: 'true'
|
140
144
|
post_install_message:
|
141
145
|
rdoc_options: []
|
142
146
|
require_paths:
|
@@ -145,14 +149,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
149
|
requirements:
|
146
150
|
- - ">="
|
147
151
|
- !ruby/object:Gem::Version
|
148
|
-
version: '2.
|
152
|
+
version: '2.7'
|
149
153
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
154
|
requirements:
|
151
155
|
- - ">="
|
152
156
|
- !ruby/object:Gem::Version
|
153
157
|
version: '0'
|
154
158
|
requirements: []
|
155
|
-
rubygems_version: 3.
|
159
|
+
rubygems_version: 3.3.3
|
156
160
|
signing_key:
|
157
161
|
specification_version: 4
|
158
162
|
summary: Generate search scopes
|