rails-simple-search 1.0.2 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 544d8e584f073bd4f57b9db0426f8a6dc41d6272
4
- data.tar.gz: bc4bbd8caabca8932014b1dd2a7e932670210664
3
+ metadata.gz: e0cb79f906b5bb0f7c65885ef8787c7b907f3441
4
+ data.tar.gz: 3c3319e16c6a6faa0a71c73fdb013f5f8fd33eb5
5
5
  SHA512:
6
- metadata.gz: df18323be0781c21ce3213d55d79435de56946e2efa7c5237dbe4f75ac108a8d90881d78701de558b82dfee9043ef33f1f592a7c572a6388091a3c2fa0ee2466
7
- data.tar.gz: abffa445a4f31648637dca3816b6eee095ece3929f4407b3d1034512a2c55cfe400361a761626c8ee019d187073aea997ac7c99706a5fcbf25cd5edda1088630
6
+ metadata.gz: ff757c3910004616748741ed2fbb3c4e568d2caad9a8b042a87d4816abdda4916cebb17060d7ff07637feb0298a6cce1996653bacb6c38466c29f0cd0145d896
7
+ data.tar.gz: f5bdd04a091b1d5376e2dbaeeeace3740b3f62826b3d569abcbffaab7a8e309e93d3eed5bcc8f6d580c5504cc567a760aa18377c97d386cd3936b53594775fb2
@@ -2,6 +2,7 @@ require_relative 'sql_handler'
2
2
 
3
3
  module RailsSimpleSearch
4
4
  DEFAULT_CONFIG = { :exact_match => [],
5
+ :or_separator => '_or_',
5
6
  :paginate => true,
6
7
  :page_name => 'page',
7
8
  :offset => 0,
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
- if @conditions
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
- if @conditions.size < 1
107
- @conditions[0] = "#{key} #{verb} ?"
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.2
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-23 00:00:00.000000000 Z
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.4.5
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