From 44ebab96da0ab47cc45c64a6efdd2cbb80f9d042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 8 Mar 2010 15:19:03 +0100 Subject: Rename Template::Lookup to LookupContext. --- actionpack/lib/action_view/base.rb | 27 +++------ actionpack/lib/action_view/lookup_context.rb | 80 ++++++++++++++++++++++++++ actionpack/lib/action_view/render/layouts.rb | 4 +- actionpack/lib/action_view/render/partials.rb | 2 +- actionpack/lib/action_view/render/rendering.rb | 4 +- actionpack/lib/action_view/template.rb | 1 - actionpack/lib/action_view/template/lookup.rb | 56 ------------------ 7 files changed, 93 insertions(+), 81 deletions(-) create mode 100644 actionpack/lib/action_view/lookup_context.rb delete mode 100644 actionpack/lib/action_view/template/lookup.rb (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 656d73e32d..d18f524060 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -242,12 +242,11 @@ module ActionView #:nodoc: klass = self end - klass.new(controller.template_lookup, {}, controller) + klass.new(controller.lookup_context, {}, controller) end - def initialize(template_lookup = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc: + def initialize(lookup_context = nil, assigns_for_first_render = {}, controller = nil, formats = nil) #:nodoc: @config = nil - @formats = formats @assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) } @helpers = self.class.helpers || Module.new @@ -256,27 +255,17 @@ module ActionView #:nodoc: @_content_for = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new } @_virtual_path = nil - @template_lookup = template_lookup.is_a?(ActionView::Template::Lookup) ? - template_lookup : ActionView::Template::Lookup.new(template_lookup) + @lookup_context = lookup_context.is_a?(ActionView::LookupContext) ? + lookup_context : ActionView::LookupContext.new(lookup_context) + @lookup_context.formats = formats if formats end attr_internal :controller, :template, :config - attr_reader :template_lookup - delegate :find, :formats, :formats=, :view_paths, :view_paths=, :to => :template_lookup + attr_reader :lookup_context - def update_details(details) - old_details = template_lookup.details - template_lookup.details = old_details.merge(details) - - if block_given? - begin - yield - ensure - template_lookup.details = old_details - end - end - end + delegate :find_template, :template_exists?, :formats, :formats=, + :view_paths, :view_paths=, :update_details, :to => :lookup_context def punctuate_body!(part) flush_output_buffer diff --git a/actionpack/lib/action_view/lookup_context.rb b/actionpack/lib/action_view/lookup_context.rb new file mode 100644 index 0000000000..82aebe1678 --- /dev/null +++ b/actionpack/lib/action_view/lookup_context.rb @@ -0,0 +1,80 @@ +module ActionView + # LookupContext is the object responsible to hold all information required to lookup + # templates, i.e. view paths and details. The LookupContext is also responsible to + # generate a key, given to view paths, used in the resolver cache lookup. Since + # this key is generated just once during the request, it speeds up all cache accesses. + class LookupContext #:nodoc: + attr_reader :details, :view_paths + + class DetailsKey #:nodoc: + attr_reader :details + alias :eql? :equal? + + @details_keys = Hash.new + + def self.get(details) + @details_keys[details] ||= new(details) + end + + def initialize(details) + @details, @hash = details, details.hash + end + end + + def initialize(view_paths, details = {}) + @details, @details_key = details, nil + self.view_paths = view_paths + end + + # Shortcut to read formats from details. + def formats + @details[:formats] + end + + # Shortcut to set formats in details. + def formats=(value) + self.details = @details.merge(:formats => Array(value)) + end + + # Whenever setting view paths, makes a copy so we can manipulate then in + # instance objects as we wish. + def view_paths=(paths) + @view_paths = ActionView::Base.process_view_paths(paths) + end + + # Setter for details. Everything this method is invoked, we need to nullify + # the details key if it changed. + def details=(details) + @details = details + @details_key = nil if @details_key && @details_key.details != details + end + + def details_key + @details_key ||= DetailsKey.get(details) unless details.empty? + end + + # Update the details keys by merging the given hash into the current + # details hash. If a block is given, the details are modified just during + # the execution of the block and reverted to the previous value after. + def update_details(new_details) + old_details = self.details + self.details = old_details.merge(new_details) + + if block_given? + begin + yield + ensure + self.details = old_details + end + end + end + + def find_template(name, prefix = nil, partial = false) + @view_paths.find(name, details, prefix, partial || false, details_key) + end + + def template_exists?(name, prefix = nil, partial = false) + @view_paths.exists?(name, details, prefix, partial || false, details_key) + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_view/render/layouts.rb b/actionpack/lib/action_view/render/layouts.rb index e1dbd3c120..b9c63e0090 100644 --- a/actionpack/lib/action_view/render/layouts.rb +++ b/actionpack/lib/action_view/render/layouts.rb @@ -48,10 +48,10 @@ module ActionView # the given name exists across all details before raising the error. def _find_layout(layout) #:nodoc: begin - find(layout) + find_template(layout) rescue ActionView::MissingTemplate => e update_details(:formats => nil) do - raise unless template_lookup.exists?(layout) + raise unless template_exists?(layout) end end end diff --git a/actionpack/lib/action_view/render/partials.rb b/actionpack/lib/action_view/render/partials.rb index 950c9d2cd8..0fe2d560f7 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(path, prefix, true) + @view.find_template(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 61fea6f49e..96c0b4fe6a 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -63,9 +63,9 @@ module ActionView elsif options.key?(:text) Template::Text.new(options[:text], self.formats.try(:first)) elsif options.key?(:file) - find(options[:file], options[:_prefix]) + find_template(options[:file], options[:_prefix]) elsif options.key?(:template) - find(options[:template], options[:_prefix]) + find_template(options[:template], options[:_prefix]) end end diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index c176359253..cd6b1930a1 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -13,7 +13,6 @@ module ActionView autoload :Handler autoload :Handlers autoload :Text - autoload :Lookup end extend Template::Handlers diff --git a/actionpack/lib/action_view/template/lookup.rb b/actionpack/lib/action_view/template/lookup.rb deleted file mode 100644 index 30de093934..0000000000 --- a/actionpack/lib/action_view/template/lookup.rb +++ /dev/null @@ -1,56 +0,0 @@ -module ActionView - class Template - class Lookup - attr_reader :details, :view_paths - - class DetailsKey - attr_reader :details - alias :eql? :equal? - - @details_keys = Hash.new - - def self.get(details) - @details_keys[details] ||= new(details) - end - - def initialize(details) - @details, @hash = details, details.hash - end - end - - def initialize(view_paths, details = {}) - @details = details - self.view_paths = view_paths - end - - def formats - @details[:formats] - end - - def formats=(value) - self.details = @details.merge(:formats => Array(value)) - end - - def view_paths=(paths) - @view_paths = ActionView::Base.process_view_paths(paths) - end - - def details=(details) - @details = details - @details_key = nil if @details_key && @details_key.details != details - end - - def details_key - @details_key ||= DetailsKey.get(details) unless details.empty? - end - - def find(name, prefix = nil, partial = false) - @view_paths.find(name, details, prefix, partial || false, details_key) - end - - def exists?(name, prefix = nil, partial = false) - @view_paths.exists?(name, details, prefix, partial || false, details_key) - end - end - end -end \ No newline at end of file -- cgit v1.2.3