File tree Expand file tree Collapse file tree 5 files changed +36
-5
lines changed
Expand file tree Collapse file tree 5 files changed +36
-5
lines changed Original file line number Diff line number Diff line change 11# CHANGELOG
22
3+ ## 3.0.2
4+
5+ * Added need_tag option
6+
37## 3.0.0
48
59 * Almost all code refactored
Original file line number Diff line number Diff line change @@ -82,6 +82,16 @@ You can change the namespace of the variables:
8282 ...
8383` ` `
8484
85+ You can get json without script tag (kudos to @afa ):
86+
87+ ` ` ` erb
88+ <head>
89+ <title>some title</title>
90+ <script><%= include_gon(:need_tag => false) %></script>
91+ <!-- include your action js code with 'serverExports' namespace -->
92+ ...
93+ ` ` `
94+
8595You put something like this in the action of your controller:
8696
8797` ` ` ruby
@@ -329,13 +339,13 @@ Thats it!
329339Puts this line into ` Gemfile ` then run ` $ bundle ` :
330340
331341``` ruby
332- gem ' gon' , ' 3.0.0 '
342+ gem ' gon' , ' 3.0.2 '
333343```
334344
335345Or if you are old-school Rails 2 developer put this into ` config/environment.rb ` and run ` $ rake gems:install ` :
336346
337347``` ruby
338- config.gem ' gon' , :version => ' 3.0.0 '
348+ config.gem ' gon' , :version => ' 3.0.2 '
339349```
340350
341351Or manually install gon gem: ` $ gem install gon `
Original file line number Diff line number Diff line change @@ -8,7 +8,8 @@ def render_data(options)
88 end
99 data = Gon . all_variables
1010 namespace = options [ :namespace ] || 'gon'
11- start = '<script>window.' + namespace + ' = {};'
11+ need_tag = options [ :need_tag ] . nil? || options [ :need_tag ]
12+ start = "#{ need_tag ? '<script>' : '' } window.#{ namespace } = {};"
1213 script = ''
1314
1415 if options [ :camel_case ]
@@ -21,7 +22,8 @@ def render_data(options)
2122 end
2223 end
2324
24- script = start + Gon ::Escaper . escape ( script ) + '</script>'
25+ script = start + Gon ::Escaper . escape ( script )
26+ script << '</script>' if need_tag
2527 script . html_safe
2628 end
2729
Original file line number Diff line number Diff line change 11module Gon
2- VERSION = '3.0.0 '
2+ VERSION = '3.0.2 '
33end
Original file line number Diff line number Diff line change 6464 '</script>'
6565 end
6666
67+ it 'outputs correct js with an integer, camel-case and namespace' do
68+ Gon . int_cased = 1
69+ @base . include_gon ( camel_case : true , namespace : 'camel_cased' ) . should == \
70+ '<script>window.camel_cased = {};' +
71+ 'camel_cased.intCased=1;' +
72+ '</script>'
73+ end
74+
75+ it 'outputs correct js with an integer and without tag' do
76+ Gon . int = 1
77+ @base . include_gon ( need_tag : false ) . should == \
78+ 'window.gon = {};' +
79+ 'gon.int=1;'
80+ end
81+
6782 end
6883
6984 it 'returns exception if try to set public method as variable' do
You can’t perform that action at this time.
0 commit comments