rails-simple-search 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rails-simple-search.rb +1 -0
- data/lib/sql_handler.rb +81 -15
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0cb79f906b5bb0f7c65885ef8787c7b907f3441
|
4
|
+
data.tar.gz: 3c3319e16c6a6faa0a71c73fdb013f5f8fd33eb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff757c3910004616748741ed2fbb3c4e568d2caad9a8b042a87d4816abdda4916cebb17060d7ff07637feb0298a6cce1996653bacb6c38466c29f0cd0145d896
|
7
|
+
data.tar.gz: f5bdd04a091b1d5376e2dbaeeeace3740b3f62826b3d569abcbffaab7a8e309e93d3eed5bcc8f6d580c5504cc567a760aa18377c97d386cd3936b53594775fb2
|
data/lib/rails-simple-search.rb
CHANGED
data/lib/sql_handler.rb
CHANGED
@@ -20,9 +20,7 @@ module RailsSimpleSearch
|
|
20
20
|
run_criteria
|
21
21
|
|
22
22
|
query = @model_class.joins(@joins_str)
|
23
|
-
|
24
|
-
query = query.where(@conditions)
|
25
|
-
end
|
23
|
+
query = query.where(@condition_group.to_ar_condition) unless @condition_group.empty?
|
26
24
|
|
27
25
|
if @config[:paginate]
|
28
26
|
@count = query.count
|
@@ -52,12 +50,15 @@ module RailsSimpleSearch
|
|
52
50
|
|
53
51
|
def run_criteria
|
54
52
|
return unless @conditions.nil?
|
53
|
+
@condition_group = ConditionGroup.new
|
54
|
+
@condition_group.set_relation(:and)
|
55
|
+
|
55
56
|
@criteria.each do |key, value|
|
56
57
|
if @config[:page_name].to_s == key.to_s
|
57
58
|
@page = value.to_i
|
58
59
|
@criteria[key] = @page
|
59
60
|
else
|
60
|
-
parse_attribute(key, value)
|
61
|
+
@condition_group.add(parse_attribute(key, value))
|
61
62
|
end
|
62
63
|
end
|
63
64
|
|
@@ -103,14 +104,8 @@ module RailsSimpleSearch
|
|
103
104
|
verb = '='
|
104
105
|
end
|
105
106
|
|
106
|
-
|
107
|
-
|
108
|
-
@conditions[1] = value
|
109
|
-
else
|
110
|
-
@conditions[0] += " and #{key} #{verb} ?"
|
111
|
-
@conditions << value
|
112
|
-
end
|
113
|
-
end
|
107
|
+
ConditionGroup.new(ConditionItem.new(key, verb, value))
|
108
|
+
end
|
114
109
|
|
115
110
|
def insert_join(base_class, asso_ref)
|
116
111
|
base_table = base_class.table_name
|
@@ -132,10 +127,20 @@ module RailsSimpleSearch
|
|
132
127
|
end
|
133
128
|
|
134
129
|
def parse_attribute(attribute, value)
|
130
|
+
attributes = attribute.split(@config[:or_separator])
|
131
|
+
if(attributes.size > 1)
|
132
|
+
cg = ConditionGroup.new
|
133
|
+
cg.set_relation(:or)
|
134
|
+
attributes.each do |a|
|
135
|
+
cg.add(parse_attribute(a, value))
|
136
|
+
end
|
137
|
+
return cg
|
138
|
+
end
|
139
|
+
|
135
140
|
unless attribute =~ /\./
|
136
141
|
field = attribute
|
137
|
-
insert_condition(@model_class, attribute, field, value)
|
138
|
-
return
|
142
|
+
condition = insert_condition(@model_class, attribute, field, value)
|
143
|
+
return condition
|
139
144
|
end
|
140
145
|
|
141
146
|
association_fields = attribute.split(/\./)
|
@@ -148,8 +153,69 @@ module RailsSimpleSearch
|
|
148
153
|
base_class = association_fields.shift.klass
|
149
154
|
end
|
150
155
|
|
151
|
-
insert_condition(base_class, attribute, field, value)
|
156
|
+
condition = insert_condition(base_class, attribute, field, value)
|
157
|
+
return condition
|
152
158
|
end
|
153
159
|
|
154
160
|
end
|
161
|
+
|
162
|
+
class ConditionItem
|
163
|
+
attr_reader :field, :verb, :value
|
164
|
+
|
165
|
+
def initialize(field, verb, value)
|
166
|
+
@field = field
|
167
|
+
@verb = verb
|
168
|
+
@value = value
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
class ConditionGroup
|
173
|
+
def initialize(item=nil)
|
174
|
+
if item
|
175
|
+
@condition_item = item
|
176
|
+
else
|
177
|
+
@children = []
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def add(cg)
|
182
|
+
if leaf?
|
183
|
+
raise "It's not allowed to add child into leaf node"
|
184
|
+
end
|
185
|
+
@children << cg
|
186
|
+
end
|
187
|
+
|
188
|
+
def set_relation(and_or)
|
189
|
+
if leaf?
|
190
|
+
raise "It's not needed to set relation for leaf node"
|
191
|
+
end
|
192
|
+
@relation = and_or
|
193
|
+
end
|
194
|
+
|
195
|
+
def leaf?
|
196
|
+
@condition_item ? true : false
|
197
|
+
end
|
198
|
+
|
199
|
+
def empty?
|
200
|
+
(@children && @children.empty?) ? true : false
|
201
|
+
end
|
202
|
+
|
203
|
+
def to_ar_condition
|
204
|
+
condition = []
|
205
|
+
if leaf?
|
206
|
+
i = @condition_item
|
207
|
+
condition << "#{i.field} #{i.verb} ?"
|
208
|
+
condition << i.value
|
209
|
+
else
|
210
|
+
tmp = @children.map(&:to_ar_condition)
|
211
|
+
condition << "(" + tmp.map(&:first).join(" #{@relation} ") + ")"
|
212
|
+
tmp.each do |t|
|
213
|
+
(1..(t.length-1)).each do |index|
|
214
|
+
condition << t[index]
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
condition
|
219
|
+
end
|
220
|
+
end
|
155
221
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-simple-search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yi Zhang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: rails-simple-search is a light and easy to use gem. It could help developers
|
14
14
|
quickly build a search page.
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
41
|
rubyforge_project:
|
42
|
-
rubygems_version: 2.
|
42
|
+
rubygems_version: 2.5.1
|
43
43
|
signing_key:
|
44
44
|
specification_version: 4
|
45
45
|
summary: A very simple and light gem to quickly build search/filter function in rails
|