rgviz-rails 0.7 → 0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rgviz_rails/executor.rb +17 -2
- data/rails/init.rb +4 -1
- data/spec/rgviz/executor_spec.rb +50 -2
- metadata +3 -3
data/lib/rgviz_rails/executor.rb
CHANGED
@@ -8,14 +8,16 @@ module Rgviz
|
|
8
8
|
@joins = {}
|
9
9
|
end
|
10
10
|
|
11
|
-
def execute
|
11
|
+
def execute(options = {})
|
12
12
|
@table = Table.new
|
13
|
+
@extra_conditions = options[:conditions]
|
13
14
|
|
14
15
|
generate_columns
|
15
16
|
generate_conditions
|
16
17
|
generate_group
|
17
18
|
generate_order
|
18
19
|
generate_rows
|
20
|
+
|
19
21
|
@table
|
20
22
|
end
|
21
23
|
|
@@ -64,9 +66,22 @@ module Rgviz
|
|
64
66
|
end
|
65
67
|
|
66
68
|
def generate_rows
|
69
|
+
conditions = @conditions
|
70
|
+
if @extra_conditions
|
71
|
+
if conditions
|
72
|
+
if @extra_conditions.kind_of? String
|
73
|
+
conditions = "(#{conditions}) AND #{@extra_conditions}"
|
74
|
+
elsif @extra_conditions.kind_of? Array
|
75
|
+
conditions = ["(#{conditions}) AND #{@extra_conditions[0]}", *@extra_conditions[1 .. -1]]
|
76
|
+
end
|
77
|
+
else
|
78
|
+
conditions = @extra_conditions
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
67
82
|
results = @model_class.send :all,
|
68
83
|
:select => @selects.join(','),
|
69
|
-
:conditions =>
|
84
|
+
:conditions => conditions,
|
70
85
|
:group => @group,
|
71
86
|
:order => @order,
|
72
87
|
:limit => @query.limit,
|
data/rails/init.rb
CHANGED
@@ -8,6 +8,7 @@ config.after_initialize do
|
|
8
8
|
when nil then original_render *args, &block
|
9
9
|
else
|
10
10
|
model = hash[:rgviz]
|
11
|
+
conditions = hash[:conditions]
|
11
12
|
query = params[:tq]
|
12
13
|
tqx = params[:tqx] || ''
|
13
14
|
pieces = tqx.split ';'
|
@@ -30,7 +31,9 @@ config.after_initialize do
|
|
30
31
|
|
31
32
|
begin
|
32
33
|
executor = Rgviz::Executor.new model, Rgviz::Parser.new(query).parse
|
33
|
-
|
34
|
+
options = {}
|
35
|
+
options[:conditions] = conditions if conditions
|
36
|
+
table = executor.execute options
|
34
37
|
original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'ok',version:'#{version}',table:#{table.to_json}});"
|
35
38
|
rescue ParseException => e
|
36
39
|
msg = e.message.gsub("'", "\\'")
|
data/spec/rgviz/executor_spec.rb
CHANGED
@@ -12,9 +12,9 @@ describe Executor do
|
|
12
12
|
Parser.new(string).parse
|
13
13
|
end
|
14
14
|
|
15
|
-
def exec(query)
|
15
|
+
def exec(query, options = {})
|
16
16
|
exec = Executor.new Person, (parse query)
|
17
|
-
exec.execute
|
17
|
+
exec.execute options
|
18
18
|
end
|
19
19
|
|
20
20
|
def format_datetime(date)
|
@@ -182,4 +182,52 @@ describe Executor do
|
|
182
182
|
Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
|
183
183
|
end
|
184
184
|
|
185
|
+
it "processes with conditions as string" do
|
186
|
+
Person.make :age => 1
|
187
|
+
Person.make :age => 2
|
188
|
+
Person.make :age => 3
|
189
|
+
|
190
|
+
table = exec 'select age', :conditions => 'age = 2'
|
191
|
+
|
192
|
+
table.rows.length.should == 1
|
193
|
+
table.rows[0].c.length.should == 1
|
194
|
+
table.rows[0].c[0].v.should == 2
|
195
|
+
end
|
196
|
+
|
197
|
+
it "processes with conditions as string and another filter" do
|
198
|
+
Person.make :age => 1
|
199
|
+
Person.make :age => 2
|
200
|
+
Person.make :age => 3
|
201
|
+
|
202
|
+
table = exec 'select age where age > 1', :conditions => 'age < 3'
|
203
|
+
|
204
|
+
table.rows.length.should == 1
|
205
|
+
table.rows[0].c.length.should == 1
|
206
|
+
table.rows[0].c[0].v.should == 2
|
207
|
+
end
|
208
|
+
|
209
|
+
it "processes with conditions as array" do
|
210
|
+
Person.make :age => 1
|
211
|
+
Person.make :age => 2
|
212
|
+
Person.make :age => 3
|
213
|
+
|
214
|
+
table = exec 'select age', :conditions => ['age = ?', 2]
|
215
|
+
|
216
|
+
table.rows.length.should == 1
|
217
|
+
table.rows[0].c.length.should == 1
|
218
|
+
table.rows[0].c[0].v.should == 2
|
219
|
+
end
|
220
|
+
|
221
|
+
it "processes with conditions as array and another filter" do
|
222
|
+
Person.make :age => 1
|
223
|
+
Person.make :age => 2
|
224
|
+
Person.make :age => 3
|
225
|
+
|
226
|
+
table = exec 'select age where age > 1', :conditions => ['age < ?', 3]
|
227
|
+
|
228
|
+
table.rows.length.should == 1
|
229
|
+
table.rows[0].c.length.should == 1
|
230
|
+
table.rows[0].c[0].v.should == 2
|
231
|
+
end
|
232
|
+
|
185
233
|
end
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rgviz-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 8
|
9
|
+
version: "0.8"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ary Borenszweig
|