Skip to content

Commit 015a8eb

Browse files
committed
date validation if init and data blank
1 parent 9bd3d78 commit 015a8eb

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/gon/base.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ def render_data(options)
1212
start = "#{need_tag ? '<script>' : ''}window.#{namespace} = {};"
1313
script = ''
1414

15-
if options[:camel_case]
16-
data.each do |key, val|
17-
script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
18-
end
19-
else
20-
data.each do |key, val|
21-
script << "#{namespace}.#{key.to_s}=#{val.to_json};"
15+
if data.present?
16+
if options[:camel_case]
17+
data.each do |key, val|
18+
script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
19+
end
20+
else
21+
data.each do |key, val|
22+
script << "#{namespace}.#{key.to_s}=#{val.to_json};"
23+
end
2224
end
2325
end
2426

spec/gon/basic_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,14 @@
8181

8282

8383
it 'outputs correct js without variables, without tag and gon init' do
84+
@base.include_gon(need_tag: false, init: true).should == 'window.gon = {};'
85+
end
86+
87+
it 'outputs correct js without variables, without tag and gon init' do
88+
Gon.int = 1
8489
@base.include_gon(need_tag: false, init: true).should == \
85-
'window.gon = {};'
90+
'window.gon = {};' +
91+
'gon.int=1;'
8692
end
8793

8894
end

0 commit comments

Comments
 (0)