dynamic_query 0.2.0 → 0.3.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.
data/README.md CHANGED
@@ -12,6 +12,7 @@ gem 'dynamic_query'
12
12
  rails g dynamic_query
13
13
 
14
14
  ## you may skip this step unless you want to customize your own helper
15
+ ## Regenerate the helper after upgrade dynamic_query
15
16
  rails g dynamic_query:helper
16
17
  ```
17
18
 
@@ -5,8 +5,8 @@
5
5
  <% or_val.each_with_index do |(and_key, and_val), idx2| %>
6
6
  <tr>
7
7
  <td>
8
- <%= submit_tag 'ǁ', :name => "query[add_or]", :style => 'color: purple;', :title => 'OR' if idx1 == 0 && idx2 == 0 %>
9
- <%= submit_tag '-', :name => "query[remove_#{or_key}]", :style => 'color: red;' if idx1 != 0 && idx2 == 0 %>
8
+ <%= submit_tag 'ǁ', :name => "query[action][add_or]", :style => 'color: purple;', :title => 'OR' if idx1 == 0 && idx2 == 0 %>
9
+ <%= submit_tag '-', :name => "query[action][remove_#{or_key}]", :style => 'color: red;' if idx1 != 0 && idx2 == 0 %>
10
10
  </td>
11
11
  <td><%= select_tag "query[#{or_key}][#{and_key}][column]", options_for_select(panel[:columns].keys, and_val[:column]) %></td>
12
12
  <td><%= select_tag "query[#{or_key}][#{and_key}][operator]", options_for_select(DynamicQuery::OPERATOR, and_val[:operator]), :class => 'query_op' %></td>
@@ -25,8 +25,8 @@
25
25
  <% end %>
26
26
  </td>
27
27
  <td>
28
- <%= submit_tag '+', :name => "query[#{or_key}-and]", :style => 'color: blue;', :title => 'AND' if idx2 == 0 %>
29
- <%= submit_tag 'x', :name => "query[#{or_key}-#{and_key}]", :style => 'color: green;' if idx2 != 0 %>
28
+ <%= submit_tag '+', :name => "query[action][add_and_to_#{or_key}]", :style => 'color: blue;', :title => 'AND' if idx2 == 0 %>
29
+ <%= submit_tag 'x', :name => "query[action][remove_#{and_key}_from_#{or_key}]", :style => 'color: green;' if idx2 != 0 %>
30
30
  </td>
31
31
  </tr>
32
32
  <% end %>
@@ -5,8 +5,8 @@
5
5
  <% or_val.each_with_index do |(and_key, and_val), idx2| %>
6
6
  <tr>
7
7
  <td>
8
- <%= submit_tag 'ǁ', :name => "query[add_or]", :style => 'color: purple;', :title => 'OR' if idx1 == 0 && idx2 == 0 %>
9
- <%= submit_tag '-', :name => "query[remove_#{or_key}]", :style => 'color: red;' if idx1 != 0 && idx2 == 0 %>
8
+ <%= submit_tag 'ǁ', :name => "query[action][add_or]", :style => 'color: purple;', :title => 'OR' if idx1 == 0 && idx2 == 0 %>
9
+ <%= submit_tag '-', :name => "query[action][remove_#{or_key}]", :style => 'color: red;' if idx1 != 0 && idx2 == 0 %>
10
10
  </td>
11
11
  <td><%= select_tag "query[#{or_key}][#{and_key}][column]", options_for_select(panel[:columns].keys, and_val[:column]) %></td>
12
12
  <td><%= select_tag "query[#{or_key}][#{and_key}][operator]", options_for_select(OPERATOR, and_val[:operator]), :class => 'query_op' %></td>
@@ -25,8 +25,8 @@
25
25
  <% end %>
26
26
  </td>
27
27
  <td>
28
- <%= submit_tag '+', :name => "query[#{or_key}-and]", :style => 'color: blue;', :title => 'AND' if idx2 == 0 %>
29
- <%= submit_tag 'x', :name => "query[#{or_key}-#{and_key}]", :style => 'color: green;' if idx2 != 0 %>
28
+ <%= submit_tag '+', :name => "query[action][add_and_to_#{or_key}]", :style => 'color: blue;', :title => 'AND' if idx2 == 0 %>
29
+ <%= submit_tag 'x', :name => "query[action][remove_#{and_key}_from_#{or_key}]", :style => 'color: green;' if idx2 != 0 %>
30
30
  </td>
31
31
  </tr>
32
32
  <% end %>
@@ -2,7 +2,7 @@ class DynamicQuery
2
2
 
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 2
5
+ MINOR = 3
6
6
  PATCH = 0
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
data/lib/dynamic_query.rb CHANGED
@@ -75,21 +75,26 @@ module DynamicQuery
75
75
 
76
76
  def panel(query)
77
77
  query ||= { 'or_1' => { 'and_1' => { :column => '', :operator => '', :value1 => '', :value2 => '' } } }
78
- add_and_condition(query)
79
- add_or_condition(query)
80
- remove_and_condition(query)
81
- remove_or_condition(query)
82
- query[:columns] = @columns
83
- query
78
+ query = query.clone
79
+ action = query[:action]
80
+
81
+ panel = filter_valid_info(query)
82
+ panel_action(panel, action)
83
+ panel[:columns] = @columns
84
+
85
+ panel
84
86
  end
85
87
 
86
88
  def statement(query)
87
89
  query ||= { 'or_1' => { 'and_1' => { :column => '', :operator => '', :value1 => '', :value2 => '' } } }
90
+ query = query.clone
91
+ query = filter_valid_info(query)
92
+
88
93
  or_stat = []
89
- query.select { |k, v| k =~ /^or_\d+$/ && v.kind_of?(Hash) }.each do |or_key, or_val|
94
+ query.each do |or_key, or_val|
90
95
  and_stat = [[]]
91
96
  or_val.each do |and_key, and_val|
92
- if and_val.kind_of?(Hash) && @columns.include?(and_val[:column])
97
+ if @columns.include?(and_val[:column])
93
98
  case and_val[:operator]
94
99
  when '=', '>', '>=', '<', '<=', '!=', 'LIKE', 'NOT LIKE'
95
100
  unless and_val[:value1].blank?
@@ -129,59 +134,49 @@ module DynamicQuery
129
134
  private
130
135
 
131
136
 
132
- def remove_or_condition(query)
133
- query.each do |key, val|
134
- if key =~ /^remove_or_\d+$/
135
- query.delete("or_#{key.match(/\d+/)[0]}")
136
- break
137
- end
138
- end
139
- end
140
-
141
- def remove_and_condition(query)
142
- query.each do |key, val|
143
- if key =~ /^or_\d+-and_\d+$/
144
- query["or_#{key.scan(/\d+/)[0]}"].delete("and_#{key.scan(/\d+/)[1]}")
145
- break
146
- end
147
- end
148
- end
149
-
150
- def add_or_condition(query)
151
- or_key = nil
137
+ def filter_valid_info(query)
138
+ output = {}
152
139
 
153
- query.each do |key, val|
154
- if key =~ /^add_or$/
155
- or_key = get_new_or_condition_key(query)
156
- break
140
+ query.each do |or_key, or_val|
141
+ if or_key =~ /^or_\d+$/ && or_val.kind_of?(Hash)
142
+ or_val.each do |and_key, and_val|
143
+ if and_key =~ /^and_\d+$/ && and_val.kind_of?(Hash) &&
144
+ (['column', 'operator', 'value1', 'value2'] - and_val.keys.map { |k| k.to_s }).empty?
145
+ output[or_key] ||= {}; output[or_key][and_key] ||= {}
146
+ output[or_key][and_key] = and_val
147
+ end
148
+ end
157
149
  end
158
150
  end
159
151
 
160
- if or_key
161
- query[or_key] = {}
162
- query[or_key]['and_1'] = { :column => '', :operator => '', :value1 => '', :value2 => '' }
163
- end
152
+ output
164
153
  end
165
154
 
166
-
167
- def add_and_condition(query)
168
- query.each do |key, val|
169
- if key =~ /^or_\d+-and$/
170
- and_key = get_new_and_condition_key(query["or_#{key.match(/\d+/)[0]}"])
171
- query["or_#{key.match(/\d+/)[0]}"][and_key] = { :column => '', :operator => '', :value1 => '', :value2 => '' }
172
- break
173
- end
174
- end
155
+ def panel_action(panel, action)
156
+ case action.keys.first
157
+ when /^add_or$/
158
+ or_key = get_new_or_condition_key(panel)
159
+ panel[or_key] = { 'and_1' => { :column => '', :operator => '', :value1 => '', :value2 => '' } }
160
+ when /^remove_or_\d+/
161
+ panel.delete("or_#{action.keys.first.match(/\d+/)[0]}")
162
+ when /add_and_to_or_\d+/
163
+ or_key = "or_#{action.keys.first.match(/\d+/)[0]}"
164
+ and_key = get_new_and_condition_key(panel[or_key])
165
+ panel[or_key][and_key] = { :column => '', :operator => '', :value1 => '', :value2 => '' }
166
+ when /remove_and_\d+_from_or_\d+/
167
+ or_key = "or_#{action.keys.first.scan(/\d+/)[1]}"
168
+ and_key = "and_#{action.keys.first.scan(/\d+/)[0]}"
169
+ panel[or_key].delete(and_key) if panel[or_key]
170
+ end if action.kind_of?(Hash)
175
171
  end
176
172
 
177
- def get_new_or_condition_key(query)
178
- count = query.keys.select { |i| i =~ /\d+$/ }.map { |key| key.match(/\d+/)[0].to_i }.max.to_i + 1
179
- p count
173
+ def get_new_or_condition_key(or_conditions)
174
+ count = or_conditions.keys.map { |key| key.match(/\d+/)[0].to_i }.max.to_i + 1
180
175
  "or_#{count}"
181
176
  end
182
177
 
183
- def get_new_and_condition_key(or_condition)
184
- count = or_condition.keys.map { |key| key.match(/\d+/)[0].to_i }.max.to_i + 1
178
+ def get_new_and_condition_key(and_conditions)
179
+ count = and_conditions.keys.map { |key| key.match(/\d+/)[0].to_i }.max.to_i + 1
185
180
  "and_#{count}"
186
181
  end
187
182
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-22 00:00:00.000000000 Z
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: 2346444755047590570
146
+ hash: -1018923921235041626
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements:
@@ -155,5 +155,5 @@ rubyforge_project:
155
155
  rubygems_version: 1.8.24
156
156
  signing_key:
157
157
  specification_version: 3
158
- summary: dynamic_query-0.2.0
158
+ summary: dynamic_query-0.3.0
159
159
  test_files: []