Skip to content

Commit ae9f258

Browse files
thedarkonejosh
authored andcommitted
Fix template extension parsing. [rails#2315 state:resolved] [rails#2284 state:resolved]
Signed-off-by: Joshua Peek <[email protected]>
1 parent e3b166a commit ae9f258

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

actionpack/lib/action_view/template.rb

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -217,46 +217,30 @@ def valid_extension?(extension)
217217
end
218218

219219
def valid_locale?(locale)
220-
I18n.available_locales.include?(locale.to_sym)
220+
locale && I18n.available_locales.include?(locale.to_sym)
221221
end
222222

223223
# Returns file split into an array
224224
# [base_path, name, locale, format, extension]
225225
def split(file)
226226
if m = file.to_s.match(/^(.*\/)?([^\.]+)\.(.*)$/)
227-
base_path = m[1]
228-
name = m[2]
229-
extensions = m[3]
230-
else
231-
return
227+
[m[1], m[2], *parse_extensions(m[3])]
232228
end
229+
end
233230

234-
locale = nil
235-
format = nil
236-
extension = nil
237-
238-
if m = extensions.split(".")
239-
if valid_locale?(m[0]) && m[1] && valid_extension?(m[2]) # All three
240-
locale = m[0]
241-
format = m[1]
242-
extension = m[2]
243-
elsif m[0] && m[1] && valid_extension?(m[2]) # Multipart formats
244-
format = "#{m[0]}.#{m[1]}"
245-
extension = m[2]
246-
elsif valid_locale?(m[0]) && valid_extension?(m[1]) # locale and extension
247-
locale = m[0]
248-
extension = m[1]
249-
elsif valid_extension?(m[1]) # format and extension
250-
format = m[0]
251-
extension = m[1]
252-
elsif valid_extension?(m[0]) # Just extension
253-
extension = m[0]
254-
else # No extension
255-
format = m[0]
256-
end
231+
# returns parsed extensions as an array
232+
# [locale, format, extension]
233+
def parse_extensions(extensions)
234+
exts = extensions.split(".")
235+
236+
if extension = valid_extension?(exts.last) && exts.pop || nil
237+
locale = valid_locale?(exts.first) && exts.shift || nil
238+
format = exts.join('.') if exts.any? # join('.') is needed for multipart templates
239+
else # no extension, just format
240+
format = exts.last
257241
end
258242

259-
[base_path, name, locale, format, extension]
243+
[locale, format, extension]
260244
end
261245
end
262246
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'abstract_unit'
2+
3+
class TemplateTest < Test::Unit::TestCase
4+
def test_template_path_parsing
5+
with_options :base_path => nil, :name => 'abc', :locale => nil, :format => 'html', :extension => 'erb' do |t|
6+
t.assert_parses_template_path 'abc.en.html.erb', :locale => 'en'
7+
t.assert_parses_template_path 'abc.en.plain.html.erb', :locale => 'en', :format => 'plain.html'
8+
t.assert_parses_template_path 'abc.html.erb'
9+
t.assert_parses_template_path 'abc.plain.html.erb', :format => 'plain.html'
10+
t.assert_parses_template_path 'abc.erb', :format => nil
11+
t.assert_parses_template_path 'abc.html', :extension => nil
12+
13+
t.assert_parses_template_path '_abc.html.erb', :name => '_abc'
14+
15+
t.assert_parses_template_path 'test/abc.html.erb', :base_path => 'test'
16+
t.assert_parses_template_path './test/abc.html.erb', :base_path => './test'
17+
t.assert_parses_template_path '../test/abc.html.erb', :base_path => '../test'
18+
19+
t.assert_parses_template_path 'abc', :extension => nil, :format => nil, :name => nil
20+
t.assert_parses_template_path 'abc.xxx', :extension => nil, :format => 'xxx', :name => 'abc'
21+
t.assert_parses_template_path 'abc.html.xxx', :extension => nil, :format => 'xxx', :name => 'abc'
22+
end
23+
end
24+
25+
private
26+
def assert_parses_template_path(path, parse_results)
27+
template = ActionView::Template.new(path, '')
28+
parse_results.each_pair do |k, v|
29+
assert_block(%Q{Expected template to parse #{k.inspect} from "#{path}" as #{v.inspect}, but got #{template.send(k).inspect}}) { v == template.send(k) }
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)