dynamic_query 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/app/helpers/dynamic_query.erb +4 -4
- data/lib/dynamic_query/dynamic_query.erb +4 -4
- data/lib/dynamic_query/version.rb +1 -1
- data/lib/dynamic_query.rb +45 -50
- metadata +4 -4
data/README.md
CHANGED
@@ -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}
|
29
|
-
<%= submit_tag 'x', :name => "query[#{
|
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}
|
29
|
-
<%= submit_tag 'x', :name => "query[#{
|
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 %>
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
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.
|
94
|
+
query.each do |or_key, or_val|
|
90
95
|
and_stat = [[]]
|
91
96
|
or_val.each do |and_key, and_val|
|
92
|
-
if
|
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
|
133
|
-
|
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 |
|
154
|
-
if
|
155
|
-
|
156
|
-
|
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
|
-
|
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
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
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(
|
178
|
-
count =
|
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(
|
184
|
-
count =
|
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.
|
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-
|
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:
|
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.
|
158
|
+
summary: dynamic_query-0.3.0
|
159
159
|
test_files: []
|