1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/lib/fixture_template.rb

67 lines
2 KiB
Ruby
Raw Normal View History

2009-03-23 18:45:01 -04:00
module ActionView #:nodoc:
class FixtureResolver < Resolver
2009-04-27 21:21:26 -04:00
def initialize(hash = {}, options = {})
super(options)
@hash = hash
end
def find_templates(name, details, prefix, partial)
if regexp = details_to_regexp(name, details, prefix, partial)
cached(regexp) do
templates = []
@hash.select { |k,v| k =~ regexp }.each do |path, source|
templates << Template.new(source, path, *path_to_details(path))
end
2009-06-23 17:45:27 -04:00
templates.sort_by {|t| -t.details.values.compact.size }
2009-03-23 18:45:01 -04:00
end
end
end
2009-04-27 21:21:26 -04:00
private
def formats_regexp
@formats_regexp ||= begin
formats = Mime::SET.symbols
2009-04-27 21:21:26 -04:00
'(?:' + formats.map { |l| "\\.#{Regexp.escape(l.to_s)}" }.join('|') + ')?'
end
2009-03-23 18:45:01 -04:00
end
2009-04-27 21:21:26 -04:00
def handler_regexp
e = TemplateHandlers.extensions.map{|h| "\\.#{Regexp.escape(h.to_s)}"}.join("|")
"(?:#{e})?"
2009-03-23 18:45:01 -04:00
end
2009-04-27 21:21:26 -04:00
def details_to_regexp(name, details, prefix, partial)
path = ""
path << "#{prefix}/" unless prefix.empty?
path << (partial ? "_#{name}" : name)
extensions = ""
[:locales, :formats].each do |k|
extensions << if exts = details[k]
'(?:' + exts.map {|e| "\\.#{Regexp.escape(e.to_s)}"}.join('|') + ')?'
else
k == :formats ? formats_regexp : ''
end
end
2009-06-23 17:45:27 -04:00
2009-05-20 20:22:29 -04:00
%r'^#{Regexp.escape(path)}#{extensions}#{handler_regexp}$'
2009-04-27 21:21:26 -04:00
end
# TODO: fix me
# :api: plugin
def path_to_details(path)
# [:erb, :format => :html, :locale => :en, :partial => true/false]
2009-06-23 17:45:27 -04:00
if m = path.match(%r'(_)?[\w-]+((?:\.[\w-]+)*)\.(\w+)$')
2009-04-27 21:21:26 -04:00
partial = m[1] == '_'
details = (m[2]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[3])
format = Mime[details.last] && details.pop.to_sym
locale = details.last && details.pop.to_sym
return handler, :format => format, :locale => locale, :partial => partial
end
2009-03-23 18:45:01 -04:00
end
end
end