Skip to content

Commit c2998e3

Browse files
committed
Added global functionality with specs
1 parent 0c71e1f commit c2998e3

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

lib/gon/base.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module Base
33
class << self
44

55
def render_data(options)
6-
data = Gon.all_variables.merge(Gon.global.all_variables)
6+
if Gon.global.all_variables.present?
7+
Gon::Request.gon['global'] = Gon.global.all_variables
8+
end
9+
data = Gon.all_variables
710
namespace = options[:namespace] || 'gon'
811
start = '<script>window.' + namespace + ' = {};'
912
script = ''
@@ -24,8 +27,8 @@ def render_data(options)
2427

2528
def get_controller(options)
2629
options[:controller] ||
27-
@request_env['action_controller.instance'] ||
28-
@request_env['action_controller.rescue.response'].
30+
Gon::Request.env['action_controller.instance'] ||
31+
Gon::Request.env['action_controller.rescue.response'].
2932
instance_variable_get('@template').
3033
instance_variable_get('@controller')
3134
end

lib/gon/global.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def jbuilder(*args)
3131
store_builder_data 'jbuilder', data, options
3232
end
3333

34+
def clear
35+
@global_vars = {}
36+
end
37+
3438
def inspect
3539
'Gon'
3640
end

lib/gon/helpers.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module InstanceMethods
1010
def include_gon(options = {})
1111
if variables_for_request_present?
1212
Gon::Base.render_data(options)
13+
elsif Gon.global.all_variables.present?
14+
Gon.clear
15+
Gon::Base.render_data(options)
1316
else
1417
""
1518
end

spec/gon/global_spec.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
require 'gon'
2+
3+
describe Gon::Global do
4+
5+
before(:each) do
6+
Gon::Global.clear
7+
end
8+
9+
describe '#all_variables' do
10+
11+
it 'returns all variables in hash' do
12+
Gon.global.a = 1
13+
Gon.global.b = 2
14+
Gon.global.c = Gon.global.a + Gon.global.b
15+
Gon.global.c.should == 3
16+
Gon.global.all_variables.should == {'a' => 1, 'b' => 2, 'c' => 3}
17+
end
18+
19+
it 'supports all data types' do
20+
Gon.global.clear
21+
Gon.global.int = 1
22+
Gon.global.float = 1.1
23+
Gon.global.string = 'string'
24+
Gon.global.array = [ 1, 'string' ]
25+
Gon.global.hash_var = { :a => 1, :b => '2'}
26+
Gon.global.hash_w_array = { :a => [ 2, 3 ] }
27+
Gon.global.klass = Hash
28+
end
29+
30+
end
31+
32+
describe '#include_gon' do
33+
34+
before(:each) do
35+
Gon.clear
36+
Gon.global.clear
37+
ActionView::Base.
38+
instance_methods.
39+
map(&:to_s).
40+
include?('include_gon').should == true
41+
@base = ActionView::Base.new
42+
@base.request = request
43+
end
44+
45+
it 'outputs correct js with an integer' do
46+
Gon.global.int = 1
47+
@base.include_gon.should == "<script>window.gon = {};" +
48+
"gon.global={\"int\":1};" +
49+
"</script>"
50+
end
51+
52+
it 'outputs correct js with an integer and integer in Gon' do
53+
Gon::Request.
54+
instance_variable_set(:@request_id, request.object_id)
55+
Gon.int = 1
56+
Gon.global.int = 1
57+
@base.include_gon.should == "<script>window.gon = {};" +
58+
"gon.int=1;" +
59+
"gon.global={\"int\":1};" +
60+
"</script>"
61+
end
62+
63+
it 'outputs correct js with a string' do
64+
Gon.global.str = %q(a'b"c)
65+
@base.include_gon.should == "<script>window.gon = {};" +
66+
"gon.global={\"str\":\"a'b\\\"c\"};" +
67+
"</script>"
68+
end
69+
70+
it 'outputs correct js with a script string' do
71+
Gon.global.str = %q(</script><script>alert('!')</script>)
72+
@base.include_gon.should == "<script>window.gon = {};" +
73+
"gon.global={\"str\":\"<\\/script><script>alert('!')<\\/script>\"};" +
74+
"</script>"
75+
end
76+
77+
end
78+
79+
it 'returns exception if try to set public method as variable' do
80+
Gon.global.clear
81+
lambda { Gon.global.all_variables = 123 }.should raise_error
82+
lambda { Gon.global.rabl = 123 }.should raise_error
83+
end
84+
85+
it 'works fine with rabl and jbuilder' do
86+
pending
87+
end
88+
89+
def request
90+
@request ||= double 'request', :env => {}
91+
end
92+
93+
end

0 commit comments

Comments
 (0)