gon 5.1.2 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of gon might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +34 -0
- data/lib/gon.rb +1 -1
- data/lib/gon/base.rb +32 -3
- data/lib/gon/helpers.rb +4 -0
- data/lib/gon/spec_helpers.rb +5 -2
- data/lib/gon/version.rb +1 -1
- data/lib/gon/watch.rb +4 -0
- data/spec/gon/basic_spec.rb +100 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6490edc958229669a41401931a59c79cbb3826f
|
4
|
+
data.tar.gz: 9e0337987644e7f2bd4c80456a1a69da673ad640
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba65303d31b34f7d2b9bad0d775efb210fb0eba67f30e80190639736773e4a773ed072a4276795c3271507b7f08079f99ad8c5f9b0159eb46c496c59eb768a34
|
7
|
+
data.tar.gz: f543c36fea096b81de1c089fa9af7c1f8845eced67410485b9a345569aa9d181c8551e5a24f6c91866e1a16022b240e24cb1a8ced93b089c2840c2920161eeb8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 5.2.0
|
4
|
+
|
5
|
+
* fix issue where include_gon would raise exception if the controller did not assign any gon variables. Thanks to @asalme
|
6
|
+
* namespace_check option. Thanks to @tommyh
|
7
|
+
* Only inject gon into ActionController::Base-like object in spec_helper. Thanks to @kevinoconnor7
|
8
|
+
* AMD compatible version of including gon. Thanks to @vijoc
|
9
|
+
|
10
|
+
## 5.1.2
|
11
|
+
|
12
|
+
* Clarifying helpers, dump gon#watch content to safe json before render. Thanks to @Strech
|
13
|
+
|
14
|
+
## 5.1.1
|
15
|
+
|
16
|
+
* global_root option. Thanks to @rafaelliu
|
17
|
+
* MultiJson support. Thanks to @Strech
|
18
|
+
|
3
19
|
## 5.1.0
|
4
20
|
|
5
21
|
* Many fixes https://github.com/gazay/gon/compare/91845f3f0debd0cb8fa569aad65f5dc40a7e28e5...8dc7400fbb83ba5a086bd36c76342a393690d53f
|
data/README.md
CHANGED
@@ -110,6 +110,40 @@ alert(gon.your_array)
|
|
110
110
|
alert(gon.your_hash)
|
111
111
|
```
|
112
112
|
|
113
|
+
### AMD compatible version: `include_gon_amd`
|
114
|
+
|
115
|
+
If your site uses AMD modules you can use the `include_gon_amd` helper to
|
116
|
+
include the variables and watch function as a module. Options are mostly
|
117
|
+
the same as for `include_gon`, except for `namespace_check`, which does
|
118
|
+
nothing and `namespace`, which is used as the name of the defined module.
|
119
|
+
The end result will look somewhat like the following:
|
120
|
+
|
121
|
+
```js
|
122
|
+
define('yourNameSpace', [], function() {
|
123
|
+
var gon = {};
|
124
|
+
gon.yourVariable = yourValue;
|
125
|
+
// etc...
|
126
|
+
|
127
|
+
return gon;
|
128
|
+
});
|
129
|
+
```
|
130
|
+
|
131
|
+
A (very) simplified usage example:
|
132
|
+
|
133
|
+
`app/views/layouts/application.html.erb`
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
include_gon_amd namespace: 'data'
|
137
|
+
```
|
138
|
+
|
139
|
+
`Some JavaScript module`
|
140
|
+
|
141
|
+
```js
|
142
|
+
define(['data'], function(data) {
|
143
|
+
alert(data.myVariable);
|
144
|
+
});
|
145
|
+
```
|
146
|
+
|
113
147
|
## gon.watch - renew your data easily!
|
114
148
|
|
115
149
|
You can use gon for renewing your data without reloading pages and
|
data/lib/gon.rb
CHANGED
data/lib/gon/base.rb
CHANGED
@@ -5,8 +5,8 @@ class Gon
|
|
5
5
|
class << self
|
6
6
|
|
7
7
|
def render_data(options)
|
8
|
-
namespace, tag, cameled, camel_depth, watch, type, cdata, global_root = parse_options(options)
|
9
|
-
script = "window.#{namespace}={};"
|
8
|
+
namespace, tag, cameled, camel_depth, watch, type, cdata, global_root, namespace_check = parse_options(options)
|
9
|
+
script = namespace_check ? "window.#{namespace}=window.#{namespace}||{};" : "window.#{namespace}={};"
|
10
10
|
|
11
11
|
script << formatted_data(namespace, cameled, camel_depth, watch, global_root)
|
12
12
|
script = Gon::Escaper.escape_unicode(script)
|
@@ -15,6 +15,19 @@ class Gon
|
|
15
15
|
script.html_safe
|
16
16
|
end
|
17
17
|
|
18
|
+
def render_data_amd(options)
|
19
|
+
namespace, tag, cameled, camel_depth, watch, type, cdata, global_root = parse_options(options)
|
20
|
+
|
21
|
+
script = "define('#{namespace}',[],function(){"
|
22
|
+
script << amd_formatted_data(namespace, cameled, camel_depth, watch, global_root)
|
23
|
+
script << 'return gon;});'
|
24
|
+
|
25
|
+
script = Gon::Escaper.escape_unicode(script)
|
26
|
+
script = Gon::Escaper.javascript_tag(script, type, cdata) if tag
|
27
|
+
|
28
|
+
script.html_safe
|
29
|
+
end
|
30
|
+
|
18
31
|
def get_controller(options = {})
|
19
32
|
options[:controller] ||
|
20
33
|
(
|
@@ -56,8 +69,9 @@ class Gon
|
|
56
69
|
type = options[:type].nil? || options[:type]
|
57
70
|
cdata = options[:cdata].nil? || options[:cdata]
|
58
71
|
global_root = options.has_key?(:global_root) ? options[:global_root] : 'global'
|
72
|
+
namespace_check = options.has_key?(:namespace_check) ? options[:namespace_check] : false
|
59
73
|
|
60
|
-
[namespace, tag, cameled, camel_depth, watch, type, cdata, global_root]
|
74
|
+
[namespace, tag, cameled, camel_depth, watch, type, cdata, global_root, namespace_check]
|
61
75
|
end
|
62
76
|
|
63
77
|
def formatted_data(namespace, keys_cameled, camel_depth, watch, global_root)
|
@@ -75,6 +89,21 @@ class Gon
|
|
75
89
|
script
|
76
90
|
end
|
77
91
|
|
92
|
+
def amd_formatted_data(namespace, keys_cameled, camel_depth, watch, global_root)
|
93
|
+
script = 'var gon={};'
|
94
|
+
|
95
|
+
gon_variables(global_root).each do |key, val|
|
96
|
+
js_key = keys_cameled ? key.to_s.camelize(:lower) : key.to_s
|
97
|
+
script << "gon['#{js_key}']=#{to_json(val, camel_depth)};"
|
98
|
+
end
|
99
|
+
|
100
|
+
if watch and Gon::Watch.all_variables.present?
|
101
|
+
script << Gon.watch.render_amd
|
102
|
+
end
|
103
|
+
|
104
|
+
script
|
105
|
+
end
|
106
|
+
|
78
107
|
def to_json(value, camel_depth)
|
79
108
|
# starts at 2 because 1 is the root key which is converted in the formatted_data method
|
80
109
|
Gon::JsonDumper.dump convert_hash_keys(value, 2, camel_depth)
|
data/lib/gon/helpers.rb
CHANGED
data/lib/gon/spec_helpers.rb
CHANGED
@@ -7,8 +7,11 @@ class Gon
|
|
7
7
|
module GonSession
|
8
8
|
def process(*)
|
9
9
|
# preload threadlocal & store controller instance
|
10
|
-
controller.
|
11
|
-
|
10
|
+
if controller.is_a? ActionController::Base
|
11
|
+
controller.gon
|
12
|
+
Gon.send(:current_gon).env[Gon::Base::ENV_CONTROLLER_KEY] =
|
13
|
+
controller
|
14
|
+
end
|
12
15
|
super
|
13
16
|
end
|
14
17
|
end
|
data/lib/gon/version.rb
CHANGED
data/lib/gon/watch.rb
CHANGED
@@ -8,6 +8,10 @@ class Gon
|
|
8
8
|
JS_FUNCTION + "window.gon.watchedVariables=#{Gon::JsonDumper.dump all_variables};"
|
9
9
|
end
|
10
10
|
|
11
|
+
def render_amd
|
12
|
+
JS_FUNCTION + "gon.watchedVariables=#{Gon::JsonDumper.dump all_variables};"
|
13
|
+
end
|
14
|
+
|
11
15
|
def all_variables
|
12
16
|
@watch_variables || {}
|
13
17
|
end
|
data/spec/gon/basic_spec.rb
CHANGED
@@ -208,6 +208,98 @@ describe Gon do
|
|
208
208
|
)
|
209
209
|
end
|
210
210
|
|
211
|
+
it 'outputs correct js with namespace check' do
|
212
|
+
expect(@base.include_gon(namespace_check: true)).to eq( \
|
213
|
+
'<script type="text/javascript">' +
|
214
|
+
"\n//<![CDATA[\n" +
|
215
|
+
'window.gon=window.gon||{};'\
|
216
|
+
"\n//]]>\n" +
|
217
|
+
'</script>'
|
218
|
+
)
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'outputs correct js without namespace check' do
|
222
|
+
expect(@base.include_gon(namespace_check: false)).to eq( \
|
223
|
+
'<script type="text/javascript">' +
|
224
|
+
"\n//<![CDATA[\n" +
|
225
|
+
'window.gon={};'\
|
226
|
+
"\n//]]>\n" +
|
227
|
+
'</script>'
|
228
|
+
)
|
229
|
+
end
|
230
|
+
|
231
|
+
context "without a current_gon instance" do
|
232
|
+
|
233
|
+
before(:each) do
|
234
|
+
RequestStore.store[:gon] = nil
|
235
|
+
allow(Gon).to receive(:current_gon).and_return(nil)
|
236
|
+
end
|
237
|
+
|
238
|
+
it "does not raise an exception" do
|
239
|
+
expect { @base.include_gon }.to_not raise_error(Exception)
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'outputs correct js' do
|
243
|
+
expect(@base.include_gon).to eq("")
|
244
|
+
end
|
245
|
+
|
246
|
+
it 'outputs correct js with init' do
|
247
|
+
expect(@base.include_gon(init: true)).to eq( \
|
248
|
+
'<script type="text/javascript">' +
|
249
|
+
"\n//<![CDATA[\n" +
|
250
|
+
'window.gon={};'\
|
251
|
+
"\n//]]>\n" +
|
252
|
+
'</script>'
|
253
|
+
)
|
254
|
+
end
|
255
|
+
|
256
|
+
end
|
257
|
+
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
describe '#include_gon_amd' do
|
262
|
+
|
263
|
+
before(:each) do
|
264
|
+
Gon.clear
|
265
|
+
Gon::Request.
|
266
|
+
instance_variable_set(:@request_id, request.object_id)
|
267
|
+
@base = ActionView::Base.new
|
268
|
+
@base.request = request
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'is included in ActionView::Base as a helper' do
|
272
|
+
expect(ActionView::Base.
|
273
|
+
instance_methods.
|
274
|
+
map(&:to_s).
|
275
|
+
include?('include_gon_amd')).to eq(true)
|
276
|
+
end
|
277
|
+
|
278
|
+
it 'outputs correct js without variables' do
|
279
|
+
expect(@base.include_gon_amd).to eq( wrap_script( \
|
280
|
+
'define(\'gon\',[],function(){'+
|
281
|
+
'var gon={};return gon;'+
|
282
|
+
'});')
|
283
|
+
)
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'outputs correct js with an integer' do
|
287
|
+
Gon.int = 1
|
288
|
+
|
289
|
+
expect(@base.include_gon_amd).to eq( wrap_script(
|
290
|
+
'define(\'gon\',[],function(){'+
|
291
|
+
'var gon={};gon[\'int\']=1;return gon;'+
|
292
|
+
'});')
|
293
|
+
)
|
294
|
+
end
|
295
|
+
|
296
|
+
it 'outputs correct module name when given a namespace' do
|
297
|
+
expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script(
|
298
|
+
'define(\'data\',[],function(){'+
|
299
|
+
'var gon={};return gon;'+
|
300
|
+
'});')
|
301
|
+
)
|
302
|
+
end
|
211
303
|
end
|
212
304
|
|
213
305
|
it 'returns exception if try to set public method as variable' do
|
@@ -239,4 +331,12 @@ describe Gon do
|
|
239
331
|
@request ||= double 'request', :env => {}
|
240
332
|
end
|
241
333
|
|
334
|
+
def wrap_script(content, type='text/javascript', cdata=true)
|
335
|
+
script = "<script type=\"#{type}\">"
|
336
|
+
script << "\n//<![CDATA[\n" if cdata
|
337
|
+
script << content
|
338
|
+
script << "\n//]]>\n" if cdata
|
339
|
+
script << '</script>'
|
340
|
+
end
|
341
|
+
|
242
342
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gazay
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|