rgviz-rails 0.28 → 0.29

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,25 @@
1
1
  module Rgviz
2
2
  module ViewHelper
3
3
  def rgviz(options = {})
4
+ def opts_to_json(opts, special_keys)
5
+ @s = '{'
6
+ i = 0
7
+ opts.each do |key, value|
8
+ key = key.to_s.gsub('"', '\"')
9
+
10
+ @s << ',' if i > 0
11
+ @s << "\"#{key}\":"
12
+ if special_keys.include?(key) || !value.kind_of?(String)
13
+ @s << value.to_s
14
+ else
15
+ value = value.gsub('"', '\"')
16
+ @s << "\"#{value}\""
17
+ end
18
+ i += 1
19
+ end
20
+ @s << '}'
21
+ end
22
+
4
23
  options = options.with_indifferent_access
5
24
 
6
25
  id = options[:id]
@@ -23,7 +42,36 @@ module Rgviz
23
42
  opts = options[:options] || {}
24
43
  opts[:width] = 640 unless opts[:width]
25
44
  opts[:height] = 480 unless opts[:height]
26
- opts = opts.to_json
45
+
46
+ params = []
47
+ uses_rgviz_get_value = false
48
+ uses_rgviz_append = false
49
+
50
+ visitor = MagicNamesVisitor.new(html_prefix, js_prefix, param_prefix)
51
+
52
+ special_keys = []
53
+
54
+ opts.each do |key, value|
55
+ next unless value.kind_of?(String)
56
+
57
+ source = visitor.get_source(value, false)
58
+ next unless source[:source]
59
+
60
+ special_keys << key
61
+
62
+ case source[:source]
63
+ when :html
64
+ opts[key] = "rgviz_get_value('#{source[:id]}')"
65
+ uses_rgviz_get_value = true
66
+ when :js
67
+ opts[key] = "#{source[:id]}()"
68
+ when :param
69
+ opts[key] = "param_#{source[:id]}"
70
+ params << source[:id].to_i unless params.include?(source[:id])
71
+ end
72
+ end
73
+
74
+ opts = opts_to_json(opts, special_keys)
27
75
 
28
76
  raise "Must specify an :id" unless id
29
77
  raise "Must specify a :kind" unless kind
@@ -35,11 +83,14 @@ module Rgviz
35
83
  query = Parser.new(query).parse
36
84
 
37
85
  # And replace the html_ and javascript_ magic names
38
- visitor = MagicNamesVisitor.new(html_prefix, js_prefix, param_prefix)
39
86
  query.accept visitor
40
87
  query_builder = visitor.query_builder
41
88
  query_builder_var = visitor.query_builder_var
42
- params = visitor.params
89
+
90
+ uses_rgviz_get_value |= visitor.uses_rgviz_get_value?
91
+ uses_rgviz_append |= visitor.uses_rgviz_append?
92
+
93
+ visitor.params.each{|p| params << p unless params.include?(p)}
43
94
  params = params.sort!.map{|i| "param_#{i}"}
44
95
 
45
96
  out = ''
@@ -49,11 +100,49 @@ module Rgviz
49
100
  if @first_time == 1
50
101
  out << "<script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>\n"
51
102
  end
52
- @first_time = 0
53
-
54
103
  # Now the real script
55
104
  out << "<script type=\"text/javascript\">\n"
56
105
 
106
+ # Define a function to get the value of an html element
107
+ if uses_rgviz_get_value && !@defined_rgviz_get_value
108
+ out << "function rgviz_get_value(id) {\n"
109
+ out << "var e = document.getElementById(id);\n"
110
+ out << "var n = e.tagName.toLowerCase();\n"
111
+ out << "var s = null;\n"
112
+ out << "if (n == 'select' && e.multiple) {\n"
113
+ out << "var s = [];\n"
114
+ out << "var o = e.options;\n"
115
+ out << "for(var i = 0; i < o.length; i++)\n"
116
+ out << "if (o[i].selected) s.push(o[i].value);\n"
117
+ out << "} else if (n == 'input' && e.type.toLowerCase() == 'checkbox') {\n"
118
+ out << "s = [e.checked];\n"
119
+ out << "} else {\n"
120
+ out << "s = [e.value];\n"
121
+ out << "}\n"
122
+ out << "return s;\n"
123
+ out << "}\n"
124
+ @defined_rgviz_get_value = true
125
+ end
126
+
127
+ # Define a function to append the value of a magic something
128
+ if uses_rgviz_append && !@defined_rgviz_append
129
+ out << "function rgviz_append(s, b, a) {\n"
130
+ out << "var q = '';\n"
131
+ out << "if (s.length == 0) {\n"
132
+ out << "q += '1 = 2';\n";
133
+ out << "} else {\n"
134
+ out << "if (s.length > 1) q += '(';\n"
135
+ out << "for(var i = 0; i < s.length; i++) {\n";
136
+ out << "if (i > 0) q += ' or ';\n"
137
+ out << "q += b + s[i] + a;\n"
138
+ out << "}";
139
+ out << "if (s.length > 1) q += ')';\n"
140
+ out << "}\n"
141
+ out << "return q;\n"
142
+ out << "}\n"
143
+ @defined_rgviz_append = true
144
+ end
145
+
57
146
  # Load visualizations and the package, if not already loaded
58
147
  pack = kind.downcase
59
148
  @packages ||= []
@@ -99,6 +188,8 @@ module Rgviz
99
188
  end
100
189
  out << "></div>\n"
101
190
 
191
+ @first_time = 0
192
+
102
193
  out
103
194
  end
104
195
 
@@ -126,8 +217,15 @@ module Rgviz
126
217
  @params
127
218
  end
128
219
 
220
+ def uses_rgviz_get_value?
221
+ @uses_rgviz_get_value
222
+ end
223
+
224
+ def uses_rgviz_append?
225
+ @uses_rgviz_append
226
+ end
227
+
129
228
  def visit_query(node)
130
- @s << "var e = null;\n"
131
229
  @s << "var q = '"
132
230
  node.select.accept self if node.select
133
231
  node.where.accept self if node.where
@@ -229,24 +327,10 @@ module Rgviz
229
327
  @s << "';\n"
230
328
  case source[:source]
231
329
  when :html
232
- @s << "e = document.getElementById('#{source[:id]}');\n"
233
- @s << "if (e.tagName.toLowerCase() == 'select' && e.multiple) {\n"
234
- @s << "var s = [];\n"
235
- @s << "var o = e.options;\n"
236
- @s << "for(var i = 0; i < o.length; i++) {\n"
237
- @s << "if (o[i].selected)\n";
238
- @s << "s.push(o[i].value);\n"
239
- @s << "}\n"
240
- append_selections node, source
241
- @s << "} else {\n"
242
- @s << "q += '"
243
- node.left.accept self
244
- @s << " #{node.operator} "
245
- @s << "';\n"
246
- append_before_source_type source[:type]
247
- @s << "e.value"
248
- append_after_source_type source[:type]
249
- @s << "}\n";
330
+ @s << "var s = rgviz_get_value('#{source[:id]}');\n"
331
+ append_selections node, source
332
+
333
+ @uses_rgviz_get_value = true
250
334
  when :js
251
335
  @s << "var s = #{source[:id]}();\n"
252
336
  @s << "if (typeof(s) != 'object') s = [s];\n"
@@ -284,23 +368,19 @@ module Rgviz
284
368
  when nil
285
369
  @s << "`#{node.name}`"
286
370
  when :html
287
- @s << "';\n"
288
371
  append_before_source_type source[:type]
289
- @s << "document.getElementById('#{source[:id]}').value"
372
+ @s << " + rgviz_get_value('#{source[:id]}') + "
290
373
  append_after_source_type source[:type]
291
- @s << "q += '"
374
+
375
+ @uses_rgviz_get_value = true
292
376
  when :js
293
- @s << "';\n"
294
377
  append_before_source_type source[:type]
295
- @s << "#{source[:id]}()"
378
+ @s << " + #{source[:id]}() + "
296
379
  append_after_source_type source[:type]
297
- @s << "q += '"
298
380
  when :param
299
- @s << "';\n"
300
381
  append_before_source_type source[:type]
301
- @s << "param_#{source[:id]}"
382
+ @s << " + param_#{source[:id]} + "
302
383
  append_after_source_type source[:type]
303
- @s << "q += '"
304
384
  @params << source[:id].to_i unless @params.include?(source[:id])
305
385
  end
306
386
  end
@@ -310,7 +390,8 @@ module Rgviz
310
390
  end
311
391
 
312
392
  def visit_string_column(node)
313
- @s << node.value
393
+ value = node.value.gsub('"', '\"')
394
+ @s << "\"#{value}\""
314
395
  end
315
396
 
316
397
  def visit_boolean_column(node)
@@ -361,13 +442,25 @@ module Rgviz
361
442
  end
362
443
  end
363
444
 
364
- def get_source(name)
445
+ def get_source(name, include_type = true)
365
446
  if name.start_with?(@html_prefix)
366
- get_source_type :html, name[@html_prefix.length .. -1]
447
+ if include_type
448
+ get_source_type :html, name[@html_prefix.length .. -1]
449
+ else
450
+ {:source => :html, :id => name[@html_prefix.length .. -1]}
451
+ end
367
452
  elsif name.start_with?(@js_prefix)
368
- get_source_type :js, name[@js_prefix.length .. -1]
453
+ if include_type
454
+ get_source_type :js, name[@js_prefix.length .. -1]
455
+ else
456
+ {:source => :js, :id => name[@js_prefix.length .. -1]}
457
+ end
369
458
  elsif name.start_with?(@param_prefix)
370
- get_source_type :param, name[@param_prefix.length .. -1]
459
+ if include_type
460
+ get_source_type :param, name[@param_prefix.length .. -1]
461
+ else
462
+ {:source => :param, :id => name[@param_prefix.length .. -1]}
463
+ end
371
464
  else
372
465
  {}
373
466
  end
@@ -392,15 +485,15 @@ module Rgviz
392
485
  def append_before_source_type(type)
393
486
  case type
394
487
  when :number
395
- @s << "q += "
488
+ return
396
489
  when :string
397
- @s << "q += \"'\" +"
490
+ @s << "\"'"
398
491
  when :date
399
- @s << "q += \"date '\" + "
492
+ @s << "date \"'"
400
493
  when :datetime
401
- @s << "q += \"datetime '\" + "
494
+ @s << "datetime \"'"
402
495
  when :timeofday
403
- @s << "q += \"timeofday '\" + "
496
+ @s << "timeofday \"'"
404
497
  end
405
498
  end
406
499
 
@@ -409,27 +502,19 @@ module Rgviz
409
502
  when :number
410
503
  return
411
504
  else
412
- @s << " + \"'\";\n"
505
+ @s << "'\""
413
506
  end
414
507
  end
415
508
 
416
509
  def append_selections(node, source)
417
- @s << "if (s.length == 0) {\n"
418
- @s << "q += '1 = 2';\n";
419
- @s << "} else {\n"
420
- @s << "q += '(';\n"
421
- @s << "for(var i = 0; i < s.length; i++) {\n";
422
- @s << "if (i > 0) q += ' or ';\n"
423
- @s << "q += '"
424
- node.left.accept self
425
- @s << " #{node.operator} "
426
- @s << "';\n"
427
- append_before_source_type source[:type]
428
- @s << "s[i]"
429
- append_after_source_type source[:type]
430
- @s << "}";
431
- @s << "q += ')';\n"
432
- @s << "}\n"
510
+ @s << "q += rgviz_append(s, '";
511
+ node.left.accept self
512
+ @s << " #{node.operator} "
513
+ append_before_source_type source[:type]
514
+ @s << ", "
515
+ append_after_source_type source[:type]
516
+ @s << "');\n"
517
+ @uses_rgviz_append = true
433
518
  end
434
519
 
435
520
  def has_magic_name?(node)
data/rails/init.rb CHANGED
@@ -42,9 +42,9 @@ config.after_initialize do
42
42
  original_render :text => Rgviz::JsRenderer.render_error('not_supported', "Unsupported output type: #{out}", tqx)
43
43
  end
44
44
  rescue ParseException => e
45
- case out
45
+ case tqx['out']
46
46
  when 'json'
47
- original_render :text => Rgviz::JsRenderer.render_error('invalid_query', e.message)
47
+ original_render :text => Rgviz::JsRenderer.render_error('invalid_query', e.message, tqx)
48
48
  when 'html'
49
49
  original_render :text => "<b>Error:</b> #{e.message}"
50
50
  when 'csv'
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: 51
4
+ hash: 49
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 28
9
- version: "0.28"
8
+ - 29
9
+ version: "0.29"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ary Borenszweig
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-19 00:00:00 +07:00
17
+ date: 2010-08-20 00:00:00 +07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20