gon 5.2.3 → 6.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43277ce7c2fa158208c211171b2e2e522ed84cfa
4
- data.tar.gz: 161e43d9aada6fe673fd2f938c856defdcb59810
3
+ metadata.gz: 1797244a5882bb22566a77182ee6e1c3f5327998
4
+ data.tar.gz: 506c013713108e2f32a57b1c4ddd3bf734bfc9bc
5
5
  SHA512:
6
- metadata.gz: 54085e90ee7e4dbc0a754b6e56b273943fd9f2fb972073a6abd86875a1c067f740f6a79a2e02c133dc437e666181f8adfa54451087a0d5343be96c07ea7a659c
7
- data.tar.gz: 15c74e24cdfa5e890d6ceb0b408acce28259748ed157d58a1ebcef3bebc1d6ebd21bceec6a93e14c354f9876ffd81abe7721cc6fedd3c12e72305be26a18c2b8
6
+ metadata.gz: 2352e7136f09f3f4bb709e75b29105f9793d1e67b7150cecbc6353877423a1271727d88118dedd2397cc756c471558c2285ed91d9f191e64e19fd39be68d29f9
7
+ data.tar.gz: 0c8c02ed9855110af818b65f30884bc648bd6bd4a1224e43c1000f7da6355c075af91cf44b471c303c0d2340a4138172f2aed1475ffa6e50d73233b493f8ec98
@@ -1,9 +1,10 @@
1
1
  language: ruby
2
+ sudo: false
2
3
  rvm:
3
4
  - 1.9.3
4
5
  - 2.0.0
5
- - 2.1.0
6
- - 2.1.4
6
+ - 2.1.6
7
+ - 2.2.2
7
8
  - jruby-19mode # JRuby in 1.9 mode
8
9
  - rbx
9
10
  matrix:
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 6.0.0
4
+
5
+ * Refactoring
6
+ * nonce option. Thanks to @joeljackson
7
+ * Included rails url_helpers into jbuilder. Thanks to @razum2um
8
+ * Welcome @takiy33 as contributor!
9
+
3
10
  ## 5.2.3
4
11
 
5
12
  * Coffescript implementation of watch.js. Thanks to @willcosgrove
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in gon.gemspec
4
4
  gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2011-2014 gazay
3
+ Copyright (c) 2011-2015 gazay
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # Gon gem — get your Rails variables in your js
2
2
 
3
+ [![Join the chat at https://gitter.im/gazay/gon](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gazay/gon?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4
+
3
5
  ![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
6
 
5
- [![Build Status](https://secure.travis-ci.org/gazay/gon.png)](http://travis-ci.org/gazay/gon) [![CodeClimate](https://codeclimate.com/github/gazay/gon.png)](https://codeclimate.com/github/gazay/gon)
6
- [![gon API Documentation](https://www.omniref.com/ruby/gems/gon.png)](https://www.omniref.com/ruby/gems/gon)
7
+ [![Build Status](https://secure.travis-ci.org/gazay/gon.png)](http://travis-ci.org/gazay/gon) [![CodeClimate](https://codeclimate.com/github/gazay/gon/badges/gpa.svg)](https://codeclimate.com/github/gazay/gon)
7
8
 
8
9
  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!
9
10
 
@@ -15,6 +16,10 @@ For Sinatra available [gon-sinatra](https://github.com/gazay/gon-sinatra).
15
16
 
16
17
  For .Net MVC available port [NGon](https://github.com/brooklynDev/NGon).
17
18
 
19
+ <a href="https://evilmartians.com/?utm_source=gon">
20
+ <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
21
+ </a>
22
+
18
23
  ## An example of typical use
19
24
 
20
25
  ### 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
@@ -68,9 +73,6 @@ in modern web applications!
68
73
 
69
74
  ### More details about configuration and usage you can find in [gon wiki](https://github.com/gazay/gon/wiki)
70
75
 
71
- Old readme available in [./README_old.md](https://github.com/gazay/gon/blob/master/README_old.md)
72
-
73
-
74
76
  `app/views/layouts/application.html.erb`
75
77
 
76
78
  ``` erb
@@ -81,6 +83,14 @@ Old readme available in [./README_old.md](https://github.com/gazay/gon/blob/mast
81
83
  ...
82
84
  ```
83
85
 
86
+ For rails 4:
87
+ ``` erb
88
+ <%= Gon::Base.render_data %>
89
+ ...
90
+ ```
91
+
92
+
93
+
84
94
  You can pass some [options](https://github.com/gazay/gon/wiki/Options)
85
95
  to `include_gon` method.
86
96
 
@@ -113,10 +123,10 @@ alert(gon.your_hash)
113
123
 
114
124
  ### AMD compatible version: `include_gon_amd`
115
125
 
116
- If your site uses AMD modules you can use the `include_gon_amd` helper to
117
- include the variables and watch function as a module. Options are mostly
118
- the same as for `include_gon`, except for `namespace_check`, which does
119
- nothing and `namespace`, which is used as the name of the defined module.
126
+ If your site uses AMD modules you can use the `include_gon_amd` helper to
127
+ include the variables and watch function as a module. Options are mostly
128
+ the same as for `include_gon`, except for `namespace_check`, which does
129
+ nothing and `namespace`, which is used as the name of the defined module.
120
130
  The end result will look somewhat like the following:
121
131
 
122
132
  ```js
@@ -218,6 +228,7 @@ Gon uses `MultiJson` with autodetect mode, so all you need is just require your
218
228
  ## Contributors
219
229
 
220
230
  * @gazay
231
+ * @takiy33
221
232
 
222
233
  Special thanks to @brainopia, @kossnocorp and @ai.
223
234
 
data/Rakefile CHANGED
@@ -1,5 +1,3 @@
1
- require 'rubygems'
2
- require 'rake'
3
1
  require 'bundler'
4
2
  Bundler::GemHelper.install_tasks
5
3
 
@@ -13,19 +13,17 @@ Gem::Specification.new do |s|
13
13
  s.summary = %q{Get your Rails variables in your JS}
14
14
  s.description = %q{If you need to send some data to your js files and you don't want to do this with long way trough views and parsing - use this force!}
15
15
 
16
- s.rubyforge_project = 'gon'
17
-
18
16
  s.files = `git ls-files`.split("\n")
19
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
- s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
21
17
  s.require_paths = ['lib']
22
- s.add_dependency 'actionpack', '>= 2.3.0'
23
- s.add_dependency 'request_store', '>= 1.0.5'
24
- s.add_dependency 'json'
25
- s.add_dependency 'multi_json'
26
- s.add_development_dependency 'rabl'
27
- s.add_development_dependency 'rabl-rails'
28
- s.add_development_dependency 'rspec'
29
- s.add_development_dependency 'jbuilder'
30
- s.add_development_dependency 'rake'
18
+ s.required_ruby_version = '> 1.8.7'
19
+ s.add_dependency 'actionpack', '~> 2.3'
20
+ s.add_dependency 'request_store', '~> 1.0'
21
+ s.add_dependency 'json', '~> 0'
22
+ s.add_dependency 'multi_json', '~> 0'
23
+ s.add_development_dependency 'rabl', '0.11.3'
24
+ s.add_development_dependency 'rabl-rails', '~> 0'
25
+ s.add_development_dependency 'rspec', '~> 3.0'
26
+ s.add_development_dependency 'jbuilder', '~> 0'
27
+ s.add_development_dependency 'railties', '~> 2.3'
28
+ s.add_development_dependency 'rake', '~> 0'
31
29
  end
data/lib/gon.rb CHANGED
@@ -4,6 +4,7 @@ require 'action_controller'
4
4
  require 'multi_json'
5
5
 
6
6
  require 'gon/base'
7
+ require 'gon/env_finder'
7
8
  require 'gon/global'
8
9
  require 'gon/watch'
9
10
  require 'gon/request'
@@ -11,6 +12,7 @@ require 'gon/helpers'
11
12
  require 'gon/escaper'
12
13
  require 'gon/rabl'
13
14
  require 'gon/jbuilder'
15
+ require 'gon/jbuilder/parser'
14
16
  require 'gon/json_dumper'
15
17
 
16
18
  # NOTE : ActionDispatch::Request#uuid appears only in Rails 3.2.1
@@ -116,13 +118,7 @@ class Gon
116
118
  end
117
119
 
118
120
  def public_method_name?(method)
119
- public_methods.include?(
120
- if RUBY_VERSION > '1.9'
121
- method.to_s[0..-2].to_sym
122
- else
123
- method.to_s[0..-2]
124
- end
125
- )
121
+ public_methods.include?(method.to_s[0..-2].to_sym)
126
122
  end
127
123
 
128
124
  # JbuilderTemplate will not be defined if jbuilder is required
@@ -1,107 +1,92 @@
1
+ require 'ostruct'
2
+
1
3
  class Gon
2
4
  module Base
3
- ENV_CONTROLLER_KEY = 'action_controller.instance'
5
+ VALID_OPTION_DEFAULTS = {
6
+ namespace: 'gon',
7
+ camel_case: false,
8
+ camel_depth: 1,
9
+ watch: false,
10
+ need_tag: true,
11
+ type: false,
12
+ cdata: true,
13
+ global_root: 'global',
14
+ namespace_check: false,
15
+ amd: false,
16
+ nonce: nil
17
+ }
4
18
 
5
19
  class << self
6
20
 
7
- def render_data(options)
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}={};"
21
+ def render_data(options = {})
22
+ _o = define_options(options)
10
23
 
11
- script << formatted_data(namespace, cameled, camel_depth, watch, global_root)
24
+ script = formatted_data(_o)
12
25
  script = Gon::Escaper.escape_unicode(script)
13
- script = Gon::Escaper.javascript_tag(script, type, cdata) if tag
26
+ script = Gon::Escaper.javascript_tag(script, _o.type, _o.cdata, _o.nonce) if _o.tag
14
27
 
15
28
  script.html_safe
16
29
  end
17
30
 
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
31
+ private
30
32
 
31
- def get_controller(options = {})
32
- options[:controller] ||
33
- (
34
- current_gon &&
35
- current_gon.env[Gon::Base::ENV_CONTROLLER_KEY] ||
36
- current_gon.env['action_controller.rescue.response'].
37
- instance_variable_get('@template').
38
- instance_variable_get('@controller')
39
- )
40
- end
33
+ def define_options(options)
34
+ _o = OpenStruct.new
41
35
 
42
- def get_template_path(options, extension)
43
- if options[:template]
44
- if right_extension?(extension, options[:template])
45
- options[:template]
46
- else
47
- [options[:template], extension].join('.')
48
- end
49
- else
50
- controller = get_controller(options).controller_path
51
- action = get_controller(options).action_name
52
- "app/views/#{controller}/#{action}.json.#{extension}"
36
+ VALID_OPTION_DEFAULTS.each do |opt_name, default|
37
+ _o.send("#{opt_name}=", options.fetch(opt_name, default))
53
38
  end
54
- end
55
-
56
- private
57
-
58
- def current_gon
59
- RequestStore.store[:gon]
60
- end
39
+ _o.watch = options[:watch] || !Gon.watch.all_variables.empty?
40
+ _o.tag = _o.need_tag
41
+ _o.cameled = _o.camel_case
61
42
 
62
- def parse_options(options)
63
- namespace = options[:namespace] || 'gon'
64
- need_tag = options[:need_tag].nil? || options[:need_tag]
65
- cameled = options[:camel_case]
66
- camel_depth = options[:camel_depth] || 1
67
- watch = options[:watch] || !Gon.watch.all_variables.empty?
68
- tag = need_tag
69
- type = options[:type].nil? || options[:type]
70
- cdata = options[:cdata].nil? || options[:cdata]
71
- global_root = options.has_key?(:global_root) ? options[:global_root] : 'global'
72
- namespace_check = options.has_key?(:namespace_check) ? options[:namespace_check] : false
73
-
74
- [namespace, tag, cameled, camel_depth, watch, type, cdata, global_root, namespace_check]
43
+ _o
75
44
  end
76
45
 
77
- def formatted_data(namespace, keys_cameled, camel_depth, watch, global_root)
46
+ def formatted_data(_o)
78
47
  script = ''
48
+ before, after = render_wrap(_o)
49
+ script << before
79
50
 
80
- gon_variables(global_root).each do |key, val|
81
- js_key = keys_cameled ? key.to_s.camelize(:lower) : key.to_s
82
- script << "#{namespace}.#{js_key}=#{to_json(val, camel_depth)};"
83
- end
84
-
85
- if watch and Gon::Watch.all_variables.present?
86
- script << Gon.watch.render
87
- end
51
+ script << gon_variables(_o.global_root).
52
+ map { |key, val| render_variable(_o, key, val) }.join
53
+ script << (render_watch(_o) || '')
88
54
 
55
+ script << after
89
56
  script
90
57
  end
91
58
 
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)};"
59
+ def render_wrap(_o)
60
+ if _o.amd
61
+ ["define('#{_o.namespace}',[],function(){var gon={};", 'return gon;});']
62
+ else
63
+ before = \
64
+ if _o.namespace_check
65
+ "window.#{_o.namespace}=window.#{_o.namespace}||{};"
66
+ else
67
+ "window.#{_o.namespace}={};"
68
+ end
69
+ [before, '']
98
70
  end
71
+ end
99
72
 
100
- if watch and Gon::Watch.all_variables.present?
101
- script << Gon.watch.render_amd
73
+ def render_variable(_o, key, value)
74
+ js_key = _o.cameled ? key.to_s.camelize(:lower) : key.to_s
75
+ if _o.amd
76
+ "gon['#{js_key}']=#{to_json(value, _o.camel_depth)};"
77
+ else
78
+ "#{_o.namespace}.#{js_key}=#{to_json(value, _o.camel_depth)};"
102
79
  end
80
+ end
103
81
 
104
- script
82
+ def render_watch(_o)
83
+ if _o.watch and Gon::Watch.all_variables.present?
84
+ if _o.amd
85
+ Gon.watch.render_amd
86
+ else
87
+ Gon.watch.render
88
+ end
89
+ end
105
90
  end
106
91
 
107
92
  def to_json(value, camel_depth)
@@ -113,14 +98,14 @@ class Gon
113
98
  return value if current_depth > (max_depth.is_a?(Symbol) ? 1000 : max_depth)
114
99
 
115
100
  case value
116
- when Hash
117
- Hash[value.map { |k, v|
118
- [ k.to_s.camelize(:lower), convert_hash_keys(v, current_depth + 1, max_depth) ]
119
- }]
120
- when Enumerable
121
- value.map { |v| convert_hash_keys(v, current_depth + 1, max_depth) }
122
- else
123
- value
101
+ when Hash
102
+ Hash[value.map { |k, v|
103
+ [ k.to_s.camelize(:lower), convert_hash_keys(v, current_depth + 1, max_depth) ]
104
+ }]
105
+ when Enumerable
106
+ value.map { |v| convert_hash_keys(v, current_depth + 1, max_depth) }
107
+ else
108
+ value
124
109
  end
125
110
  end
126
111
 
@@ -138,10 +123,6 @@ class Gon
138
123
  data.merge(Gon.all_variables)
139
124
  end
140
125
 
141
- def right_extension?(extension, template_path)
142
- File.extname(template_path) == ".#{extension}"
143
- end
144
-
145
126
  end
146
127
  end
147
128
  end
@@ -0,0 +1,46 @@
1
+ class Gon
2
+ module EnvFinder
3
+ ENV_CONTROLLER_KEY = 'action_controller.instance'
4
+ ENV_RESPONSE_KEY = 'action_controller.rescue.response'
5
+
6
+ class << self
7
+
8
+ def controller_env(options = {})
9
+ options[:controller] ||
10
+ (
11
+ current_gon &&
12
+ current_gon.env[ENV_CONTROLLER_KEY] ||
13
+ current_gon.env[ENV_RESPONSE_KEY].
14
+ instance_variable_get('@template').
15
+ instance_variable_get('@controller')
16
+ )
17
+ end
18
+
19
+ def template_path(options, extension)
20
+ if options[:template]
21
+ if right_extension?(extension, options[:template])
22
+ options[:template]
23
+ else
24
+ [options[:template], extension].join('.')
25
+ end
26
+ else
27
+ controller = controller_env(options).controller_path
28
+ action = controller_env(options).action_name
29
+ "app/views/#{controller}/#{action}.json.#{extension}"
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def right_extension?(extension, template_path)
36
+ File.extname(template_path) == ".#{extension}"
37
+ end
38
+
39
+ def current_gon
40
+ RequestStore.store[:gon]
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -12,9 +12,12 @@ class Gon
12
12
  end
13
13
  end
14
14
 
15
- def javascript_tag(content, type, cdata)
16
- type = { type: 'text/javascript' } if type
17
- content_tag(:script, javascript_cdata_section(content, cdata).html_safe, type)
15
+ def javascript_tag(content, type, cdata, nonce)
16
+ options = {}
17
+ options.merge!( { type: 'text/javascript' } ) if type
18
+ options.merge!( { nonce: nonce } ) if nonce
19
+
20
+ content_tag(:script, javascript_cdata_section(content, cdata).html_safe, options)
18
21
  end
19
22
 
20
23
  def javascript_cdata_section(content, cdata)