diff options
author | José Valim <jose.valim@gmail.com> | 2010-03-08 23:13:24 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-03-08 23:25:16 +0100 |
commit | 8f082ff4217175f52234f2223658619a9c923afc (patch) | |
tree | 10b106df434109c5b5a117a06239a86be8266053 | |
parent | 01f0e47663bbbc593af0c36d4cf49124b200e3d8 (diff) | |
download | rails-8f082ff4217175f52234f2223658619a9c923afc.tar.gz rails-8f082ff4217175f52234f2223658619a9c923afc.tar.bz2 rails-8f082ff4217175f52234f2223658619a9c923afc.zip |
Clean LookupContext API.
-rw-r--r-- | actionpack/lib/abstract_controller/rendering.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/abstract_controller/view_paths.rb | 7 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/compatibility.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_view/base.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/lookup_context.rb | 10 | ||||
-rw-r--r-- | actionpack/lib/action_view/paths.rb | 29 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/layouts.rb | 4 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/partials.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/render/rendering.rb | 8 | ||||
-rw-r--r-- | actionpack/lib/action_view/template/resolver.rb | 16 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 6 |
11 files changed, 43 insertions, 46 deletions
diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index 77c554fa3f..5048754e33 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -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 diff --git a/actionpack/lib/abstract_controller/view_paths.rb b/actionpack/lib/abstract_controller/view_paths.rb index ec8f31cdfe..6513c0778e 100644 --- a/actionpack/lib/abstract_controller/view_paths.rb +++ b/actionpack/lib/abstract_controller/view_paths.rb @@ -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. # diff --git a/actionpack/lib/action_controller/metal/compatibility.rb b/actionpack/lib/action_controller/metal/compatibility.rb index 5cad0814ca..ab8d87b2c4 100644 --- a/actionpack/lib/action_controller/metal/compatibility.rb +++ b/actionpack/lib/action_controller/metal/compatibility.rb @@ -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 diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 38413560f3..564ea6684c 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -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, diff --git a/actionpack/lib/action_view/lookup_context.rb b/actionpack/lib/action_view/lookup_context.rb index 608f1ef818..4a4dc0d06c 100644 --- a/actionpack/lib/action_view/lookup_context.rb +++ b/actionpack/lib/action_view/lookup_context.rb @@ -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. diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb index 82a9f9a13c..35927d09d1 100644 --- a/actionpack/lib/action_view/paths.rb +++ b/actionpack/lib/action_view/paths.rb @@ -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 diff --git a/actionpack/lib/action_view/render/layouts.rb b/actionpack/lib/action_view/render/layouts.rb index 91a92a833a..8688de3d18 100644 --- a/actionpack/lib/action_view/render/layouts.rb +++ b/actionpack/lib/action_view/render/layouts.rb @@ -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 diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 0fe2d560f7..950c9d2cd8 100644 --- a/actionpack/lib/action_view/render/partials.rb +++ b/actionpack/lib/action_view/render/partials.rb @@ -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) diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 0322d4b641..47ea70f5ad 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -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 diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 9a011f7638..6e6c4c21ee 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -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) diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index ecfe14b797..e3c4869391 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -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 |