Skip to content

Commit 284efd3

Browse files
committed
Added support for jbuilder gem by @dhh
1 parent 7f8a787 commit 284efd3

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

lib/gon.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
gem 'blankslate'
12
require 'action_view'
23
require 'action_controller'
34
require 'gon/helpers'

lib/gon/jbuilder.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
require 'jbuilder'
22

3-
module Gon::Jbuilder
4-
class << self
5-
def parse_jbuilder(jbuilder_path, controller)
6-
source = File.read(jbuilder_path)
7-
controller.instance_variables.each do |name|
8-
self.instance_variable_set(name, controller.instance_variable_get(name))
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)
915
end
10-
output = JbuilderTemplate.encode(controller) do |json|
11-
eval source
12-
end
13-
JSON.parse(output)
1416
end
1517
end
1618
end

lib/gon/rabl.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
require 'rabl'
22

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
1012
end
1113
end
1214
end

spec/gon/gon_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
Gon.objects.length.should == 2
5050
end
5151

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+
5261
def request
5362
@request ||= double 'request', :env => {}
5463
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.objects @objects

0 commit comments

Comments
 (0)