Skip to content

Commit 8d8aab3

Browse files
committed
A litlle bit refactoring
1 parent 319aef9 commit 8d8aab3

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

gon.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ Gem::Specification.new do |s|
2424
s.add_development_dependency "rspec"
2525
s.add_development_dependency "jbuilder"
2626
s.add_development_dependency "rake"
27-
s.add_development_dependency "debugger"
2827
end

lib/gon/base.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,20 @@ def render_data(options)
77
if Gon.global.all_variables.present?
88
data[:global] = Gon.global.all_variables
99
end
10-
namespace = options[:namespace] || 'gon'
11-
need_tag = options[:need_tag].nil? || options[:need_tag]
12-
13-
need_type = !options[:need_type].nil? || options[:need_type]
14-
need_type ? type ='<script type="text/javascript">' : type = '<script>'
15-
16-
start = "#{need_tag ? type : ''}window.#{namespace} = {};"
10+
namespace, tag, cameled = parse_options options
11+
start = "#{tag if tag}window.#{namespace} = {};"
1712
script = ''
1813

19-
if data.present?
20-
if options[:camel_case]
21-
data.each do |key, val|
22-
script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
23-
end
14+
data.each do |key, val|
15+
if cameled
16+
script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
2417
else
25-
data.each do |key, val|
26-
script << "#{namespace}.#{key.to_s}=#{val.to_json};"
27-
end
18+
script << "#{namespace}.#{key.to_s}=#{val.to_json};"
2819
end
2920
end
3021

3122
script = start + Gon::Escaper.escape(script)
32-
script << '</script>' if need_tag
23+
script << '</script>' if tag
3324
script.html_safe
3425
end
3526

@@ -57,6 +48,16 @@ def get_template_path(options, extension)
5748

5849
private
5950

51+
def parse_options(options)
52+
namespace = options[:namespace] || 'gon'
53+
need_tag = options[:need_tag].nil? || options[:need_tag]
54+
need_type = options[:need_type].present? && options[:need_type]
55+
cameled = options[:camel_case]
56+
tag = need_tag && (need_type ? '<script type="text/javascript">' : '<script>')
57+
58+
[namespace, tag, cameled]
59+
end
60+
6061
def right_extension?(extension, template_path)
6162
File.extname(template_path) == ".#{extension}"
6263
end

0 commit comments

Comments
 (0)