提交 ae9f258e 编写于 作者: T thedarkone 提交者: Joshua Peek

Fix template extension parsing. [#2315 state:resolved] [#2284 state:resolved]

Signed-off-by: NJoshua Peek <josh@joshpeek.com>
上级 e3b166aa
......@@ -217,46 +217,30 @@ def valid_extension?(extension)
end
def valid_locale?(locale)
I18n.available_locales.include?(locale.to_sym)
locale && I18n.available_locales.include?(locale.to_sym)
end
# Returns file split into an array
# [base_path, name, locale, format, extension]
def split(file)
if m = file.to_s.match(/^(.*\/)?([^\.]+)\.(.*)$/)
base_path = m[1]
name = m[2]
extensions = m[3]
else
return
end
locale = nil
format = nil
extension = nil
if m = extensions.split(".")
if valid_locale?(m[0]) && m[1] && valid_extension?(m[2]) # All three
locale = m[0]
format = m[1]
extension = m[2]
elsif m[0] && m[1] && valid_extension?(m[2]) # Multipart formats
format = "#{m[0]}.#{m[1]}"
extension = m[2]
elsif valid_locale?(m[0]) && valid_extension?(m[1]) # locale and extension
locale = m[0]
extension = m[1]
elsif valid_extension?(m[1]) # format and extension
format = m[0]
extension = m[1]
elsif valid_extension?(m[0]) # Just extension
extension = m[0]
else # No extension
format = m[0]
end
end
[base_path, name, locale, format, extension]
[m[1], m[2], *parse_extensions(m[3])]
end
end
# returns parsed extensions as an array
# [locale, format, extension]
def parse_extensions(extensions)
exts = extensions.split(".")
if extension = valid_extension?(exts.last) && exts.pop || nil
locale = valid_locale?(exts.first) && exts.shift || nil
format = exts.join('.') if exts.any? # join('.') is needed for multipart templates
else # no extension, just format
format = exts.last
end
[locale, format, extension]
end
end
end
require 'abstract_unit'
class TemplateTest < Test::Unit::TestCase
def test_template_path_parsing
with_options :base_path => nil, :name => 'abc', :locale => nil, :format => 'html', :extension => 'erb' do |t|
t.assert_parses_template_path 'abc.en.html.erb', :locale => 'en'
t.assert_parses_template_path 'abc.en.plain.html.erb', :locale => 'en', :format => 'plain.html'
t.assert_parses_template_path 'abc.html.erb'
t.assert_parses_template_path 'abc.plain.html.erb', :format => 'plain.html'
t.assert_parses_template_path 'abc.erb', :format => nil
t.assert_parses_template_path 'abc.html', :extension => nil
t.assert_parses_template_path '_abc.html.erb', :name => '_abc'
t.assert_parses_template_path 'test/abc.html.erb', :base_path => 'test'
t.assert_parses_template_path './test/abc.html.erb', :base_path => './test'
t.assert_parses_template_path '../test/abc.html.erb', :base_path => '../test'
t.assert_parses_template_path 'abc', :extension => nil, :format => nil, :name => nil
t.assert_parses_template_path 'abc.xxx', :extension => nil, :format => 'xxx', :name => 'abc'
t.assert_parses_template_path 'abc.html.xxx', :extension => nil, :format => 'xxx', :name => 'abc'
end
end
private
def assert_parses_template_path(path, parse_results)
template = ActionView::Template.new(path, '')
parse_results.each_pair do |k, v|
assert_block(%Q{Expected template to parse #{k.inspect} from "#{path}" as #{v.inspect}, but got #{template.send(k).inspect}}) { v == template.send(k) }
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册