google_visualr 2.2.0 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c9e5ef85f15735729d999bbc550dc078ce306a82
4
- data.tar.gz: d00153d2e742e7f679114ce0a676dc6030f40149
3
+ metadata.gz: c619ef5e478b69cae31b6633f12bd0538ae28d1f
4
+ data.tar.gz: c8b933d1ab6b6fd1f8d6ffee52d510b4f63ff5d8
5
5
  SHA512:
6
- metadata.gz: 8a2a3b6c8d47e5692030c241dc60a2fe9bd649fca44a4e0026b05d895bdbaf1a5dfd5e1b35c1dc6c3e593d26162c976f307ff92cdc8dc115ac303696399757b8
7
- data.tar.gz: f8bd40ffd09089f9eb9cb1809dd3f8f55c6dd7b10c61a1bcf2c3f11be6647484383efadaab389e5e88fcd39e91f57db08cf4296173caa4c3e036d8f4e4527453
6
+ metadata.gz: 0709add89f814b1af09c975333cfe3beacda8dd989e300388d341d5c7a14de18198265c55cdf8e7d85b50dc8636cb9909b4bd786f5dbfaf49f7e44e116ce0e63
7
+ data.tar.gz: b19ad8a044a5a91916d51c745d4f1b42b54e6dfb1851b1f59be2c9aef924f64d5d775929754e9d0e5c2ce898f87c92c576f9d0efa78e95009439cf211a501239
data/README.markdown CHANGED
@@ -88,6 +88,11 @@ I would like to collect some data about who's using this Gem. [Please drop me a
88
88
 
89
89
  ## Change Log
90
90
 
91
+ <em>Version 2.3.0</em>
92
+
93
+ * [Issue 69](https://github.com/winston/google_visualr/pull/69) Support generating chart Javascript without <script> tag
94
+ * Split `base_chart#to_js` into 3 methods - `to_js`, `load_js` and `draw_js` which can be used on their own.
95
+
91
96
  <em>Version 2.2.0</em>
92
97
 
93
98
  * [Issue 64](https://github.com/winston/google_visualr/pull/64) Works with Turbolinks.
@@ -1,21 +1,23 @@
1
1
  module GoogleVisualr
2
-
3
2
  module Rails
4
-
5
3
  module ViewHelper
6
-
7
4
  extend ActiveSupport::Concern
8
5
 
9
6
  included do
10
7
  helper_method "render_chart"
11
8
  end
12
9
 
13
- def render_chart(chart, dom)
14
- chart.to_js(dom).html_safe
10
+ def render_chart(chart, dom, options={})
11
+ script_tag = options.fetch(:script_tag) { true }
12
+ if script_tag
13
+ chart.to_js(dom).html_safe
14
+ else
15
+ html = ""
16
+ html << chart.load_js(dom)
17
+ html << chart.draw_js(dom)
18
+ html.html_safe
19
+ end
15
20
  end
16
-
17
21
  end
18
-
19
22
  end
20
-
21
23
  end
@@ -36,10 +36,29 @@ module GoogleVisualr
36
36
  #
37
37
  # Parameters:
38
38
  # *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
39
-
40
39
  def to_js(element_id)
41
- js = "\n<script type='text/javascript'>"
42
- js << "\n google.load('visualization','1', {packages: ['#{package_name}'], callback: #{chart_function_name(element_id)}});"
40
+ js = ""
41
+ js << "\n<script type='text/javascript'>"
42
+ js << load_js(element_id)
43
+ js << draw_js(element_id)
44
+ js << "\n</script>"
45
+ js
46
+ end
47
+
48
+ # Generates JavaScript for loading the appropriate Google Visualization package, with callback to render chart.
49
+ #
50
+ # Parameters:
51
+ # *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
52
+ def load_js(element_id)
53
+ "\n google.load('visualization','1', {packages: ['#{package_name}'], callback: #{chart_function_name(element_id)}});"
54
+ end
55
+
56
+ # Generates JavaScript function for rendering the chart.
57
+ #
58
+ # Parameters:
59
+ # *div_id [Required] The ID of the DIV element that the Google Chart should be rendered in.
60
+ def draw_js(element_id)
61
+ js = ""
43
62
  js << "\n function #{chart_function_name(element_id)}() {"
44
63
  js << "\n #{@data_table.to_js}"
45
64
  js << "\n var chart = new google.visualization.#{chart_name}(document.getElementById('#{element_id}'));"
@@ -48,7 +67,6 @@ module GoogleVisualr
48
67
  end
49
68
  js << "\n chart.draw(data_table, #{js_parameters(@options)});"
50
69
  js << "\n };"
51
- js << "\n</script>"
52
70
  js
53
71
  end
54
72
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleVisualr
2
- VERSION = "2.2.0"
2
+ VERSION = "2.3.0"
3
3
  end
@@ -1,9 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "ApplicationController" do
4
-
5
4
  describe "#render_chart" do
6
-
7
5
  it "has method" do
8
6
  ApplicationController.instance_methods.should include :render_chart
9
7
  end
@@ -18,6 +16,10 @@ describe "ApplicationController" do
18
16
  js.should == base_chart_js("div_chart")
19
17
  end
20
18
 
19
+ it "returns html_safe javascript without script tag" do
20
+ controller = ApplicationController.new
21
+ js = controller.render_chart base_chart, "div_chart", script_tag: false
22
+ js.should == base_chart_js_without_script_tag("div_chart")
23
+ end
21
24
  end
22
-
23
25
  end
@@ -48,6 +48,7 @@ describe GoogleVisualr::BaseChart do
48
48
  it "generates JS" do
49
49
  js = @chart.to_js("body")
50
50
  js.should == base_chart_js("body")
51
+ js.should include("<script")
51
52
  end
52
53
 
53
54
  it "generates JS with listeners" do
@@ -209,14 +209,14 @@ describe GoogleVisualr::DataTable do
209
209
  it "accepts BigDecimal as number" do
210
210
  expect {
211
211
  dt.set_cell(0, 1, BigDecimal.new(42))
212
- }.to_not raise_exception(ArgumentError)
212
+ }.to_not raise_exception
213
213
  end
214
214
  end
215
215
 
216
216
  it "accepts 'nil' for all column types" do
217
217
  expect {
218
218
  dt.set_cell(0, 0, nil)
219
- }.to_not raise_exception(ArgumentError)
219
+ }.to_not raise_exception
220
220
  end
221
221
  end
222
222
 
@@ -17,18 +17,23 @@ def base_chart(data_table=data_table)
17
17
  GoogleVisualr::BaseChart.new(data_table, { :legend => "Test Chart", :width => 800, :is3D => true })
18
18
  end
19
19
 
20
- def base_chart_js(div_class="div_class")
21
- js = "\n<script type='text/javascript'>"
22
- js << "\n google.load('visualization','1', {packages: ['basechart'], callback: draw_#{div_class}});"
20
+
21
+ def base_chart_js_without_script_tag(div_class="div_class")
22
+ js = "\n google.load('visualization','1', {packages: ['basechart'], callback: draw_#{div_class}});"
23
23
  js << "\n function draw_#{div_class}() {"
24
24
  js << "\n var data_table = new google.visualization.DataTable();data_table.addColumn({\"type\":\"string\",\"label\":\"Year\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Sales\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Expenses\"});data_table.addRow([{v: \"2004\"}, {v: 1000}, {v: 400}]);data_table.addRow([{v: \"2005\"}, {v: 1200}, {v: 450}]);data_table.addRow([{v: \"2006\"}, {v: 1500}, {v: 600}]);data_table.addRow([{v: \"2007\"}, {v: 800}, {v: 500}]);\n var chart = new google.visualization.BaseChart(document.getElementById('#{div_class}'));"
25
25
  js << "\n chart.draw(data_table, {legend: \"Test Chart\", width: 800, is3D: true});"
26
26
  js << "\n };"
27
+ end
28
+
29
+ def base_chart_js(div_class="div_class")
30
+ js = "\n<script type='text/javascript'>"
31
+ js << base_chart_js_without_script_tag(div_class)
27
32
  js << "\n</script>"
28
33
  end
29
34
 
30
35
  def base_chart_with_listener_js(div_class="div_class")
31
- js = "\n<script type='text/javascript'>"
36
+ js = "\n<script type='text/javascript'>"
32
37
  js << "\n google.load('visualization','1', {packages: ['basechart'], callback: draw_#{div_class}});"
33
38
  js << "\n function draw_#{div_class}() {"
34
39
  js << "\n var data_table = new google.visualization.DataTable();data_table.addColumn({\"type\":\"string\",\"label\":\"Year\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Sales\"});data_table.addColumn({\"type\":\"number\",\"label\":\"Expenses\"});data_table.addRow([{v: \"2004\"}, {v: 1000}, {v: 400}]);data_table.addRow([{v: \"2005\"}, {v: 1200}, {v: 450}]);data_table.addRow([{v: \"2006\"}, {v: 1500}, {v: 600}]);data_table.addRow([{v: \"2007\"}, {v: 800}, {v: 500}]);\n var chart = new google.visualization.BaseChart(document.getElementById('#{div_class}'));"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_visualr
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Winston Teo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-06 00:00:00.000000000 Z
11
+ date: 2014-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler