like_query 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/lib/like_query/formatter.rb +32 -0
- data/lib/like_query/model_extensions.rb +25 -17
- data/lib/like_query/version.rb +1 -1
- data/lib/like_query.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4acb76ba7e99c858ec318cb2f52d82e3c063d53941775c5b18d34cb084f8d624
|
4
|
+
data.tar.gz: 619ed012558607d25ef0bef7766a96b5ee1ec9923185d6ff7c4dfda0a62ee08c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5846f20fa64023168c9f4468dadb90aa5f73ea131b63eae29e6ad9b06b750d4e5b9d38cd670accdad72be12eb2940a4af11dfac96c865fda0c7cfcc3ccc71aad
|
7
|
+
data.tar.gz: 66c59b0de6ebaa45fe6d237561c88a6e266575dade903dacbd6880df001cc4f172631a26040e9a7322f227f6bee1d787b300d8e9d2e19abb44211dd2d4510e8e
|
data/README.md
CHANGED
@@ -106,6 +106,25 @@ Book.like('Nelson 3.4..8', [:title, :price])
|
|
106
106
|
# => find books where title includes "Nelson" and price (must be number column) is between 3.4 and 8
|
107
107
|
```
|
108
108
|
|
109
|
+
**Column specific search**
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
Book.like('title:history', [:title, :price])
|
113
|
+
# => find books where title includes "history"
|
114
|
+
|
115
|
+
Book.like('author.name:marc', [{author: [:name]}])
|
116
|
+
# => find books where author.name includes "marc"
|
117
|
+
```
|
118
|
+
|
119
|
+
There is a helper for allowing the user translating the column names:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
LikeQuery::Formatter.translate_columns('one N:two', [['n','name']], case_sensitive: false)
|
123
|
+
# => 'one name:two'
|
124
|
+
# case_sensitive: Default is true
|
125
|
+
```
|
126
|
+
Only upper and lower chars (if case-insensitive), point, underscore, and no German-Umlaute are allowed on the column-key
|
127
|
+
|
109
128
|
**Class LikeQuery::Collect**
|
110
129
|
|
111
130
|
```ruby
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module LikeQuery
|
2
|
+
class Formatter
|
3
|
+
|
4
|
+
#== Translate columns for more convenient usage
|
5
|
+
# second parameter is always a Array of Arrays
|
6
|
+
# Example:
|
7
|
+
# translate_columns('d:50', [[:d,:diameter], ['w', 'width']], case_sensitive: false)
|
8
|
+
# => "diameter:50"
|
9
|
+
def self.translate_columns(search_string, column_translations, case_sensitive: true)
|
10
|
+
search_string.split(' ').map do |str|
|
11
|
+
|
12
|
+
reg = (case_sensitive ? /^[a-z_\.]+(?=:)/ : /^[a-zA-Z_\.]+(?=:)/)
|
13
|
+
|
14
|
+
column = if case_sensitive
|
15
|
+
str.match(reg)
|
16
|
+
else
|
17
|
+
str.downcase.match(reg)
|
18
|
+
end
|
19
|
+
if column
|
20
|
+
column_translations.each do |tr|
|
21
|
+
key = (case_sensitive ? tr.first : tr.first.downcase).to_s
|
22
|
+
if column.to_s == key
|
23
|
+
str = str.sub(reg, tr.second.to_s)
|
24
|
+
break
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
str
|
29
|
+
end.join(' ')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -54,18 +54,21 @@ module LikeQuery
|
|
54
54
|
end
|
55
55
|
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
|
+
__q = q.compact
|
59
|
+
queries.push("(#{__q.join(' OR ')})") if __q.present?
|
58
60
|
end
|
59
61
|
|
60
62
|
if associations.present?
|
61
63
|
r = left_outer_joins(associations).where(queries.join(" AND "))
|
62
|
-
dbg(->{"RESULT => #{r.to_sql}"})
|
64
|
+
dbg(-> { "RESULT => #{r.to_sql}" })
|
63
65
|
r
|
64
66
|
else
|
65
67
|
r = where(queries.join(" AND "))
|
66
|
-
dbg(->{"RESULT => #{r.to_sql}"})
|
68
|
+
dbg(-> { "RESULT => #{r.to_sql}" })
|
67
69
|
r
|
68
70
|
end
|
71
|
+
|
69
72
|
end
|
70
73
|
|
71
74
|
def generate_hash(output_schema = nil, limit: 50, image: nil, url: nil)
|
@@ -98,8 +101,10 @@ module LikeQuery
|
|
98
101
|
|
99
102
|
def format_string_element(str)
|
100
103
|
|
101
|
-
|
102
|
-
|
104
|
+
column = str.match(/^[a-z_\.]+(?=:)/)
|
105
|
+
_str = (column ? str.match(/(?<=:)[\s\S]+/) : str).to_s
|
106
|
+
match_number = _str.match(/^(\d+(?:\.\d+)?)$/)
|
107
|
+
match_range = _str.match(/^(\d+(?:\.\d+)?)(\.\.)(\d+(?:\.\d+)?)$/)
|
103
108
|
|
104
109
|
res = if match_range
|
105
110
|
mr = match_range.to_s
|
@@ -114,11 +119,10 @@ module LikeQuery
|
|
114
119
|
float_until: mn,
|
115
120
|
}
|
116
121
|
else
|
117
|
-
{
|
118
|
-
string: "%#{str}%"
|
119
|
-
}
|
122
|
+
{}
|
120
123
|
end
|
121
|
-
res[:string] =
|
124
|
+
res[:string] = _str
|
125
|
+
res[:column] = column.to_s if column
|
122
126
|
res
|
123
127
|
end
|
124
128
|
|
@@ -132,18 +136,22 @@ module LikeQuery
|
|
132
136
|
end
|
133
137
|
arel = base_object.arel_table[schema_column]
|
134
138
|
|
135
|
-
|
139
|
+
col = (association ? [association.name, schema_column.to_s].join('.') : schema_column.to_s)
|
136
140
|
|
137
|
-
|
138
|
-
|
139
|
-
q
|
141
|
+
if !search_string[:column] || col == search_string[:column]
|
142
|
+
if search_string[:float_from] && [:float, :decimal, :big_decimal, :integer, :bigint].include?(type)
|
140
143
|
|
141
|
-
|
144
|
+
q = arel.between(search_string[:float_from]..search_string[:float_until]).to_sql
|
145
|
+
dbg(" Query from number-field => #{q}")
|
146
|
+
q
|
147
|
+
|
148
|
+
else
|
142
149
|
|
143
|
-
|
144
|
-
|
145
|
-
|
150
|
+
q = arel.matches("%#{search_string[:string]}%").to_sql
|
151
|
+
dbg(" Query from string-field => #{q}")
|
152
|
+
q
|
146
153
|
|
154
|
+
end
|
147
155
|
end
|
148
156
|
end
|
149
157
|
|
data/lib/like_query/version.rb
CHANGED
data/lib/like_query.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.
|
4
|
+
version: 0.3.1
|
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-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/like_query.rb
|
36
36
|
- lib/like_query/collect.rb
|
37
37
|
- lib/like_query/extender.rb
|
38
|
+
- lib/like_query/formatter.rb
|
38
39
|
- lib/like_query/model_extensions.rb
|
39
40
|
- lib/like_query/railtie.rb
|
40
41
|
- lib/like_query/version.rb
|