2012-12-13 08:47:33 -05:00
|
|
|
require 'thread_safe'
|
2011-09-21 18:28:05 -04:00
|
|
|
require 'active_support/core_ext/module/remove_method'
|
2013-05-05 06:24:06 -04:00
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
2010-03-08 14:39:15 -05:00
|
|
|
|
2010-03-08 09:19:03 -05:00
|
|
|
module ActionView
|
2010-06-16 14:27:50 -04:00
|
|
|
# = Action View Lookup Context
|
|
|
|
#
|
2010-03-08 09:19:03 -05:00
|
|
|
# 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:
|
2012-02-21 20:55:56 -05:00
|
|
|
attr_accessor :prefixes, :rendered_format
|
2011-06-05 11:34:40 -04:00
|
|
|
|
2010-03-08 10:32:40 -05:00
|
|
|
mattr_accessor :fallbacks
|
2010-10-10 17:11:50 -04:00
|
|
|
@@fallbacks = FallbackFileSystemResolver.instances
|
2010-03-08 10:32:40 -05:00
|
|
|
|
2010-03-08 14:39:15 -05:00
|
|
|
mattr_accessor :registered_details
|
2010-03-11 06:45:05 -05:00
|
|
|
self.registered_details = []
|
2010-03-08 14:39:15 -05:00
|
|
|
|
2010-03-12 05:50:45 -05:00
|
|
|
def self.register_detail(name, options = {}, &block)
|
2010-03-11 06:45:05 -05:00
|
|
|
self.registered_details << name
|
2011-12-08 09:23:23 -05:00
|
|
|
initialize = registered_details.map { |n| "@details[:#{n}] = details[:#{n}] || default_#{n}" }
|
2010-06-07 04:13:41 -04:00
|
|
|
|
2011-09-22 09:03:05 -04:00
|
|
|
Accessors.send :define_method, :"default_#{name}", &block
|
2010-03-19 12:20:15 -04:00
|
|
|
Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
|
|
|
def #{name}
|
2012-06-22 19:21:58 -04:00
|
|
|
@details.fetch(:#{name}, [])
|
2010-03-19 12:20:15 -04:00
|
|
|
end
|
2010-03-11 06:45:05 -05:00
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
def #{name}=(value)
|
2012-01-05 15:18:54 -05:00
|
|
|
value = value.present? ? Array(value) : default_#{name}
|
2010-06-07 04:13:41 -04:00
|
|
|
_set_detail(:#{name}, value) if value != @details[:#{name}]
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
2011-09-21 18:28:05 -04:00
|
|
|
|
|
|
|
remove_possible_method :initialize_details
|
|
|
|
def initialize_details(details)
|
|
|
|
#{initialize.join("\n")}
|
|
|
|
end
|
2010-03-12 05:50:45 -05:00
|
|
|
METHOD
|
2010-03-08 14:39:15 -05:00
|
|
|
end
|
|
|
|
|
2010-03-19 12:20:15 -04:00
|
|
|
# Holds accessors for the registered details.
|
|
|
|
module Accessors #:nodoc:
|
2010-03-11 06:45:05 -05:00
|
|
|
end
|
|
|
|
|
2013-03-14 17:02:20 -04:00
|
|
|
register_detail(:locale) do
|
|
|
|
locales = [I18n.locale]
|
|
|
|
locales.concat(I18n.fallbacks[I18n.locale]) if I18n.respond_to? :fallbacks
|
|
|
|
locales << I18n.default_locale
|
|
|
|
locales.uniq!
|
|
|
|
locales
|
|
|
|
end
|
2012-06-15 14:36:09 -04:00
|
|
|
register_detail(:formats) { ActionView::Base.default_formats || [:html, :text, :js, :css, :xml, :json] }
|
2011-09-22 09:06:04 -04:00
|
|
|
register_detail(:handlers){ Template::Handlers.extensions }
|
2010-03-08 14:39:15 -05:00
|
|
|
|
2010-03-08 09:19:03 -05:00
|
|
|
class DetailsKey #:nodoc:
|
|
|
|
alias :eql? :equal?
|
2010-03-14 05:25:29 -04:00
|
|
|
alias :object_hash :hash
|
2010-03-08 09:19:03 -05:00
|
|
|
|
2010-03-14 05:25:29 -04:00
|
|
|
attr_reader :hash
|
2012-12-13 08:47:33 -05:00
|
|
|
@details_keys = ThreadSafe::Cache.new
|
2010-03-08 09:19:03 -05:00
|
|
|
|
|
|
|
def self.get(details)
|
2011-12-13 14:25:03 -05:00
|
|
|
@details_keys[details] ||= new
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
|
|
|
|
2011-12-14 03:23:34 -05:00
|
|
|
def self.clear
|
|
|
|
@details_keys.clear
|
|
|
|
end
|
|
|
|
|
2010-03-14 05:25:29 -04:00
|
|
|
def initialize
|
|
|
|
@hash = object_hash
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Add caching behavior on top of Details.
|
|
|
|
module DetailsCache
|
|
|
|
attr_accessor :cache
|
2010-06-07 04:13:41 -04:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Calculate the details key. Remove the handlers from calculation to improve performance
|
|
|
|
# since the user cannot modify it explicitly.
|
|
|
|
def details_key #:nodoc:
|
|
|
|
@details_key ||= DetailsKey.get(@details) if @cache
|
|
|
|
end
|
|
|
|
|
|
|
|
# Temporary skip passing the details_key forward.
|
|
|
|
def disable_cache
|
|
|
|
old_value, @cache = @cache, false
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@cache = old_value
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def _set_detail(key, value)
|
2011-12-13 18:26:29 -05:00
|
|
|
@details = @details.dup if @details_key
|
2011-09-21 18:28:05 -04:00
|
|
|
@details_key = nil
|
2011-12-13 14:13:03 -05:00
|
|
|
@details[key] = value
|
2010-06-07 04:13:41 -04:00
|
|
|
end
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Helpers related to template lookup using the lookup context information.
|
2010-03-08 14:39:15 -05:00
|
|
|
module ViewPaths
|
2012-07-11 04:18:52 -04:00
|
|
|
attr_reader :view_paths, :html_fallback_for_js
|
2010-03-08 09:19:03 -05:00
|
|
|
|
2010-03-08 14:39:15 -05:00
|
|
|
# Whenever setting view paths, makes a copy so we can manipulate then in
|
|
|
|
# instance objects as we wish.
|
|
|
|
def view_paths=(paths)
|
2012-01-05 15:08:18 -05:00
|
|
|
@view_paths = ActionView::PathSet.new(Array(paths))
|
2010-03-08 14:39:15 -05:00
|
|
|
end
|
2010-03-08 09:19:03 -05:00
|
|
|
|
2011-09-22 05:54:13 -04:00
|
|
|
def find(name, prefixes = [], partial = false, keys = [], options = {})
|
|
|
|
@view_paths.find(*args_for_lookup(name, prefixes, partial, keys, options))
|
2010-03-08 14:39:15 -05:00
|
|
|
end
|
2010-03-12 08:25:10 -05:00
|
|
|
alias :find_template :find
|
2010-03-08 09:19:03 -05:00
|
|
|
|
2011-09-22 05:54:13 -04:00
|
|
|
def find_all(name, prefixes = [], partial = false, keys = [], options = {})
|
|
|
|
@view_paths.find_all(*args_for_lookup(name, prefixes, partial, keys, options))
|
2010-03-08 14:57:33 -05:00
|
|
|
end
|
|
|
|
|
2011-09-22 05:54:13 -04:00
|
|
|
def exists?(name, prefixes = [], partial = false, keys = [], options = {})
|
|
|
|
@view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys, options))
|
2010-03-08 14:39:15 -05:00
|
|
|
end
|
2010-03-12 08:25:10 -05:00
|
|
|
alias :template_exists? :exists?
|
2010-03-08 09:19:03 -05:00
|
|
|
|
2010-03-11 06:45:05 -05:00
|
|
|
# Add fallbacks to the view paths. Useful in cases you are rendering a :file.
|
2010-03-08 14:39:15 -05:00
|
|
|
def with_fallbacks
|
|
|
|
added_resolvers = 0
|
|
|
|
self.class.fallbacks.each do |resolver|
|
|
|
|
next if view_paths.include?(resolver)
|
|
|
|
view_paths.push(resolver)
|
|
|
|
added_resolvers += 1
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
2010-03-08 14:39:15 -05:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
added_resolvers.times { view_paths.pop }
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
2010-03-15 21:08:34 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-09-22 05:54:13 -04:00
|
|
|
def args_for_lookup(name, prefixes, partial, keys, details_options) #:nodoc:
|
2010-09-17 16:39:14 -04:00
|
|
|
name, prefixes = normalize_name(name, prefixes)
|
2011-09-22 05:54:13 -04:00
|
|
|
details, details_key = detail_args_for(details_options)
|
|
|
|
[name, prefixes, partial || false, details, details_key, keys]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Compute details hash and key according to user options (e.g. passed from #render).
|
|
|
|
def detail_args_for(options)
|
|
|
|
return @details, details_key if options.empty? # most common path.
|
|
|
|
user_details = @details.merge(options)
|
|
|
|
[user_details, DetailsKey.get(user_details)]
|
2010-03-15 21:08:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Support legacy foo.erb names even though we now ignore .erb
|
|
|
|
# as well as incorrectly putting part of the path in the template
|
|
|
|
# name instead of the prefix.
|
2010-09-17 16:39:14 -04:00
|
|
|
def normalize_name(name, prefixes) #:nodoc:
|
2012-09-14 01:47:22 -04:00
|
|
|
prefixes = prefixes.presence
|
2011-12-20 09:12:38 -05:00
|
|
|
parts = name.to_s.split('/')
|
2012-01-17 06:14:17 -05:00
|
|
|
parts.shift if parts.first.empty?
|
2011-12-08 09:23:23 -05:00
|
|
|
name = parts.pop
|
2010-09-17 16:39:14 -04:00
|
|
|
|
2011-12-08 09:23:23 -05:00
|
|
|
return name, prefixes || [""] if parts.empty?
|
|
|
|
|
|
|
|
parts = parts.join('/')
|
|
|
|
prefixes = prefixes ? prefixes.map { |p| "#{p}/#{parts}" } : [parts]
|
2010-12-27 03:14:13 -05:00
|
|
|
|
|
|
|
return name, prefixes
|
2010-03-15 21:08:34 -04:00
|
|
|
end
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
include Accessors
|
|
|
|
include DetailsCache
|
|
|
|
include ViewPaths
|
2010-03-08 14:39:15 -05:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
def initialize(view_paths, details = {}, prefixes = [])
|
2011-09-22 09:06:04 -04:00
|
|
|
@details, @details_key = {}, nil
|
2012-02-21 20:55:56 -05:00
|
|
|
@skip_default_locale = false
|
2011-09-21 18:28:05 -04:00
|
|
|
@cache = true
|
|
|
|
@prefixes = prefixes
|
2012-02-21 20:55:56 -05:00
|
|
|
@rendered_format = nil
|
2010-03-27 15:51:25 -04:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
self.view_paths = view_paths
|
|
|
|
initialize_details(details)
|
|
|
|
end
|
2010-03-08 14:39:15 -05:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Override formats= to expand ["*/*"] values and automatically
|
|
|
|
# add :html as fallback to :js.
|
|
|
|
def formats=(values)
|
|
|
|
if values
|
2011-09-22 09:03:05 -04:00
|
|
|
values.concat(default_formats) if values.delete "*/*"
|
2012-07-11 04:18:52 -04:00
|
|
|
if values == [:js]
|
|
|
|
values << :html
|
|
|
|
@html_fallback_for_js = true
|
|
|
|
end
|
2010-03-10 16:11:48 -05:00
|
|
|
end
|
2011-09-21 18:28:05 -04:00
|
|
|
super(values)
|
|
|
|
end
|
2010-03-10 16:11:48 -05:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Do not use the default locale on template lookup.
|
|
|
|
def skip_default_locale!
|
|
|
|
@skip_default_locale = true
|
|
|
|
self.locale = nil
|
|
|
|
end
|
2010-03-10 16:11:48 -05:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Override locale to return a symbol instead of array.
|
|
|
|
def locale
|
|
|
|
@details[:locale].first
|
|
|
|
end
|
2010-06-07 04:13:41 -04:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# Overload locale= to also set the I18n.locale. If the current I18n.config object responds
|
|
|
|
# to original_config, it means that it's has a copy of the original I18n configuration and it's
|
|
|
|
# acting as proxy, which we need to skip.
|
|
|
|
def locale=(value)
|
|
|
|
if value
|
|
|
|
config = I18n.config.respond_to?(:original_config) ? I18n.config.original_config : I18n.config
|
|
|
|
config.locale = value
|
2010-06-04 20:28:04 -04:00
|
|
|
end
|
|
|
|
|
2011-09-22 09:03:05 -04:00
|
|
|
super(@skip_default_locale ? I18n.locale : default_locale)
|
2011-09-21 18:28:05 -04:00
|
|
|
end
|
2010-03-19 12:20:15 -04:00
|
|
|
|
2011-09-21 18:28:05 -04:00
|
|
|
# A method which only uses the first format in the formats array for layout lookup.
|
|
|
|
def with_layout_format
|
|
|
|
if formats.size == 1
|
|
|
|
yield
|
|
|
|
else
|
|
|
|
old_formats = formats
|
|
|
|
_set_detail(:formats, formats[0,1])
|
2010-03-08 14:39:15 -05:00
|
|
|
|
2010-06-07 04:13:41 -04:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2011-09-21 18:28:05 -04:00
|
|
|
_set_detail(:formats, old_formats)
|
2010-03-08 14:39:15 -05:00
|
|
|
end
|
|
|
|
end
|
2010-03-08 09:19:03 -05:00
|
|
|
end
|
|
|
|
end
|
2010-09-17 16:39:14 -04:00
|
|
|
end
|