gon 2.0.3 → 2.0.4

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 CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  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!
5
5
 
6
- Now with [Rabl](https://github.com/nesquena/rabl) support!
6
+ Now with [Jbuilder](https://github.com/rails/jbuilder) and [Rabl](https://github.com/nesquena/rabl) support!
7
7
 
8
8
  ## An example of typical use
9
9
 
@@ -52,7 +52,7 @@ gem line to your Gemfile and do the following:
52
52
  ...
53
53
  ```
54
54
 
55
- For camelize your variables in js you can use:
55
+ To camelize your variables in js you can use:
56
56
 
57
57
  ``` erb
58
58
  <head>
@@ -62,7 +62,7 @@ For camelize your variables in js you can use:
62
62
  ...
63
63
  ```
64
64
 
65
- For different namespace of your variables in js you can use:
65
+ You can change the namespace of the variables:
66
66
 
67
67
  ``` erb
68
68
  <head>
@@ -72,7 +72,7 @@ For different namespace of your variables in js you can use:
72
72
  ...
73
73
  ```
74
74
 
75
- In action of your controller you put something like this:
75
+ You put something like this in the action of your controller:
76
76
 
77
77
  ``` ruby
78
78
  @your_int = 123
@@ -90,7 +90,7 @@ gon.your_array # > [1, 2, 123]
90
90
  gon.clear # gon.all_variables now is {}
91
91
  ```
92
92
 
93
- In javascript file for view of this action write call to your variable:
93
+ Access the varaibles from your JavaScript file:
94
94
 
95
95
  ``` js
96
96
  alert(gon.your_int)
@@ -125,7 +125,7 @@ The way of writing Rabl templates is very clearly described in their repo.
125
125
  Profit of using Rabl with gon:
126
126
 
127
127
  1. You can clean your controllers now!
128
- 2. Clear and easy work with database objects and collections
128
+ 2. Work with database objects and collections clearly and easyly
129
129
  3. All power of Rabl
130
130
  4. You can still be lazy and don't use common way to transfer data in js
131
131
  5. And so on
@@ -202,18 +202,54 @@ gon mapping method:
202
202
  gon.rabl 'path/to/rabl/file', :as => 'alias'
203
203
  ```
204
204
 
205
+ ## Usage with Jbuilder
206
+
207
+ Use gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https://guthub.com/nesquena/rabl):
208
+
209
+ Jbuilder works now only on Ruby 1.9+, so Gon support for Jbuilder works on 1.9+ only
210
+
211
+ 1. Create Jbuilder template.
212
+
213
+ `app/views/posts/index.json.jbuilder`
214
+
215
+ ``` jbuilder
216
+ json.posts @posts, :id, :title, :body
217
+ ```
218
+
219
+ 2. In your controller you should map this template to gon.
220
+
221
+ ``` ruby
222
+ def index
223
+ # some controller logic
224
+ @posts = Post.all
225
+
226
+ gon.jbuilder 'app/views/posts/index.json.jbuilder'
227
+ # some controller logic
228
+ end
229
+ ```
230
+ In javascript file for view of this action write call to your variable:
231
+
232
+ ``` js
233
+ alert(gon.posts)
234
+ alert(gon.posts[0])
235
+ alert(gon.posts[0].post.body)
236
+ ```
237
+
238
+ P.s. If you didn't put include_gon tag in your html head area - it
239
+ wouldn't work. You can read about this in common usage above.
240
+
205
241
  ## Installation
206
242
 
207
243
  Puts this line into `Gemfile` then run `$ bundle`:
208
244
 
209
245
  ``` ruby
210
- gem 'gon', '2.0.3'
246
+ gem 'gon', '2.0.4'
211
247
  ```
212
248
 
213
249
  Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
214
250
 
215
251
  ``` ruby
216
- config.gem 'gon', :version => '2.0.3'
252
+ config.gem 'gon', :version => '2.0.4'
217
253
  ```
218
254
 
219
255
  Or manually install gon gem: `$ gem install gon`
@@ -20,6 +20,9 @@ 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
23
26
  s.add_dependency "json"
24
27
  s.add_development_dependency "rspec"
25
28
  end
data/lib/gon.rb CHANGED
@@ -1,7 +1,11 @@
1
+ gem 'blankslate'
1
2
  require 'action_view'
2
3
  require 'action_controller'
3
4
  require 'gon/helpers'
4
5
  require 'gon/rabl'
6
+ if RUBY_VERSION =~ /9/
7
+ require 'gon/jbuilder'
8
+ end
5
9
 
6
10
  module Gon
7
11
  class << self
@@ -67,5 +71,24 @@ module Gon
67
71
  set_variable('rabl', rabl_data)
68
72
  end
69
73
  end
74
+
75
+ def jbuilder(view_path, options = {})
76
+ raise NoMethodError.new('You can use Jbuilder support only in 1.9+') if RUBY_VERSION !~ /9/
77
+
78
+ jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
79
+ @request_env['action_controller.instance'] ||
80
+ @request_env['action_controller.rescue.response'].
81
+ instance_variable_get('@template').
82
+ instance_variable_get('@controller'))
83
+ if options[:as]
84
+ set_variable(options[:as].to_s, jbuilder_data)
85
+ elsif jbuilder_data.is_a? Hash
86
+ jbuilder_data.each do |key, value|
87
+ set_variable(key, value)
88
+ end
89
+ else
90
+ set_variable('jbuilder', jbuilder_data)
91
+ end
92
+ end
70
93
  end
71
94
  end
@@ -0,0 +1,18 @@
1
+ require 'jbuilder'
2
+
3
+ module Gon
4
+ module Jbuilder
5
+ class << self
6
+ def parse_jbuilder(jbuilder_path, controller)
7
+ source = File.read(jbuilder_path)
8
+ controller.instance_variables.each do |name|
9
+ self.instance_variable_set(name, controller.instance_variable_get(name))
10
+ end
11
+ output = ::JbuilderTemplate.encode(controller) do |json|
12
+ eval source
13
+ end
14
+ JSON.parse(output)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,12 +1,14 @@
1
1
  require 'rabl'
2
2
 
3
- module Gon::Rabl
4
- class << self
5
- def parse_rabl(rabl_path, controller)
6
- source = File.read(rabl_path)
7
- rabl_engine = Rabl::Engine.new(source, :format => 'json')
8
- output = rabl_engine.render(controller, {})
9
- JSON.parse(output)
3
+ module Gon
4
+ module Rabl
5
+ class << self
6
+ def parse_rabl(rabl_path, controller)
7
+ source = File.read(rabl_path)
8
+ rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
9
+ output = rabl_engine.render(controller, {})
10
+ JSON.parse(output)
11
+ end
10
12
  end
11
13
  end
12
14
  end
@@ -1,3 +1,3 @@
1
1
  module Gon
2
- VERSION = '2.0.3'
2
+ VERSION = '2.0.4'
3
3
  end
@@ -49,6 +49,15 @@ describe Gon, '#all_variables' do
49
49
  Gon.objects.length.should == 2
50
50
  end
51
51
 
52
+ it 'render json from jbuilder template' do
53
+ Gon.clear
54
+ controller = ActionController::Base.new
55
+ objects = [1,2]
56
+ controller.instance_variable_set('@objects', objects)
57
+ Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller
58
+ Gon.objects.length.should == 2
59
+ end
60
+
52
61
  def request
53
62
  @request ||= double 'request', :env => {}
54
63
  end
@@ -0,0 +1 @@
1
+ json.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.3
4
+ version: 2.0.4
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-11-09 00:00:00.000000000Z
12
+ date: 2011-12-04 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
16
- requirement: &2157794660 !ruby/object:Gem::Requirement
16
+ requirement: &2155202500 !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: *2157794660
24
+ version_requirements: *2155202500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rabl
27
- requirement: &2157794240 !ruby/object:Gem::Requirement
27
+ requirement: &2155202080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2157794240
35
+ version_requirements: *2155202080
36
+ - !ruby/object:Gem::Dependency
37
+ name: jbuilder
38
+ requirement: &2155201580 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2155201580
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: json
38
- requirement: &2157793780 !ruby/object:Gem::Requirement
49
+ requirement: &2155201160 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *2157793780
57
+ version_requirements: *2155201160
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rspec
49
- requirement: &2157793360 !ruby/object:Gem::Requirement
60
+ requirement: &2155200740 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,7 +65,7 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *2157793360
68
+ version_requirements: *2155200740
58
69
  description: If you need to send some data to your js files and you don't want to
59
70
  do this with long way trough views and parsing - use this force!
60
71
  email:
@@ -70,9 +81,11 @@ files:
70
81
  - gon.gemspec
71
82
  - lib/gon.rb
72
83
  - lib/gon/helpers.rb
84
+ - lib/gon/jbuilder.rb
73
85
  - lib/gon/rabl.rb
74
86
  - lib/gon/version.rb
75
87
  - spec/gon/gon_spec.rb
88
+ - spec/test_data/sample.json.jbuilder
76
89
  - spec/test_data/sample.rabl
77
90
  homepage: https://github.com/gazay/gon
78
91
  licenses: []
@@ -100,4 +113,5 @@ specification_version: 3
100
113
  summary: Get your Rails variables in your JS
101
114
  test_files:
102
115
  - spec/gon/gon_spec.rb
116
+ - spec/test_data/sample.json.jbuilder
103
117
  - spec/test_data/sample.rabl