searchable-by 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/searchable_by/column.rb +12 -6
- data/searchable-by.gemspec +1 -1
- data/spec/searchable_by_spec.rb +11 -6
- data/spec/spec_helper.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 472133b9b419e573a1c87b721a5c368b3915ef28cb94e897424f55f7191f723f
|
4
|
+
data.tar.gz: d3c31e307f54b0545171e7e12b86d75d32f5874676af467399fdb2e533927c16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bd8dc39da51b94327d7912661cda9e2d38cdbf7d6e62eea1930e5d39a85c3bfd81c468477e8cfac48451b7dde055b9075a03686787c481c1cf2fcc5fa9d9c5f
|
7
|
+
data.tar.gz: e9881b0f0022a0f884d6780afb3f24daf53872a473e22571f1eed730c8c76083148dbec4e99825875a68bb425568776c04fb7ac5d7ad26b1aa9202be3160d357
|
data/Gemfile.lock
CHANGED
data/lib/searchable_by/column.rb
CHANGED
@@ -51,16 +51,22 @@ module SearchableBy
|
|
51
51
|
when :exact
|
52
52
|
term.downcase!
|
53
53
|
scope.and(node.lower.eq(term))
|
54
|
+
when :full
|
55
|
+
escape_term!(term)
|
56
|
+
scope.and(node.matches(term))
|
54
57
|
when :prefix
|
55
|
-
|
56
|
-
term.gsub!('_', '\_')
|
58
|
+
escape_term!(term)
|
57
59
|
scope.and(node.matches("#{term}%"))
|
58
|
-
else
|
59
|
-
|
60
|
-
term.gsub!('_', '\_')
|
61
|
-
term.gsub!(wildcard, '%') if wildcard
|
60
|
+
else # :all (wraps term in wildcards)
|
61
|
+
escape_term!(term)
|
62
62
|
scope.and(node.matches("%#{term}%"))
|
63
63
|
end
|
64
64
|
end
|
65
|
+
|
66
|
+
def escape_term!(term)
|
67
|
+
term.gsub!('%', '\%')
|
68
|
+
term.gsub!('_', '\_')
|
69
|
+
term.gsub!(wildcard, '%') if wildcard
|
70
|
+
end
|
65
71
|
end
|
66
72
|
end
|
data/searchable-by.gemspec
CHANGED
data/spec/searchable_by_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe SearchableBy do
|
|
8
8
|
|
9
9
|
it 'configures correctly' do
|
10
10
|
expect(AbstractModel._searchable_by_config.columns.size).to eq(1)
|
11
|
-
expect(Post._searchable_by_config.columns.size).to eq(
|
11
|
+
expect(Post._searchable_by_config.columns.size).to eq(6)
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'generates SQL' do
|
@@ -24,10 +24,12 @@ describe SearchableBy do
|
|
24
24
|
expect(sql).to include(%("posts"."body" LIKE '%foo\\%bar%'))
|
25
25
|
|
26
26
|
sql = User.search_by('uni*dom').to_sql
|
27
|
-
expect(sql).to include(%("users"."country" LIKE '
|
27
|
+
expect(sql).to include(%("users"."country" LIKE 'uni%dom'))
|
28
|
+
expect(sql).to include(%("users"."bio" LIKE '%uni*dom%'))
|
28
29
|
|
29
30
|
sql = User.search_by('"uni * dom"').to_sql
|
30
|
-
expect(sql).to include(%("users"."country" LIKE '
|
31
|
+
expect(sql).to include(%("users"."country" LIKE 'uni % dom'))
|
32
|
+
expect(sql).to include(%("users"."bio" LIKE '%uni * dom%'))
|
31
33
|
end
|
32
34
|
|
33
35
|
it 'searches' do
|
@@ -63,6 +65,9 @@ describe SearchableBy do
|
|
63
65
|
expect(Post.search_by('"ab"').pluck(:title)).to be_empty
|
64
66
|
expect(Post.search_by('"ab1"').pluck(:title)).to match_array(%w[ab1])
|
65
67
|
|
68
|
+
# country uses match: :full in combination with wildcard: '*'
|
69
|
+
expect(Post.search_by('*kingdom').pluck(:title)).to match_array(%w[ax1 ax2 ab1])
|
70
|
+
|
66
71
|
# body uses match: :all (default)
|
67
72
|
expect(Post.search_by('recip').pluck(:title)).to match_array(%w[ax1 ax2 bx1 bx2 ab1])
|
68
73
|
end
|
@@ -89,8 +94,8 @@ describe SearchableBy do
|
|
89
94
|
end
|
90
95
|
|
91
96
|
it 'supports wildcard searching' do
|
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
|
+
expect(User.search_by('*uni*dom')).to match_array(USERS.values_at(:a))
|
98
|
+
expect(User.search_by('*uni*o*')).to match_array(USERS.values_at(:a, :b))
|
99
|
+
expect(User.search_by('*uni*of*dom')).to be_empty
|
95
100
|
end
|
96
101
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -32,7 +32,7 @@ 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: :full
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -44,6 +44,7 @@ class Post < AbstractModel
|
|
44
44
|
column :title, match: :prefix, match_phrase: :exact
|
45
45
|
column :body
|
46
46
|
column proc { User.arel_table[:name] }, match: :exact
|
47
|
+
column proc { User.arel_table[:country] }, wildcard: '*', match: :full
|
47
48
|
column { User.arel_table.alias('reviewers_posts')[:name] }
|
48
49
|
|
49
50
|
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.6.
|
4
|
+
version: 0.6.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: 2021-07-
|
11
|
+
date: 2021-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|