Clean up the API required from ActionView::Template.

This commit is contained in:
José Valim 2010-03-09 13:12:11 +01:00
parent c507e16dba
commit 00d6271d2b
6 changed files with 37 additions and 46 deletions

View File

@ -617,9 +617,9 @@ module ActionMailer #:nodoc:
def each_template(paths, name, &block) #:nodoc:
Array(paths).each do |path|
templates = lookup_context.find_all(name, path)
templates = templates.uniq_by { |t| t.formats }
unless templates.empty?
templates = templates.uniq_by { |t| t.details[:formats] }
templates.each(&block)
return
end

View File

@ -108,7 +108,7 @@ module AbstractController
end
def _with_template_hook(template)
self.formats = template.details[:formats]
self.formats = template.formats
end
end
end

View File

@ -16,23 +16,22 @@ module ActionView
end
extend Template::Handlers
attr_reader :source, :identifier, :handler, :mime_type, :formats, :details
attr_reader :source, :identifier, :handler, :virtual_path, :formats
def initialize(source, identifier, handler, details)
@source = source
@identifier = identifier
@handler = handler
@details = details
@partial = details[:partial]
@virtual_path = details[:virtual_path]
@method_names = {}
format = details.delete(:format) || begin
# TODO: Clean this up
handler.respond_to?(:default_format) ? handler.default_format.to_sym.to_s : "html"
end
@mime_type = Mime::Type.lookup_by_extension(format.to_s)
@formats = [format.to_sym]
@formats << :html if format == :js
@details[:formats] = Array.wrap(format.to_sym)
format = details[:format]
format ||= handler.default_format.to_sym if handler.respond_to?(:default_format)
format ||= :html
@formats = [format.to_sym]
end
def render(view, locals, &block)
@ -47,19 +46,20 @@ module ActionView
end
end
# TODO: Figure out how to abstract this
def variable_name
@variable_name ||= identifier[%r'_?(\w+)(\.\w+)*$', 1].to_sym
def mime_type
@mime_type ||= Mime::Type.lookup_by_extension(@formats.first.to_s) if @formats.first
end
def variable_name
@variable_name ||= @virtual_path[%r'_?(\w+)(\.\w+)*$', 1].to_sym
end
# TODO: Figure out how to abstract this
def counter_name
@counter_name ||= "#{variable_name}_counter".to_sym
end
# TODO: kill hax
def partial?
@details[:partial]
@partial
end
def inspect
@ -87,7 +87,7 @@ module ActionView
source = <<-end_src
def #{method_name}(local_assigns)
_old_virtual_path, @_virtual_path = @_virtual_path, #{@details[:virtual_path].inspect};_old_output_buffer = output_buffer;#{locals_code};#{code}
_old_virtual_path, @_virtual_path = @_virtual_path, #{@virtual_path.inspect};_old_output_buffer = output_buffer;#{locals_code};#{code}
ensure
@_virtual_path, self.output_buffer = _old_virtual_path, _old_output_buffer
end

View File

@ -74,7 +74,7 @@ module ActionView
def find_templates(name, prefix, partial, details)
path = build_path(name, prefix, partial, details)
query(path, EXTENSION_ORDER.map { |ext| details[ext] })
query(partial, path, EXTENSION_ORDER.map { |ext| details[ext] })
end
def build_path(name, prefix, partial, details)
@ -84,34 +84,27 @@ module ActionView
path
end
def query(path, exts)
def query(partial, path, exts)
query = File.join(@path, path)
exts.each do |ext|
query << '{' << ext.map {|e| e && ".#{e}" }.join(',') << '}'
end
Dir[query].reject { |p| File.directory?(p) }.map do |p|
Template.new(File.read(p), File.expand_path(p), *path_to_details(p))
handler, format = extract_handler_and_format(p)
Template.new(File.read(p), File.expand_path(p), handler,
:partial => partial, :virtual_path => path, :format => format)
end
end
# # TODO: fix me
# # :api: plugin
def path_to_details(path)
# [:erb, :format => :html, :locale => :en, :partial => true/false]
if m = path.match(%r'((^|.*/)(_)?[\w-]+)((?:\.[\w-]+)*)\.(\w+)$')
partial = m[3] == '_'
details = (m[4]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[5])
def extract_handler_and_format(path)
pieces = File.basename(path).split(".")
pieces.shift
format = Mime[details.last] && details.pop.to_sym
locale = details.last && details.pop.to_sym
virtual_path = (m[1].gsub("#{@path}/", "") << details.join("."))
return handler, :format => format, :locale => locale, :partial => partial,
:virtual_path => virtual_path
end
handler = Template.handler_class_for_extension(pieces.pop)
format = pieces.last && Mime[pieces.last] && pieces.pop.to_sym
[handler, format]
end
end

View File

@ -7,10 +7,6 @@ module ActionView #:nodoc:
@content_type ||= Mime::TEXT
end
def details
{:formats => [@content_type.to_sym]}
end
def identifier
'text template'
end
@ -28,7 +24,7 @@ module ActionView #:nodoc:
end
def formats
[mime_type]
[@content_type.to_sym]
end
def partial?

View File

@ -7,7 +7,7 @@ module ActionView #:nodoc:
private
def query(path, exts)
def query(partial, path, exts)
query = Regexp.escape(path)
exts.each do |ext|
query << '(?:' << ext.map {|e| e && Regexp.escape(".#{e}") }.join('|') << ')'
@ -15,9 +15,11 @@ module ActionView #:nodoc:
templates = []
@hash.select { |k,v| k =~ /^#{query}$/ }.each do |path, source|
templates << Template.new(source, path, *path_to_details(path))
handler, format = extract_handler_and_format(path)
templates << Template.new(source, path, handler,
:partial => partial, :virtual_path => path, :format => format)
end
templates.sort_by {|t| -t.details.values.compact.size }
templates.sort_by {|t| -t.formats.size }
end
end