提交 44ebab96 编写于 作者: J José Valim

Rename Template::Lookup to LookupContext.

上级 ea68fe59
......@@ -18,39 +18,26 @@ module ViewPaths
self._view_paths = ActionView::PathSet.new
end
delegate :formats, :formats=, :to => :template_lookup
delegate :_view_paths, :to => :'self.class'
delegate :find_template, :template_exists?,
:view_paths, :formats, :formats=, :to => :lookup_context
def template_lookup
@template_lookup ||= ActionView::Template::Lookup.new(_view_paths, details_for_lookup)
# LookupContext is the object responsible to hold all information required to lookup
# templates, i.e. view paths and details. Check ActionView::LookupContext for more
# information.
def lookup_context
@lookup_context ||= ActionView::LookupContext.new(self.class._view_paths, details_for_lookup)
end
def details_for_lookup
{ }
end
# The list of view paths for this controller. See ActionView::ViewPathSet for
# more details about writing custom view paths.
def view_paths
template_lookup.view_paths
end
def append_view_path(path)
template_lookup.view_paths.push(*path)
lookup_context.view_paths.push(*path)
end
def prepend_view_path(path)
template_lookup.view_paths.unshift(*path)
end
protected
def template_exists?(*args)
template_lookup.exists?(*args)
end
def find_template(*args)
template_lookup.find(*args)
lookup_context.view_paths.unshift(*path)
end
module ClassMethods
......@@ -191,12 +178,12 @@ def _normalize_options(options)
options[:template] ||= (options[:action] || action_name).to_s
details = _normalize_details(options)
template_lookup.details = details
lookup_context.update_details(details)
options
end
def _normalize_details(options)
details = template_lookup.details
details = {}
details[:formats] = Array(options[:format]) if options[:format]
details[:locale] = Array(options[:locale]) if options[:locale]
details
......
......@@ -42,6 +42,7 @@ module ActionView
autoload :Rendering
end
autoload :LookupContext, 'action_view/lookup_context'
autoload :MissingTemplate, 'action_view/base'
autoload :Resolver, 'action_view/template/resolver'
autoload :PathResolver, 'action_view/template/resolver'
......
......@@ -242,12 +242,11 @@ def inspect
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 @@ def initialize(template_lookup = nil, assigns_for_first_render = {}, controller
@_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
......
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
......@@ -48,10 +48,10 @@ def _layout_for(name = nil, &block) #:nodoc:
# 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
......
......@@ -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(path, prefix, true)
@view.find_template(path, prefix, true)
end
def partial_path(object = @object)
......
......@@ -63,9 +63,9 @@ def _determine_template(options) #:nodoc:
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
......
......@@ -13,7 +13,6 @@ class Template
autoload :Handler
autoload :Handlers
autoload :Text
autoload :Lookup
end
extend Template::Handlers
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册