From 04d4537cd40d0415d15af4395213632735c8683f Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 9 Aug 2009 07:14:24 -0300 Subject: This change causes some failing tests, but it should be possible to make them pass with minimal performance impact. --- actionpack/lib/action_view/template/resolver.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index ebfc6cc8ce..3bd2acae7a 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -41,8 +41,10 @@ module ActionView end def handler_glob - e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join - "{#{e}}" + @handler_glob ||= begin + e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join + "{#{e}}" + end end def formats_glob -- cgit v1.2.3 From 02d9dd900048407ef555cf09b0038a57ae924b0a Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 9 Aug 2009 09:46:50 -0300 Subject: Add some more caching to the lookup --- actionpack/lib/action_view/template/resolver.rb | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 3bd2acae7a..10f664736f 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -62,6 +62,10 @@ module ActionView class FileSystemResolver < Resolver + def self.cached_glob + @@cached_glob ||= {} + end + def initialize(path, options = {}) raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver) super(options) @@ -107,20 +111,22 @@ module ActionView # :api: plugin def details_to_glob(name, details, prefix, partial, root) - path = "" - path << "#{prefix}/" unless prefix.empty? - path << (partial ? "_#{name}" : name) - - extensions = "" - [:locales, :formats].each do |k| - extensions << if exts = details[k] - '{' + exts.map {|e| ".#{e},"}.join + '}' - else - k == :formats ? formats_glob : '' + self.class.cached_glob[[name, prefix, partial, details, root]] ||= begin + path = "" + path << "#{prefix}/" unless prefix.empty? + path << (partial ? "_#{name}" : name) + + extensions = "" + [:locales, :formats].each do |k| + extensions << if exts = details[k] + '{' + exts.map {|e| ".#{e},"}.join + '}' + else + k == :formats ? formats_glob : '' + end end - end - "#{root}#{path}#{extensions}#{handler_glob}" + "#{root}#{path}#{extensions}#{handler_glob}" + end end # TODO: fix me -- cgit v1.2.3 From 27adcd1c1a5cc566cfa8c5f8268b65ef01a7e865 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 11 Aug 2009 15:02:05 -0700 Subject: Clean up ActionView some: * Call _evaluate_assigns_and_ivars at the two entry points so we don't have to do a check at every render. * Make template.render viable without having to go through a wrapper method * Remove old TemplateHandler#render(template, local_assigns) path so we don't have to set self.template every time we render a template. * Move Template rescuing code to Template#render so it gets caught every time. * Pull in some tests from Pratik that test render @object in ActionView --- actionpack/lib/action_view/template/handler.rb | 2 +- actionpack/lib/action_view/template/template.rb | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/handler.rb b/actionpack/lib/action_view/template/handler.rb index 3071c78174..7311e5e12f 100644 --- a/actionpack/lib/action_view/template/handler.rb +++ b/actionpack/lib/action_view/template/handler.rb @@ -26,7 +26,7 @@ module ActionView self.default_format = Mime::HTML def self.call(template) - "#{name}.new(self).render(template, local_assigns)" + raise "Need to implement #{self.class.name}#call(template)" end def initialize(view = nil) diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index 33d3f79ad3..f6270e5616 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -26,9 +26,17 @@ module ActionView @details[:formats] = Array.wrap(format.to_sym) end - def render(view, locals, &blk) + def render(view, locals, &block) method_name = compile(locals, view) - view.send(method_name, locals, &blk) + block ||= proc {|*names| view._layout_for(names) } + view.send(method_name, locals, &block) + rescue Exception => e + if e.is_a?(TemplateError) + e.sub_template_of(self) + raise e + else + raise TemplateError.new(self, view.assigns, e) + end end # TODO: Figure out how to abstract this -- cgit v1.2.3 From 9f5cd0156ab907d8097fc9c588823a9b09038b93 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 11 Aug 2009 23:43:15 -0700 Subject: More cleanup of ActionView and reduction in need for blocks in some cases: * only one of partial_name or :as will be available as a local * `object` is removed * Simplify _layout_for in most cases. * Remove <% render :partial do |args| %> * <% render :partial do %> still works fine --- actionpack/lib/action_view/template/handler.rb | 4 ---- actionpack/lib/action_view/template/template.rb | 1 - 2 files changed, 5 deletions(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/handler.rb b/actionpack/lib/action_view/template/handler.rb index 7311e5e12f..4bf58b9fa8 100644 --- a/actionpack/lib/action_view/template/handler.rb +++ b/actionpack/lib/action_view/template/handler.rb @@ -29,10 +29,6 @@ module ActionView raise "Need to implement #{self.class.name}#call(template)" end - def initialize(view = nil) - @view = view - end - def render(template, local_assigns) raise "Need to implement #{self.class.name}#render(template, local_assigns)" end diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index f6270e5616..8f23f9b9e3 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -28,7 +28,6 @@ module ActionView def render(view, locals, &block) method_name = compile(locals, view) - block ||= proc {|*names| view._layout_for(names) } view.send(method_name, locals, &block) rescue Exception => e if e.is_a?(TemplateError) -- cgit v1.2.3 From 9b552fb300c4606fe517eadaa30708e9d75498a6 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 14 Aug 2009 12:00:52 -0700 Subject: Caches and cache clearing seems to actually work, but the actual architecture is kind of messy. Next: CLEAN UP. --- actionpack/lib/action_view/template/template.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index 8f23f9b9e3..7d6964e3e3 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -97,7 +97,22 @@ module ActionView raise ActionView::TemplateError.new(self, {}, e) end end - + + class LocalsKey + @hash_keys = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = {} } } + + def self.get(*locals) + @hash_keys[*locals] ||= new(klass, format, locale) + end + + attr_accessor :hash + def initialize(klass, format, locale) + @hash = locals.hash + end + + alias_method :eql?, :equal? + end + def build_method_name(locals) # TODO: is locals.keys.hash reliably the same? @method_names[locals.keys.hash] ||= -- cgit v1.2.3 From 1310231c15742bf7d99e2f143d88b383c32782d3 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 14 Aug 2009 22:32:40 -0700 Subject: Got tests to pass with some more changes. * request.formats is much simpler now * For XHRs or Accept headers with a single item, we use the Accept header * For other requests, we use params[:format] or fallback to HTML * This is primarily to work around the fact that browsers provide completely broken Accept headers, so we have to whitelist the few cases we can specifically isolate and treat other requests as coming from the browser * For APIs, we can support single-item Accept headers, which disambiguates from the browsers * Requests to an action that only has an XML template from the browser will no longer find the template. This worked previously because most browsers provide a catch-all */*, but this was mostly accidental behavior. If you want to serve XML, either use the :xml format in links, or explicitly specify the XML template: render "template.xml". --- actionpack/lib/action_view/template/resolver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 10f664736f..fe657166d5 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -42,7 +42,7 @@ module ActionView def handler_glob @handler_glob ||= begin - e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join + e = TemplateHandlers.extensions.map{|h| ".#{h}"}.join(",") "{#{e}}" end end -- cgit v1.2.3 From 78ced08338e227f6cc380bb3e0f2ee8cda131d9a Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 26 Aug 2009 19:45:33 -0700 Subject: Add a default parameter for Resolver#initialize --- actionpack/lib/action_view/template/resolver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_view/template') diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index fe657166d5..0b4c62d4d0 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -4,7 +4,7 @@ require "action_view/template/template" module ActionView # Abstract superclass class Resolver - def initialize(options) + def initialize(options = {}) @cache = options[:cache] @cached = {} end -- cgit v1.2.3