Clean LookupContext API.

This commit is contained in:
José Valim 2010-03-08 23:13:24 +01:00
parent 01f0e47663
commit 8f082ff421
11 changed files with 43 additions and 46 deletions

View File

@ -97,7 +97,7 @@ module AbstractController
end
if (options.keys & [:partial, :file, :template]).empty?
options[:_prefix] ||= _prefix
options[:prefix] ||= _prefix
end
options[:template] ||= (options[:action] || action_name).to_s

View File

@ -7,8 +7,7 @@ module AbstractController
self._view_paths = ActionView::PathSet.new
end
delegate :find_template, :template_exists?,
:view_paths, :formats, :formats=, :to => :lookup_context
delegate :template_exists?, :view_paths, :formats, :formats=, :to => :lookup_context
# LookupContext is the object responsible to hold all information required to lookup
# templates, i.e. view paths and details. Check ActionView::LookupContext for more
@ -29,6 +28,10 @@ module AbstractController
lookup_context.view_paths.unshift(*path)
end
def template_exists?(*args)
lookup_context.exists?(*args)
end
module ClassMethods
# Append a path to the list of view paths for this controller.
#

View File

@ -50,8 +50,9 @@ module ActionController
end
def _normalize_options(options)
# TODO Deprecate this. Rails 2.x allowed to give a template as action.
if options[:action] && options[:action].to_s.include?(?/)
ActiveSupport::Deprecation.warn "Giving a path to render :action is deprecated. " <<
"Please use render :template instead", caller
options[:template] = options.delete(:action)
end

View File

@ -194,7 +194,7 @@ module ActionView #:nodoc:
attr_accessor :base_path, :assigns, :template_extension, :lookup_context
attr_internal :captures, :request, :layout, :controller, :template, :config
delegate :find_template, :template_exists?, :formats, :formats=,
delegate :find, :exists?, :formats, :formats=,
:view_paths, :view_paths=, :with_fallbacks, :update_details, :to => :lookup_context
delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers,

View File

@ -59,19 +59,19 @@ module ActionView
@view_paths = ActionView::Base.process_view_paths(paths)
end
def find_template(name, prefix = nil, partial = false)
def find(name, prefix = nil, partial = false)
key = details_key
@view_paths.find(name, key.details, prefix, partial || false, key)
@view_paths.find(name, prefix, partial || false, key.details, key)
end
def find_all(name, prefix = nil, partial = false)
key = details_key
@view_paths.find_all(name, key.details, prefix, partial || false, key)
@view_paths.find_all(name, prefix, partial || false, key.details, key)
end
def template_exists?(name, prefix = nil, partial = false)
def exists?(name, prefix = nil, partial = false)
key = details_key
@view_paths.exists?(name, key.details, prefix, partial || false, key)
@view_paths.exists?(name, prefix, partial || false, key.details, key)
end
# Add fallbacks to the view paths. Useful in cases you are rendering a file.

View File

@ -9,31 +9,22 @@ module ActionView #:nodoc:
METHOD
end
def find_all(path, details = {}, prefix = nil, partial = false, key=nil)
def find(path, prefix = nil, partial = false, details = {}, key = nil)
template = find_all(path, prefix, partial, details, key).first
raise MissingTemplate.new(self, "#{prefix}/#{path}", details, partial) unless template
template
end
def find_all(*args)
each do |resolver|
templates = resolver.find_all(path, details, prefix, partial, key)
templates = resolver.find_all(*args)
return templates unless templates.empty?
end
[]
end
def find(path, details = {}, prefix = nil, partial = false, key=nil)
each do |resolver|
if template = resolver.find(path, details, prefix, partial, key)
return template
end
end
raise ActionView::MissingTemplate.new(self, "#{prefix}/#{path}", details, partial)
end
def exists?(path, details = {}, prefix = nil, partial = false, key=nil)
each do |resolver|
if resolver.find(path, details, prefix, partial, key)
return true
end
end
false
def exists?(*args)
find_all(*args).any?
end
protected

View File

@ -49,10 +49,10 @@ module ActionView
def _find_layout(layout) #:nodoc:
begin
layout =~ /^\// ?
with_fallbacks { find_template(layout) } : find_template(layout)
with_fallbacks { find(layout) } : find(layout)
rescue ActionView::MissingTemplate => e
update_details(:formats => nil) do
raise unless template_exists?(layout)
raise unless exists?(layout)
end
end
end

View File

@ -294,7 +294,7 @@ module ActionView
def find_template(path=@path)
return path unless path.is_a?(String)
prefix = @view.controller_path unless path.include?(?/)
@view.find_template(path, prefix, true)
@view.find(path, prefix, true)
end
def partial_path(object = @object)

View File

@ -29,8 +29,6 @@ module ActionView
end
# This is the API to render a ViewContext's template from a controller.
# TODO Review this name since it does not render only templates, but also
# partials, files and so forth.
def render_template(options, &block)
_evaluate_assigns_and_ivars
@ -62,12 +60,12 @@ module ActionView
Template.new(options[:inline], "inline template", handler, {})
elsif options.key?(:text)
Template::Text.new(options[:text], self.formats.try(:first))
elsif options.key?(:file)
with_fallbacks { find_template(options[:file], options[:_prefix]) }
elsif options.key?(:_template)
options[:_template]
elsif options.key?(:file)
with_fallbacks { find(options[:file], options[:prefix]) }
elsif options.key?(:template)
find_template(options[:template], options[:_prefix])
find(options[:template], options[:prefix])
end
end

View File

@ -10,16 +10,20 @@ module ActionView
Hash.new { |h2,k2| h2[k2] = Hash.new { |h3, k3| h3[k3] = {} } } }
end
def clear_cache
@cached.clear
end
def find(*args)
find_all(*args).first
end
# Normalizes the arguments and passes it on to find_template.
def find_all(name, details = {}, prefix = nil, partial = nil, key=nil)
def find_all(name, prefix=nil, partial=false, details={}, key=nil)
name, prefix = normalize_name(name, prefix)
cached(key, prefix, name, partial) do
find_templates(name, details, prefix, partial)
find_templates(name, prefix, partial, details)
end
end
@ -32,7 +36,7 @@ module ActionView
# This is what child classes implement. No defaults are needed
# because Resolver guarantees that the arguments are present and
# normalized.
def find_templates(name, details, prefix, partial)
def find_templates(name, prefix, partial, details)
raise NotImplementedError
end
@ -68,12 +72,12 @@ module ActionView
private
def find_templates(name, details, prefix, partial)
path = build_path(name, details, prefix, partial)
def find_templates(name, prefix, partial, details)
path = build_path(name, prefix, partial, details)
query(path, EXTENSION_ORDER.map { |ext| details[ext] })
end
def build_path(name, details, prefix, partial)
def build_path(name, prefix, partial, details)
path = ""
path << "#{prefix}/" unless prefix.empty?
path << (partial ? "_#{name}" : name)

View File

@ -355,7 +355,7 @@ class TestController < ActionController::Base
@before = "i'm before the render"
render_to_string :text => "foo"
@after = "i'm after the render"
render :action => "test/hello_world"
render :template => "test/hello_world"
end
def render_to_string_with_exception
@ -369,7 +369,7 @@ class TestController < ActionController::Base
rescue
end
@after = "i'm after the render"
render :action => "test/hello_world"
render :template => "test/hello_world"
end
def accessing_params_in_template_with_layout
@ -514,7 +514,7 @@ class TestController < ActionController::Base
def render_to_string_with_partial
@partial_only = render_to_string :partial => "partial_only"
@partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") }
render :action => "test/hello_world"
render :template => "test/hello_world"
end
def partial_with_counter