Skip to content

Commit 7c05c6e

Browse files
committed
Parse partials path in jbuilder module
1 parent 769202d commit 7c05c6e

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lib/gon/global.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ def inspect
1414
'Gon::Global'
1515
end
1616

17+
def rabl(*args)
18+
unless Gon.constants.include?(:Rabl)
19+
raise "Possible wrong require order problem - try to add `gem 'rabl'` before `gem 'gon'` in your Gemfile"
20+
end
21+
data, options = Gon::Rabl.handler(args, true)
22+
23+
store_builder_data 'rabl', data, options
24+
end
25+
26+
def jbuilder(*args)
27+
unless Gon.constants.include?(:Jbuilder)
28+
raise "Possible wrong require order problem - try to add `gem 'jbuilder'` before `gem 'gon'` in your Gemfile"
29+
end
30+
data, options = Gon::Jbuilder.handler(args, true)
31+
32+
store_builder_data 'jbuilder', data, options
33+
end
34+
1735
private
1836

1937
def get_variable(name)

lib/gon/jbuilder.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ def handler(args, global = false)
77
if global && !options[:template]
88
raise 'You should provide :template when use rabl with global variables'
99
end
10+
controller = Gon::Base.get_controller(options)
11+
@_controller_name = global ? '' : controller.controller_path
1012

1113
include_helpers
1214

1315
data = parse_jbuilder \
1416
Gon::Base.get_template_path(options,'jbuilder'),
15-
Gon::Base.get_controller(options)
17+
controller
1618

1719
[data, options]
1820
end
@@ -69,6 +71,7 @@ def parse_source(source, controller)
6971

7072
def parse_partial(partial_line)
7173
path = partial_line.match(/['"]([^'"]*)['"]/)[1]
74+
path = parse_path path
7275
options_hash = partial_line.match(/,(.*)/)[1]
7376
if options_hash.present?
7477
options = eval '{' + options_hash + '}'
@@ -80,10 +83,40 @@ def parse_partial(partial_line)
8083
find_partials File.readlines(path)
8184
end
8285

86+
def parse_path(path)
87+
return path if File.exists?(path)
88+
89+
splitted = path.split('/')
90+
if splitted.size == 2
91+
tmp_path = construct_path(splitted[0], splitted[1])
92+
return tmp_path if tmp_path
93+
elsif splitted.size == 1
94+
tmp_path = construct_path @_controller_name, splitted[0]
95+
return tmp_path if tmp_path
96+
end
97+
98+
raise 'Something wrong with partial path in your jbuilder templates'
99+
end
100+
101+
def construct_path(part1, part2)
102+
tmp_path = "app/views/#{part1}/_#{part2}"
103+
tmp_path = path_with_ext(tmp_path)
104+
return tmp_path if tmp_path
105+
tmp_path = "app/views/#{part1}/#{part2}"
106+
tmp_path = path_with_ext(tmp_path)
107+
return tmp_path if tmp_path
108+
end
109+
110+
def path_with_ext(path)
111+
return path if File.exists?(path)
112+
return "#{path}.jbuilder" if File.exists?("#{path}.jbuilder")
113+
return "#{path}.json.jbuilder" if File.exists?("#{path}.json.jbuilder")
114+
end
115+
83116
def find_partials(lines = [])
84117
lines.map do |line|
85118
if line =~ /partial!/
86-
parse_partial(line)
119+
parse_partial line
87120
else
88121
line
89122
end

0 commit comments

Comments
 (0)