Skip to content

Commit 795dad6

Browse files
committed
Refactoring module jbuilder for supporting jbuilder's partials
1 parent 0a7fe00 commit 795dad6

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

lib/gon/jbuilder.rb

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1-
require 'jbuilder'
2-
31
module Gon
42
module Jbuilder
53
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+
612
def parse_jbuilder(jbuilder_path, controller)
7-
source = File.read(jbuilder_path)
813
controller.instance_variables.each do |name|
914
self.instance_variable_set(name, controller.instance_variable_get(name))
1015
end
11-
output = ::JbuilderTemplate.encode(controller) do |json|
12-
eval source
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
1331
end
14-
JSON.parse(output)
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
1543
end
1644
end
1745
end

spec/gon/gon_spec.rb

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
require 'gon'
33

44
describe Gon, '#all_variables' do
5-
before(:each) do
5+
6+
before(:each) do
67
Gon.request_env = {}
78
end
89

@@ -49,26 +50,45 @@
4950
Gon.objects.length.should == 2
5051
end
5152

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
53+
if RUBY_VERSION =~ /1.9/
54+
require 'jbuilder'
55+
require 'gon/jbuilder'
6056

61-
it 'render json from jbuilder template with a partial' do
62-
Gon.clear
63-
controller = ActionController::Base.new
64-
controller.view_paths << 'spec/test_data'
65-
objects = [1,2]
66-
controller.instance_variable_set('@objects', objects)
67-
Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
68-
Gon.objects.length.should == 2
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
6988
end
7089

7190
def request
7291
@request ||= double 'request', :env => {}
7392
end
93+
7494
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
json.partial! 'sample_partial', :objects => @objects
1+
json.partial! 'spec/test_data/_sample_partial.json.jbuilder', :objects => @objects

0 commit comments

Comments
 (0)