like_query 0.1.4 → 0.1.6
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/lib/like_query/model_extensions.rb +51 -27
- data/lib/like_query/version.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: c7de4a5d2ad260480095855c658997269624ccb606be9e38bd054342404bd7ad
|
|
4
|
+
data.tar.gz: 11f20847057ba089d2cf7020799f420bcf1f99ef2bc7aa09ed5a9a266a614725
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2540e87f1eabbab29fd0f032e99e98029dfdfe6c558b7793fc3d63d8635171f76b110cd56ee0c34beca883908c1a32063c610f8af52f4f52223050e830006af
|
|
7
|
+
data.tar.gz: 458da755375fc2651a95a13f1c4722da0f85711f9742ba701fd0aa79b528f377852b01c222c1423287d7c731ed36dacc0eef61512b03271d89ae07599141aa3e
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
module LikeQuery
|
|
2
2
|
module ModelExtensions
|
|
3
3
|
|
|
4
|
-
def like(search_string, schema = nil)
|
|
4
|
+
def like(search_string, schema = nil, debug: false)
|
|
5
5
|
|
|
6
6
|
search_string = '' if !search_string
|
|
7
7
|
search_string = search_string.to_s unless search_string.is_a?(String)
|
|
8
|
+
schema = [schema] if schema.is_a?(Symbol)
|
|
9
|
+
@debug_like_query = debug
|
|
8
10
|
|
|
9
11
|
raise 'like can only be called from a model' if self == ApplicationRecord
|
|
12
|
+
if schema.first.is_a?(Array) && schema.length >= 2
|
|
13
|
+
raise "only one array can be given: Either schema as one array or as multiple args, not as array"
|
|
14
|
+
end
|
|
10
15
|
|
|
11
16
|
queries = nil
|
|
12
17
|
associations = []
|
|
13
|
-
schema = [schema] if schema.is_a?(Symbol)
|
|
14
18
|
@like_query_schema = schema
|
|
15
19
|
|
|
16
20
|
search_string.split(' ').each do |s|
|
|
@@ -18,13 +22,13 @@ module LikeQuery
|
|
|
18
22
|
f_str = format_string_element(s)
|
|
19
23
|
|
|
20
24
|
q = nil
|
|
21
|
-
|
|
22
|
-
raise "only one array can be given: Either schema as one array or as multiple args, not as array"
|
|
23
|
-
end
|
|
25
|
+
|
|
24
26
|
(schema.first.is_a?(Array) ? schema.first : schema).each do |p|
|
|
27
|
+
dbg("• Iterate schema => #{p}")
|
|
25
28
|
enum_lab = enum_keys(p, s)
|
|
26
29
|
if enum_lab.present?
|
|
27
30
|
q = arel_table[p].in(enum_lab)
|
|
31
|
+
dbg(-> { " Query from enum => #{q.to_sql}" })
|
|
28
32
|
elsif p.is_a?(Hash)
|
|
29
33
|
p.each do |k, v|
|
|
30
34
|
assoc = reflect_on_association(k)
|
|
@@ -101,23 +105,25 @@ module LikeQuery
|
|
|
101
105
|
match_number = str.match(/^(\d+(?:\.\d+)?)$/)
|
|
102
106
|
match_range = str.match(/^(\d+(?:\.\d+)?)(\.\.)(\d+(?:\.\d+)?)$/)
|
|
103
107
|
|
|
104
|
-
if match_range
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
108
|
+
res = if match_range
|
|
109
|
+
mr = match_range.to_s
|
|
110
|
+
{
|
|
111
|
+
float_from: mr.split('..').first.to_f,
|
|
112
|
+
float_until: mr.split('..').last.to_f,
|
|
113
|
+
}
|
|
114
|
+
elsif match_number
|
|
115
|
+
mn = match_number.to_s
|
|
116
|
+
{
|
|
117
|
+
float_from: mn,
|
|
118
|
+
float_until: mn,
|
|
119
|
+
}
|
|
120
|
+
else
|
|
121
|
+
{
|
|
122
|
+
string: "%#{str}%"
|
|
123
|
+
}
|
|
124
|
+
end
|
|
125
|
+
res[:string] = "%#{str}%"
|
|
126
|
+
res
|
|
121
127
|
end
|
|
122
128
|
|
|
123
129
|
def create_arel(schema_column, search_string, association = nil)
|
|
@@ -130,13 +136,31 @@ module LikeQuery
|
|
|
130
136
|
end
|
|
131
137
|
arel = base_object.arel_table[schema_column]
|
|
132
138
|
|
|
133
|
-
if search_string[:float_from]
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
139
|
+
if search_string[:float_from] && [:float, :decimal, :big_decimal, :integer, :bigint].include?(type)
|
|
140
|
+
|
|
141
|
+
q = arel.between(search_string[:float_from]..search_string[:float_until])
|
|
142
|
+
dbg(-> { " Query from string-field => #{q.to_sql}" })
|
|
143
|
+
q
|
|
144
|
+
|
|
145
|
+
else
|
|
146
|
+
|
|
147
|
+
q = arel.matches(search_string[:string])
|
|
148
|
+
dbg(-> { " Query from string-field => #{q.to_sql}" })
|
|
149
|
+
q
|
|
150
|
+
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def dbg(label)
|
|
155
|
+
|
|
156
|
+
return unless @debug_like_query
|
|
157
|
+
|
|
158
|
+
if label.is_a?(Proc)
|
|
159
|
+
lab = label.call
|
|
137
160
|
else
|
|
138
|
-
|
|
161
|
+
lab = label
|
|
139
162
|
end
|
|
163
|
+
Rails.logger.debug("LikeQuery: #{lab}")
|
|
140
164
|
end
|
|
141
165
|
end
|
|
142
166
|
end
|
data/lib/like_query/version.rb
CHANGED
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.1.
|
|
4
|
+
version: 0.1.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- christian
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-11-
|
|
11
|
+
date: 2024-11-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|