gon 2.0.4 → 2.0.5
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/README.md +28 -2
- data/gon.gemspec +1 -3
- data/lib/gon.rb +6 -2
- data/lib/gon/jbuilder.rb +34 -6
- data/lib/gon/version.rb +1 -1
- data/spec/gon/gon_spec.rb +38 -8
- data/spec/test_data/_sample_partial.json.jbuilder +1 -0
- data/spec/test_data/sample_with_partial.json.jbuilder +1 -0
- metadata +20 -16
data/README.md
CHANGED
@@ -208,6 +208,15 @@ Use gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https:
|
|
208
208
|
|
209
209
|
Jbuilder works now only on Ruby 1.9+, so Gon support for Jbuilder works on 1.9+ only
|
210
210
|
|
211
|
+
0. Add jbuilder to your Gemfile (because of it depends on
|
212
|
+
ActiveSuppurt '~> 3.0.0')
|
213
|
+
|
214
|
+
`Gemfile`
|
215
|
+
|
216
|
+
``` ruby
|
217
|
+
gem 'jbuilder'
|
218
|
+
```
|
219
|
+
|
211
220
|
1. Create Jbuilder template.
|
212
221
|
|
213
222
|
`app/views/posts/index.json.jbuilder`
|
@@ -227,12 +236,29 @@ Jbuilder works now only on Ruby 1.9+, so Gon support for Jbuilder works on 1.9+
|
|
227
236
|
# some controller logic
|
228
237
|
end
|
229
238
|
```
|
239
|
+
|
230
240
|
In javascript file for view of this action write call to your variable:
|
231
241
|
|
242
|
+
Now you can use partials in jbuilder:
|
243
|
+
|
244
|
+
`app/views/posts/index.json.jbuilder`
|
245
|
+
|
246
|
+
``` jbuilder
|
247
|
+
json.partial! 'app/views/posts/_part.json.jbuilder', :comments => @posts[0].comments
|
248
|
+
```
|
249
|
+
|
250
|
+
`app/views/posts/_part.json.jbuilder`
|
251
|
+
|
252
|
+
``` jbuilder
|
253
|
+
json.comments comments.map{ |it| 'comment#' + it.id }
|
254
|
+
```
|
255
|
+
|
232
256
|
``` js
|
233
257
|
alert(gon.posts)
|
234
258
|
alert(gon.posts[0])
|
235
259
|
alert(gon.posts[0].post.body)
|
260
|
+
alert(gon.comments)
|
261
|
+
alert(gon.comments[0])
|
236
262
|
```
|
237
263
|
|
238
264
|
P.s. If you didn't put include_gon tag in your html head area - it
|
@@ -243,13 +269,13 @@ wouldn't work. You can read about this in common usage above.
|
|
243
269
|
Puts this line into `Gemfile` then run `$ bundle`:
|
244
270
|
|
245
271
|
``` ruby
|
246
|
-
gem 'gon', '2.0.
|
272
|
+
gem 'gon', '2.0.5'
|
247
273
|
```
|
248
274
|
|
249
275
|
Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
|
250
276
|
|
251
277
|
``` ruby
|
252
|
-
config.gem 'gon', :version => '2.0.
|
278
|
+
config.gem 'gon', :version => '2.0.5'
|
253
279
|
```
|
254
280
|
|
255
281
|
Or manually install gon gem: `$ gem install gon`
|
data/gon.gemspec
CHANGED
@@ -20,9 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
s.add_dependency "actionpack", '>= 2.3.0'
|
22
22
|
s.add_dependency "rabl"
|
23
|
-
if RUBY_VERSION =~ /9/
|
24
|
-
s.add_dependency "jbuilder"
|
25
|
-
end
|
26
23
|
s.add_dependency "json"
|
27
24
|
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "jbuilder"
|
28
26
|
end
|
data/lib/gon.rb
CHANGED
@@ -3,7 +3,7 @@ require 'action_view'
|
|
3
3
|
require 'action_controller'
|
4
4
|
require 'gon/helpers'
|
5
5
|
require 'gon/rabl'
|
6
|
-
if RUBY_VERSION =~ /9/
|
6
|
+
if RUBY_VERSION =~ /9/ && defined?(Jbuilder)
|
7
7
|
require 'gon/jbuilder'
|
8
8
|
end
|
9
9
|
|
@@ -73,7 +73,11 @@ module Gon
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def jbuilder(view_path, options = {})
|
76
|
-
|
76
|
+
if RUBY_VERSION !~ /9/
|
77
|
+
raise NoMethodError.new('You can use Jbuilder support only in 1.9+')
|
78
|
+
elsif !defined?(Gon::Jbuilder)
|
79
|
+
raise NoMethodError.new('You should define Jbuilder in your Gemfile')
|
80
|
+
end
|
77
81
|
|
78
82
|
jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
|
79
83
|
@request_env['action_controller.instance'] ||
|
data/lib/gon/jbuilder.rb
CHANGED
@@ -1,17 +1,45 @@
|
|
1
|
-
require 'jbuilder'
|
2
|
-
|
3
1
|
module Gon
|
4
2
|
module Jbuilder
|
5
3
|
class << self
|
4
|
+
|
5
|
+
def parse_source(source, controller)
|
6
|
+
output = ::JbuilderTemplate.encode(controller) do |json|
|
7
|
+
eval source
|
8
|
+
end
|
9
|
+
JSON.parse(output)
|
10
|
+
end
|
11
|
+
|
6
12
|
def parse_jbuilder(jbuilder_path, controller)
|
7
|
-
source = File.read(jbuilder_path)
|
8
13
|
controller.instance_variables.each do |name|
|
9
14
|
self.instance_variable_set(name, controller.instance_variable_get(name))
|
10
15
|
end
|
11
|
-
|
12
|
-
|
16
|
+
lines = find_partials(File.readlines(jbuilder_path))
|
17
|
+
source = lines.join('')
|
18
|
+
|
19
|
+
output = parse_source(source, controller)
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_partial(partial_line)
|
23
|
+
path = partial_line.match(/['"]([^'"]*)['"]/)[1]
|
24
|
+
options_hash = partial_line.match(/,(.*)/)[1]
|
25
|
+
if options_hash.present?
|
26
|
+
options = eval '{' + options_hash + '}'
|
27
|
+
options.each do |name, val|
|
28
|
+
self.instance_variable_set('@' + name.to_s, val)
|
29
|
+
eval "def #{name}; self.instance_variable_get('@' + '#{name.to_s}'); end"
|
30
|
+
end
|
13
31
|
end
|
14
|
-
|
32
|
+
find_partials(File.readlines(path))
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_partials(lines = [])
|
36
|
+
lines.map do |line|
|
37
|
+
if line =~ /partial!/
|
38
|
+
parse_partial(line)
|
39
|
+
else
|
40
|
+
line
|
41
|
+
end
|
42
|
+
end.flatten
|
15
43
|
end
|
16
44
|
end
|
17
45
|
end
|
data/lib/gon/version.rb
CHANGED
data/spec/gon/gon_spec.rb
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
require 'gon'
|
3
3
|
|
4
4
|
describe Gon, '#all_variables' do
|
5
|
-
|
5
|
+
|
6
|
+
before(:each) do
|
6
7
|
Gon.request_env = {}
|
7
8
|
end
|
8
9
|
|
@@ -49,16 +50,45 @@ describe Gon, '#all_variables' do
|
|
49
50
|
Gon.objects.length.should == 2
|
50
51
|
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
53
|
+
if RUBY_VERSION =~ /1.9/
|
54
|
+
require 'jbuilder'
|
55
|
+
require 'gon/jbuilder'
|
56
|
+
|
57
|
+
it 'render json from jbuilder template' do
|
58
|
+
Gon.clear
|
59
|
+
controller = ActionController::Base.new
|
60
|
+
objects = [1,2]
|
61
|
+
controller.instance_variable_set('@objects', objects)
|
62
|
+
Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller
|
63
|
+
Gon.objects.length.should == 2
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'render json from jbuilder template with a partial' do
|
67
|
+
Gon.clear
|
68
|
+
controller = ActionController::Base.new
|
69
|
+
controller.view_paths << 'spec/test_data'
|
70
|
+
objects = [1,2]
|
71
|
+
controller.instance_variable_set('@objects', objects)
|
72
|
+
Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
|
73
|
+
Gon.objects.length.should == 2
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should throw error if you use gon.jbuilder with ruby < 1.9+' do
|
77
|
+
RUBY_VERSION = '1.8.7'
|
78
|
+
|
79
|
+
expect { Gon.jbuilder 'some_path' }.to raise_error(NoMethodError, /1.9/)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should raise error if you use gon.jbuilder without requairing jbuilder gem' do
|
83
|
+
RUBY_VERSION = '1.9.2'
|
84
|
+
Gon.send(:remove_const, :Jbuilder)
|
85
|
+
|
86
|
+
expect { Gon.jbuilder 'some_path' }.to raise_error(NoMethodError, /Gemfile/)
|
87
|
+
end
|
59
88
|
end
|
60
89
|
|
61
90
|
def request
|
62
91
|
@request ||= double 'request', :env => {}
|
63
92
|
end
|
93
|
+
|
64
94
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
json.objects objects
|
@@ -0,0 +1 @@
|
|
1
|
+
json.partial! 'spec/test_data/_sample_partial.json.jbuilder', :objects => @objects
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153437540 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.3.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153437540
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rabl
|
27
|
-
requirement: &
|
27
|
+
requirement: &2153437120 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2153437120
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
37
|
+
name: json
|
38
|
+
requirement: &2153436660 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,21 +43,21 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2153436660
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
48
|
+
name: rspec
|
49
|
+
requirement: &2153426500 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
type: :
|
55
|
+
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2153426500
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
60
|
-
requirement: &
|
59
|
+
name: jbuilder
|
60
|
+
requirement: &2153425980 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2153425980
|
69
69
|
description: If you need to send some data to your js files and you don't want to
|
70
70
|
do this with long way trough views and parsing - use this force!
|
71
71
|
email:
|
@@ -85,8 +85,10 @@ files:
|
|
85
85
|
- lib/gon/rabl.rb
|
86
86
|
- lib/gon/version.rb
|
87
87
|
- spec/gon/gon_spec.rb
|
88
|
+
- spec/test_data/_sample_partial.json.jbuilder
|
88
89
|
- spec/test_data/sample.json.jbuilder
|
89
90
|
- spec/test_data/sample.rabl
|
91
|
+
- spec/test_data/sample_with_partial.json.jbuilder
|
90
92
|
homepage: https://github.com/gazay/gon
|
91
93
|
licenses: []
|
92
94
|
post_install_message:
|
@@ -113,5 +115,7 @@ specification_version: 3
|
|
113
115
|
summary: Get your Rails variables in your JS
|
114
116
|
test_files:
|
115
117
|
- spec/gon/gon_spec.rb
|
118
|
+
- spec/test_data/_sample_partial.json.jbuilder
|
116
119
|
- spec/test_data/sample.json.jbuilder
|
117
120
|
- spec/test_data/sample.rabl
|
121
|
+
- spec/test_data/sample_with_partial.json.jbuilder
|