gon 2.1.0 → 2.1.2
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.
- data/.travis.yml +11 -0
- data/README.md +7 -3
- data/lib/gon.rb +6 -5
- data/lib/gon/escaper.rb +20 -0
- data/lib/gon/helpers.rb +10 -4
- data/lib/gon/version.rb +1 -1
- data/spec/gon/gon_spec.rb +31 -8
- metadata +63 -95
data/.travis.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.2
|
5
|
+
- 1.9.3
|
6
|
+
- jruby-18mode # JRuby in 1.8 mode
|
7
|
+
- jruby-19mode # JRuby in 1.9 mode
|
8
|
+
- rbx-18mode
|
9
|
+
- rbx-19mode
|
10
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
11
|
+
# script: bundle exec rspec spec
|
data/README.md
CHANGED
@@ -3,12 +3,16 @@
|
|
3
3
|
![Gon. You should try this. If you look closer - you will see an elephant.](https://github.com/gazay/gon/raw/master/doc/logo_small.png)
|
4
4
|
|
5
5
|
|
6
|
+
### Build Status ![http://travis-ci.org/gazay/gon](https://secure.travis-ci.org/gazay/gon.png)
|
7
|
+
|
6
8
|
If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!
|
7
9
|
|
8
10
|
Now with [Jbuilder](https://github.com/rails/jbuilder) and [Rabl](https://github.com/nesquena/rabl) support!
|
9
11
|
|
10
12
|
For Sinatra available [gon-sinatra](https://github.com/gazay/gon-sinatra).
|
11
13
|
|
14
|
+
For .Net MVC available port [NGon](https://github.com/brooklynDev/NGon).
|
15
|
+
|
12
16
|
## An example of typical use
|
13
17
|
|
14
18
|
### Very good and detailed example and reasons to use is considered in [railscast](http://railscasts.com/episodes/324-passing-data-to-javascript) by Ryan Bates
|
@@ -22,7 +26,7 @@ you might be doing something like this:
|
|
22
26
|
3. Then there can be two ways in js:
|
23
27
|
+ if you previously wrote data in data
|
24
28
|
attributes - you should parse this attributes and write data to some
|
25
|
-
js variable.
|
29
|
+
js variable.
|
26
30
|
+ if you wrote js right in view (many frontenders would shame you for
|
27
31
|
that) - you just use data from this js - OK.
|
28
32
|
4. You can use your data in your js
|
@@ -283,13 +287,13 @@ wouldn't work. You can read about this in common usage above.
|
|
283
287
|
Puts this line into `Gemfile` then run `$ bundle`:
|
284
288
|
|
285
289
|
``` ruby
|
286
|
-
gem 'gon', '2.
|
290
|
+
gem 'gon', '2.1.2'
|
287
291
|
```
|
288
292
|
|
289
293
|
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
|
290
294
|
|
291
295
|
``` ruby
|
292
|
-
config.gem 'gon', :version => '2.
|
296
|
+
config.gem 'gon', :version => '2.1.2'
|
293
297
|
```
|
294
298
|
|
295
299
|
Or manually install gon gem: `$ gem install gon`
|
data/lib/gon.rb
CHANGED
@@ -4,6 +4,7 @@ end
|
|
4
4
|
require 'action_view'
|
5
5
|
require 'action_controller'
|
6
6
|
require 'gon/helpers'
|
7
|
+
require 'gon/escaper'
|
7
8
|
if defined?(Rabl)
|
8
9
|
require 'gon/rabl'
|
9
10
|
end
|
@@ -15,7 +16,7 @@ module Gon
|
|
15
16
|
class << self
|
16
17
|
|
17
18
|
def all_variables
|
18
|
-
@request_env[
|
19
|
+
@request_env['gon']
|
19
20
|
end
|
20
21
|
|
21
22
|
def all_variables=(values)
|
@@ -23,12 +24,12 @@ module Gon
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def clear
|
26
|
-
@request_env[
|
27
|
+
@request_env['gon'] = {}
|
27
28
|
end
|
28
29
|
|
29
30
|
def request_env=(environment)
|
30
31
|
@request_env = environment
|
31
|
-
@request_env[
|
32
|
+
@request_env['gon'] ||= {}
|
32
33
|
end
|
33
34
|
|
34
35
|
def request_env
|
@@ -57,11 +58,11 @@ module Gon
|
|
57
58
|
end
|
58
59
|
|
59
60
|
def get_variable(name)
|
60
|
-
@request_env[
|
61
|
+
@request_env['gon'][name]
|
61
62
|
end
|
62
63
|
|
63
64
|
def set_variable(name, value)
|
64
|
-
@request_env[
|
65
|
+
@request_env['gon'][name] = value
|
65
66
|
end
|
66
67
|
|
67
68
|
# TODO: Remove required argument view_path, and by default use current action
|
data/lib/gon/escaper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Gon
|
2
|
+
module Escaper
|
3
|
+
class << self
|
4
|
+
|
5
|
+
GON_JS_ESCAPE_MAP = {
|
6
|
+
'</' => '<\/'
|
7
|
+
}
|
8
|
+
|
9
|
+
def escape(javascript)
|
10
|
+
if javascript
|
11
|
+
result = javascript.gsub(/(<\/)/u) {|match| GON_JS_ESCAPE_MAP[match] }
|
12
|
+
javascript.html_safe? ? result.html_safe : result
|
13
|
+
else
|
14
|
+
''
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/gon/helpers.rb
CHANGED
@@ -6,11 +6,13 @@ module Gon
|
|
6
6
|
end
|
7
7
|
|
8
8
|
module InstanceMethods
|
9
|
+
|
9
10
|
def include_gon(options = {})
|
10
|
-
if Gon.request_env && Gon.all_variables.present?
|
11
|
+
if Gon.request_env && Gon.all_variables.present? && Gon.request == request.object_id
|
11
12
|
data = Gon.all_variables
|
12
13
|
namespace = options[:namespace] || 'gon'
|
13
|
-
|
14
|
+
start = '<script>window.' + namespace + ' = {};'
|
15
|
+
script = ''
|
14
16
|
if options[:camel_case]
|
15
17
|
data.each do |key, val|
|
16
18
|
script << namespace + '.' + key.to_s.camelize(:lower) + '=' + val.to_json + ';'
|
@@ -20,21 +22,24 @@ module Gon
|
|
20
22
|
script << namespace + '.' + key.to_s + '=' + val.to_json + ';'
|
21
23
|
end
|
22
24
|
end
|
23
|
-
script
|
25
|
+
script = start + Gon::Escaper.escape(script) + '</script>'
|
24
26
|
script.html_safe
|
25
27
|
else
|
26
28
|
""
|
27
29
|
end
|
28
30
|
end
|
31
|
+
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
32
35
|
module GonHelpers
|
36
|
+
|
33
37
|
def self.included base
|
34
38
|
base.send(:include, InstanceMethods)
|
35
39
|
end
|
36
40
|
|
37
41
|
module InstanceMethods
|
42
|
+
|
38
43
|
def gon
|
39
44
|
if !Gon.request_env || Gon.request != request.object_id
|
40
45
|
Gon.request = request.object_id
|
@@ -42,9 +47,10 @@ module Gon
|
|
42
47
|
end
|
43
48
|
Gon
|
44
49
|
end
|
50
|
+
|
45
51
|
end
|
46
|
-
end
|
47
52
|
|
53
|
+
end
|
48
54
|
end
|
49
55
|
|
50
56
|
ActionView::Base.send :include, Gon::Helpers
|
data/lib/gon/version.rb
CHANGED
data/spec/gon/gon_spec.rb
CHANGED
@@ -26,14 +26,37 @@ describe Gon, '#all_variables' do
|
|
26
26
|
Gon.klass = Hash
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
describe '#include_gon' do
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
Gon.clear
|
33
|
+
Gon.instance_variable_set(:@request_id, request.object_id)
|
34
|
+
ActionView::Base.instance_methods.map(&:to_s).include?('include_gon').should == true
|
35
|
+
@base = ActionView::Base.new
|
36
|
+
@base.request = request
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'outputs correct js with an integer' do
|
40
|
+
Gon.int = 1
|
41
|
+
@base.include_gon.should == '<script>window.gon = {};' +
|
42
|
+
'gon.int=1;' +
|
43
|
+
'</script>'
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'outputs correct js with a string' do
|
47
|
+
Gon.str = %q(a'b"c)
|
48
|
+
@base.include_gon.should == '<script>window.gon = {};' +
|
49
|
+
%q(gon.str="a'b\"c";) +
|
50
|
+
'</script>'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'outputs correct js with a script string' do
|
54
|
+
Gon.str = %q(</script><script>alert('!')</script>)
|
55
|
+
@base.include_gon.should == '<script>window.gon = {};' +
|
56
|
+
%q(gon.str="<\\/script><script>alert('!')<\\/script>";) +
|
57
|
+
'</script>'
|
58
|
+
end
|
59
|
+
|
37
60
|
end
|
38
61
|
|
39
62
|
it 'returns exception if try to set public method as variable' do
|
metadata
CHANGED
@@ -1,105 +1,81 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: gon
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.2
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 2
|
8
|
-
- 1
|
9
|
-
- 0
|
10
|
-
version: 2.1.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- gazay
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-03-07 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: actionpack
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70100875330620 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 3
|
32
|
-
- 0
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
33
21
|
version: 2.3.0
|
34
22
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: json
|
38
23
|
prerelease: false
|
39
|
-
|
24
|
+
version_requirements: *70100875330620
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &70100875329900 !ruby/object:Gem::Requirement
|
40
28
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
|
45
|
-
segments:
|
46
|
-
- 0
|
47
|
-
version: "0"
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
48
33
|
type: :runtime
|
49
|
-
version_requirements: *id002
|
50
|
-
- !ruby/object:Gem::Dependency
|
51
|
-
name: rabl
|
52
34
|
prerelease: false
|
53
|
-
|
35
|
+
version_requirements: *70100875329900
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rabl
|
38
|
+
requirement: &70100875328820 !ruby/object:Gem::Requirement
|
54
39
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
segments:
|
60
|
-
- 0
|
61
|
-
version: "0"
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
62
44
|
type: :development
|
63
|
-
version_requirements: *id003
|
64
|
-
- !ruby/object:Gem::Dependency
|
65
|
-
name: rspec
|
66
45
|
prerelease: false
|
67
|
-
|
46
|
+
version_requirements: *70100875328820
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &70100875328180 !ruby/object:Gem::Requirement
|
68
50
|
none: false
|
69
|
-
requirements:
|
70
|
-
- -
|
71
|
-
- !ruby/object:Gem::Version
|
72
|
-
|
73
|
-
segments:
|
74
|
-
- 0
|
75
|
-
version: "0"
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
76
55
|
type: :development
|
77
|
-
version_requirements: *id004
|
78
|
-
- !ruby/object:Gem::Dependency
|
79
|
-
name: jbuilder
|
80
56
|
prerelease: false
|
81
|
-
|
57
|
+
version_requirements: *70100875328180
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: jbuilder
|
60
|
+
requirement: &70100875327400 !ruby/object:Gem::Requirement
|
82
61
|
none: false
|
83
|
-
requirements:
|
84
|
-
- -
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
|
87
|
-
segments:
|
88
|
-
- 0
|
89
|
-
version: "0"
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
90
66
|
type: :development
|
91
|
-
|
92
|
-
|
93
|
-
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70100875327400
|
69
|
+
description: If you need to send some data to your js files and you don't want to
|
70
|
+
do this with long way trough views and parsing - use this force!
|
71
|
+
email:
|
94
72
|
- alex.gaziev@gmail.com
|
95
73
|
executables: []
|
96
|
-
|
97
74
|
extensions: []
|
98
|
-
|
99
75
|
extra_rdoc_files: []
|
100
|
-
|
101
|
-
files:
|
76
|
+
files:
|
102
77
|
- .gitignore
|
78
|
+
- .travis.yml
|
103
79
|
- Gemfile
|
104
80
|
- README.md
|
105
81
|
- Rakefile
|
@@ -107,6 +83,7 @@ files:
|
|
107
83
|
- doc/logo_small.png
|
108
84
|
- gon.gemspec
|
109
85
|
- lib/gon.rb
|
86
|
+
- lib/gon/escaper.rb
|
110
87
|
- lib/gon/helpers.rb
|
111
88
|
- lib/gon/jbuilder.rb
|
112
89
|
- lib/gon/rabl.rb
|
@@ -118,38 +95,29 @@ files:
|
|
118
95
|
- spec/test_data/sample_with_partial.json.jbuilder
|
119
96
|
homepage: https://github.com/gazay/gon
|
120
97
|
licenses: []
|
121
|
-
|
122
98
|
post_install_message:
|
123
99
|
rdoc_options: []
|
124
|
-
|
125
|
-
require_paths:
|
100
|
+
require_paths:
|
126
101
|
- lib
|
127
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
103
|
none: false
|
129
|
-
requirements:
|
130
|
-
- -
|
131
|
-
- !ruby/object:Gem::Version
|
132
|
-
|
133
|
-
|
134
|
-
- 0
|
135
|
-
version: "0"
|
136
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
109
|
none: false
|
138
|
-
requirements:
|
139
|
-
- -
|
140
|
-
- !ruby/object:Gem::Version
|
141
|
-
|
142
|
-
segments:
|
143
|
-
- 0
|
144
|
-
version: "0"
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
145
114
|
requirements: []
|
146
|
-
|
147
115
|
rubyforge_project: gon
|
148
116
|
rubygems_version: 1.8.15
|
149
117
|
signing_key:
|
150
118
|
specification_version: 3
|
151
119
|
summary: Get your Rails variables in your JS
|
152
|
-
test_files:
|
120
|
+
test_files:
|
153
121
|
- spec/gon/gon_spec.rb
|
154
122
|
- spec/test_data/_sample_partial.json.jbuilder
|
155
123
|
- spec/test_data/sample.json.jbuilder
|