gon 5.1.0 → 5.1.1

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: c117fb1e03d8437b1cb22592a559d8c230bffdd6
4
- data.tar.gz: e20b61c0441603d5328c54c00b7b16b69edba560
3
+ metadata.gz: 1ef96285cbcd0c7f1cf74fe2ca0c0735dfcfb282
4
+ data.tar.gz: f01d459182fee2c6d24ed36295cf79a5770d6087
5
5
  SHA512:
6
- metadata.gz: eeb234b14c08b522304e7dc5578b9ac1b4d900f594dd64ad12b35475d8700132b81caec1b534250d41974b891773f32ed93af0cadcc08a781c78d8334946bbeb
7
- data.tar.gz: a17e689b89609a7fd35202d3c3d9c306cc88792da71632c6b9423a651a1232652cd493d27760287b45f779fc123f459730434a283a0ed53967451f16b1490ff4
6
+ metadata.gz: 2b314d4df0e993c5bb0de5b0b61541652201930f51305420e5f56bc621daf3a13d4479e4b1e819b298c06c07f7112fac8eecc9502a053d46477901bf67b9b9fd
7
+ data.tar.gz: 209002b52bdddf14de95f121434388bff62f6a17d571ca23577238a3c4e2180e30b3948f96a0bd3eab9754c6e372f34cfcd578cdc88dfcc07e2407cb874e1491
data/README.md CHANGED
@@ -175,6 +175,11 @@ great for some init data.
175
175
  [Instruction](https://github.com/gazay/gon/wiki/Usage-gon-global) for
176
176
  usage gon.global.
177
177
 
178
+ ## Speed up Gon
179
+
180
+ You can use any [JSON Engine](https://github.com/intridea/multi_json#supported-json-engines) you want.
181
+ Gon uses `MultiJson` with autodetect mode, so all you need is just require your JSON library.
182
+
178
183
  ## Contributors
179
184
 
180
185
  * @gazay
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'actionpack', '>= 2.3.0'
23
23
  s.add_dependency 'request_store', '>= 1.0.5'
24
24
  s.add_dependency 'json'
25
- s.add_dependency 'oj' unless RUBY_PLATFORM =~ /java/
25
+ s.add_dependency 'multi_json'
26
26
  s.add_development_dependency 'rabl'
27
27
  s.add_development_dependency 'rabl-rails'
28
28
  s.add_development_dependency 'rspec'
data/lib/gon.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'request_store'
2
2
  require 'action_view'
3
3
  require 'action_controller'
4
+ require 'multi_json'
4
5
 
5
6
  require 'gon/base'
6
7
  require 'gon/global'
@@ -12,18 +13,13 @@ require 'gon/rabl'
12
13
  require 'gon/jbuilder'
13
14
  require 'gon/json_dumper'
14
15
 
15
- # NOTE : JRuby greater than 1.7 doesn't support C-extensions
16
- if RUBY_PLATFORM =~ /java/
17
- require 'gon/json_dumper/active_support'
18
- else
19
- require 'gon/json_dumper/oj'
20
- end
21
-
22
16
  # NOTE : ActionDispatch::Request#uuid appears only in Rails 3.2.1
23
17
  unless ActionDispatch::Request.public_instance_methods.include?(:uuid)
24
18
  require 'gon/compatibility/old_rails'
25
19
  end
26
20
 
21
+ require 'gon/spec_helpers'
22
+
27
23
  class Gon
28
24
  class << self
29
25
 
@@ -1,12 +1,14 @@
1
1
  class Gon
2
2
  module Base
3
+ ENV_CONTROLLER_KEY = 'action_controller.instance'
4
+
3
5
  class << self
4
6
 
5
7
  def render_data(options)
6
- namespace, tag, cameled, camel_depth, watch, type, cdata = parse_options(options)
8
+ namespace, tag, cameled, camel_depth, watch, type, cdata, global_root = parse_options(options)
7
9
  script = "window.#{namespace}={};"
8
10
 
9
- script << formatted_data(namespace, cameled, camel_depth, watch)
11
+ script << formatted_data(namespace, cameled, camel_depth, watch, global_root)
10
12
  script = Gon::Escaper.escape_unicode(script)
11
13
  script = Gon::Escaper.javascript_tag(script, type, cdata) if tag
12
14
 
@@ -17,7 +19,7 @@ class Gon
17
19
  options[:controller] ||
18
20
  (
19
21
  current_gon &&
20
- current_gon.env['action_controller.instance'] ||
22
+ current_gon.env[Gon::Base::ENV_CONTROLLER_KEY] ||
21
23
  current_gon.env['action_controller.rescue.response'].
22
24
  instance_variable_get('@template').
23
25
  instance_variable_get('@controller')
@@ -53,14 +55,15 @@ class Gon
53
55
  tag = need_tag
54
56
  type = options[:type].nil? || options[:type]
55
57
  cdata = options[:cdata].nil? || options[:cdata]
58
+ global_root = options.has_key?(:global_root) ? options[:global_root] : 'global'
56
59
 
57
- [namespace, tag, cameled, camel_depth, watch, type, cdata]
60
+ [namespace, tag, cameled, camel_depth, watch, type, cdata, global_root]
58
61
  end
59
62
 
60
- def formatted_data(namespace, keys_cameled, camel_depth, watch)
63
+ def formatted_data(namespace, keys_cameled, camel_depth, watch, global_root)
61
64
  script = ''
62
65
 
63
- gon_variables.each do |key, val|
66
+ gon_variables(global_root).each do |key, val|
64
67
  js_key = keys_cameled ? key.to_s.camelize(:lower) : key.to_s
65
68
  script << "#{namespace}.#{js_key}=#{to_json(val, camel_depth)};"
66
69
  end
@@ -92,14 +95,18 @@ class Gon
92
95
  end
93
96
  end
94
97
 
95
- def gon_variables
96
- data = Gon.all_variables || {}
98
+ def gon_variables(global_root)
99
+ data = {}
97
100
 
98
101
  if Gon.global.all_variables.present?
99
- data[:global] = Gon.global.all_variables
102
+ if global_root.blank?
103
+ data = Gon.global.all_variables
104
+ else
105
+ data[global_root.to_sym] = Gon.global.all_variables
106
+ end
100
107
  end
101
108
 
102
- data
109
+ data.merge(Gon.all_variables)
103
110
  end
104
111
 
105
112
  def right_extension?(extension, template_path)
@@ -7,7 +7,7 @@ class Gon
7
7
 
8
8
  def escape_unicode(javascript)
9
9
  if javascript
10
- result = escape_line_separator(escape_special_chars javascript)
10
+ result = escape_line_separator(javascript)
11
11
  javascript.html_safe? ? result.html_safe : result
12
12
  end
13
13
  end
@@ -31,10 +31,6 @@ class Gon
31
31
  javascript.gsub(/\\u2028/u, '&#x2028;')
32
32
  end
33
33
 
34
- def escape_special_chars(javascript)
35
- javascript.gsub(/(\<)/u, '\u003c').gsub(/(\>)/u, '\u003e')
36
- end
37
-
38
34
  end
39
35
  end
40
36
  end
@@ -1,7 +1,8 @@
1
1
  class Gon
2
2
  module JsonDumper
3
3
  def self.dump(object)
4
- raise NotImplementedError
4
+ MultiJson.dump object,
5
+ mode: :compat, escape_mode: :xss_safe, time_format: :ruby
5
6
  end
6
7
  end
7
8
  end
@@ -0,0 +1,27 @@
1
+ class Gon
2
+ module SpecHelper
3
+ module Rails
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ module GonSession
8
+ def process(*)
9
+ # preload threadlocal & store controller instance
10
+ controller.gon
11
+ Gon.send(:current_gon).env[Gon::Base::ENV_CONTROLLER_KEY] = controller
12
+ super
13
+ end
14
+ end
15
+
16
+ def new(*)
17
+ super.extend(GonSession)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ if defined?(ActionController::TestCase::Behavior)
25
+ ActionController::TestCase::Behavior.send :include, Gon::SpecHelper::Rails
26
+ end
27
+
@@ -1,3 +1,3 @@
1
1
  class Gon
2
- VERSION = '5.1.0'
2
+ VERSION = '5.1.1'
3
3
  end
@@ -59,8 +59,8 @@ describe Gon::Global do
59
59
  expect(@base.include_gon).to eq("<script type=\"text/javascript\">" +
60
60
  "\n//<![CDATA[\n" +
61
61
  "window.gon={};" +
62
- "gon.int=1;" +
63
62
  "gon.global={\"int\":1};" +
63
+ "gon.int=1;" +
64
64
  "\n//]]>\n" +
65
65
  "</script>")
66
66
  end
@@ -77,11 +77,11 @@ describe Gon::Global do
77
77
 
78
78
  it 'outputs correct js with a script string' do
79
79
  Gon.global.str = %q(</script><script>alert('!')</script>)
80
- escaped_str = "\"\\u003c/script\\u003e\\u003cscript\\u003ealert('!')\\u003c/script\\u003e\""
80
+ escaped_str = "\\u003c/script\\u003e\\u003cscript\\u003ealert('!')\\u003c/script\\u003e"
81
81
  expect(@base.include_gon).to eq("<script type=\"text/javascript\">" +
82
82
  "\n//<![CDATA[\n" +
83
83
  "window.gon={};" +
84
- "gon.global={\"str\":#{escaped_str}};" +
84
+ "gon.global={\"str\":\"#{escaped_str}\"};" +
85
85
  "\n//]]>\n" +
86
86
  "</script>")
87
87
  end
@@ -96,6 +96,17 @@ describe Gon::Global do
96
96
  "</script>")
97
97
  end
98
98
 
99
+ it 'outputs locally overridden value' do
100
+ Gon.str = 'local value'
101
+ Gon.global.str = 'global value'
102
+ expect(@base.include_gon(global_root: '')).to eq("<script type=\"text/javascript\">" +
103
+ "\n//<![CDATA[\n" +
104
+ "window.gon={};" +
105
+ "gon.str=\"local value\";" +
106
+ "\n//]]>\n" +
107
+ "</script>")
108
+ end
109
+
99
110
  end
100
111
 
101
112
  it 'returns exception if try to set public method as variable' do
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.1.0
4
+ version: 5.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gazay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: oj
56
+ name: multi_json
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '>='
@@ -165,10 +165,9 @@ files:
165
165
  - lib/gon/helpers.rb
166
166
  - lib/gon/jbuilder.rb
167
167
  - lib/gon/json_dumper.rb
168
- - lib/gon/json_dumper/active_support.rb
169
- - lib/gon/json_dumper/oj.rb
170
168
  - lib/gon/rabl.rb
171
169
  - lib/gon/request.rb
170
+ - lib/gon/spec_helpers.rb
172
171
  - lib/gon/version.rb
173
172
  - lib/gon/watch.rb
174
173
  - spec/gon/basic_spec.rb
@@ -1,7 +0,0 @@
1
- class Gon
2
- module JsonDumper
3
- def self.dump(object)
4
- object.to_json
5
- end
6
- end
7
- end
@@ -1,16 +0,0 @@
1
- require 'oj'
2
-
3
- class Gon
4
- module JsonDumper
5
- def self.dump(object)
6
- Oj.dump(object, mode: :compat, time_format: :ruby)
7
- end
8
- end
9
-
10
- # NOTE : If we are using Oj, gsub works only with \u, not \\u
11
- module Escaper
12
- def self.escape_line_separator(javascript)
13
- javascript.gsub(/\u2028/u, '&#x2028;')
14
- end
15
- end
16
- end