like_query 0.4.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/lib/like_query/formatter.rb +4 -1
- data/lib/like_query/model_extensions.rb +27 -2
- data/lib/like_query/version.rb +1 -1
- data/lib/like_query/view_helpers.rb +1 -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: 243756b108021b5385001f7ac4c20f3d7ece0588aa91bad0f72a58b38f72c4be
|
4
|
+
data.tar.gz: 4978917f31f305fb13f5185b1826d97ed986f656bf9b9a676cc4079a127b06df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f02df34b624b4eedd1c985d44d22d07b838672c47e9f6bf65fc7aae5ee5ac35ca8ded9a48628b67955dbede7f1da786705bbbe417aba101f954b87d5c55a54c2
|
7
|
+
data.tar.gz: d610aee369a628a1a05630f1e4d09f76c72c69e59f035af572e4958dd99ed1603f06f2c8e9beea2417b5432bd2a267d285fd4b0e317ff3c86456f08f3ad40ca5
|
data/README.md
CHANGED
@@ -116,6 +116,13 @@ Book.like('author.name:marc', [{author: [:name]}])
|
|
116
116
|
# => find books where author.name includes "marc"
|
117
117
|
```
|
118
118
|
|
119
|
+
LikeQuery raises a error if invalid column name is given, vor example:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
Book.like('title2:history', [:title, :price])
|
123
|
+
# => raises a error like column «title2» is not present within available columns: «title, price»
|
124
|
+
```
|
125
|
+
|
119
126
|
There is a helper for allowing the user translating the column names:
|
120
127
|
|
121
128
|
```ruby
|
data/lib/like_query/formatter.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
module LikeQuery
|
2
2
|
class Formatter
|
3
3
|
|
4
|
+
COLUMN_REGEX_CASE_SENSITIVE = /^[a-z_\.0-9]+(?=:)/
|
5
|
+
COLUMN_REGEX_CASE_INSENSITIVE = /^[a-zA-Z_\.0-9]+(?=:)/
|
6
|
+
|
4
7
|
#== Translate columns for more convenient usage
|
5
8
|
# second parameter is always a Array of Arrays
|
6
9
|
# Example:
|
@@ -9,7 +12,7 @@ module LikeQuery
|
|
9
12
|
def self.translate_columns(search_string, column_translations, case_sensitive: true)
|
10
13
|
search_string.split(' ').map do |str|
|
11
14
|
|
12
|
-
reg = (case_sensitive ?
|
15
|
+
reg = (case_sensitive ? COLUMN_REGEX_CASE_SENSITIVE : COLUMN_REGEX_CASE_INSENSITIVE)
|
13
16
|
|
14
17
|
column = if case_sensitive
|
15
18
|
str.match(reg)
|
@@ -15,12 +15,13 @@ module LikeQuery
|
|
15
15
|
|
16
16
|
queries = []
|
17
17
|
associations = []
|
18
|
+
available_column_keys = []
|
18
19
|
@like_query_schema = schema
|
19
20
|
|
20
21
|
search_string.split(' ').each do |s|
|
21
22
|
|
22
23
|
f_str = format_string_element(s)
|
23
|
-
|
24
|
+
#exception_on_unknown_column(f_str, schema)
|
24
25
|
q = []
|
25
26
|
|
26
27
|
(schema.first.is_a?(Array) ? schema.first : schema).each do |p|
|
@@ -38,9 +39,11 @@ module LikeQuery
|
|
38
39
|
raise "Unknown association: :#{k}" unless assoc.present?
|
39
40
|
associations.push(k.to_sym) unless associations.include?(k.to_sym)
|
40
41
|
if v.is_a?(Symbol) || v.is_a?(String)
|
42
|
+
available_column_keys.push([k.to_s, v.to_s].join('.'))
|
41
43
|
q.push(query_string(v, f_str, assoc))
|
42
44
|
elsif v.is_a?(Array)
|
43
45
|
v.each do |_v|
|
46
|
+
available_column_keys.push([k.to_s, _v.to_s].join('.'))
|
44
47
|
q.push(query_string(_v, f_str, assoc))
|
45
48
|
end
|
46
49
|
else
|
@@ -49,12 +52,18 @@ module LikeQuery
|
|
49
52
|
end
|
50
53
|
|
51
54
|
else
|
55
|
+
available_column_keys.push(p.to_s)
|
52
56
|
q.push(query_string(p, f_str))
|
53
57
|
|
54
58
|
end
|
55
59
|
|
56
60
|
end
|
57
61
|
|
62
|
+
# raise error
|
63
|
+
if f_str[:column] && !available_column_keys.include?(f_str[:column])
|
64
|
+
raise "column «#{f_str[:column]}» is not present within available columns: «#{available_column_keys.join(', ')}»"
|
65
|
+
end
|
66
|
+
|
58
67
|
__q = q.compact
|
59
68
|
queries.push("(#{__q.join(' OR ')})") if __q.present?
|
60
69
|
end
|
@@ -101,7 +110,7 @@ module LikeQuery
|
|
101
110
|
|
102
111
|
def format_string_element(str)
|
103
112
|
|
104
|
-
column = str.match(
|
113
|
+
column = str.match(LikeQuery::Formatter::COLUMN_REGEX_CASE_SENSITIVE)
|
105
114
|
_str = (column ? str.match(/(?<=:)[\s\S]+/) : str).to_s
|
106
115
|
match_number = _str.match(/^(\d+(?:\.\d+)?)$/)
|
107
116
|
match_range = _str.match(/^(\d+(?:\.\d+)?)(\.\.)(\d+(?:\.\d+)?)$/)
|
@@ -155,6 +164,22 @@ module LikeQuery
|
|
155
164
|
end
|
156
165
|
end
|
157
166
|
|
167
|
+
# def exception_on_unknown_column(search, schema)
|
168
|
+
# return unless search[:column]
|
169
|
+
#
|
170
|
+
# keys = []
|
171
|
+
#
|
172
|
+
# (schema.first.is_a?(Array) ? schema.first : schema).each do |p|
|
173
|
+
# if p.is_a?(Hash)
|
174
|
+
# p.each do |k, v|
|
175
|
+
# if v.is_a?(Array)
|
176
|
+
# else
|
177
|
+
# end
|
178
|
+
# end
|
179
|
+
# end
|
180
|
+
# end
|
181
|
+
# end
|
182
|
+
|
158
183
|
def dbg(label)
|
159
184
|
|
160
185
|
return unless @debug_like_query
|
data/lib/like_query/version.rb
CHANGED
@@ -3,7 +3,7 @@ module LikeQuery
|
|
3
3
|
def like_query_mark(string, column = nil, find: @mark_string)
|
4
4
|
res = string.to_s
|
5
5
|
find.split(' ').each do |word|
|
6
|
-
_col = word.match(
|
6
|
+
_col = word.match(LikeQuery::Formatter::COLUMN_REGEX_CASE_SENSITIVE)
|
7
7
|
if !column || !_col || _col.to_s == column.to_s
|
8
8
|
_word = (_col ? word.match(/(?<=:)[\s\S]+/) : word).to_s
|
9
9
|
res = Translighterate.highlight(res, _word)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: like_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|