Skip to content
Browse files

Clean LookupContext API.

  • Loading branch information...
1 parent 01f0e47 commit 8f082ff4217175f52234f2223658619a9c923afc @josevalim josevalim committed
View
2 actionpack/lib/abstract_controller/rendering.rb
@@ -97,7 +97,7 @@ def _normalize_options(options)
end
if (options.keys & [:partial, :file, :template]).empty?
- options[:_prefix] ||= _prefix
+ options[:prefix] ||= _prefix
end
options[:template] ||= (options[:action] || action_name).to_s
View
7 actionpack/lib/abstract_controller/view_paths.rb
@@ -7,8 +7,7 @@ module ViewPaths
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 @@ def prepend_view_path(path)
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
3 actionpack/lib/action_controller/metal/compatibility.rb
@@ -50,8 +50,9 @@ def process_action(*)
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
2 actionpack/lib/action_view/base.rb
@@ -194,7 +194,7 @@ class << self
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
10 actionpack/lib/action_view/lookup_context.rb
@@ -59,19 +59,19 @@ def view_paths=(paths)
@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
29 actionpack/lib/action_view/paths.rb
@@ -9,31 +9,22 @@ def #{method}(*args)
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
4 actionpack/lib/action_view/render/layouts.rb
@@ -49,10 +49,10 @@ def _layout_for(name = nil, &block) #:nodoc:
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
2 actionpack/lib/action_view/render/partials.rb
@@ -294,7 +294,7 @@ def collection
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
8 actionpack/lib/action_view/render/rendering.rb
@@ -29,8 +29,6 @@ def render(options = {}, locals = {}, &block) #:nodoc:
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 @@ def _determine_template(options) #:nodoc:
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
16 actionpack/lib/action_view/template/resolver.rb
@@ -10,16 +10,20 @@ def initialize
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 @@ def caching?
# 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 @@ def to_s
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
6 actionpack/test/controller/render_test.rb
@@ -355,7 +355,7 @@ def render_to_string_with_assigns
@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 @@ def render_to_string_with_caught_exception
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 @@ def partial_only_with_layout
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

0 comments on commit 8f082ff

Please sign in to comment.
Something went wrong with that request. Please try again.