First take on ViewPaths clean up.
This commit is contained in:
parent
a424f199a9
commit
6e0443fd43
@ -1,46 +1,17 @@
|
|||||||
module ActionView #:nodoc:
|
module ActionView #:nodoc:
|
||||||
class PathSet < Array #:nodoc:
|
class PathSet < Array #:nodoc:
|
||||||
def self.type_cast(obj, cache = nil)
|
%w(initialize << concat insert push unshift).each do |method|
|
||||||
# TODO: Clean this up
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||||
if obj.is_a?(String)
|
def #{method}(*args)
|
||||||
if cache.nil?
|
super
|
||||||
cache = !defined?(Rails.application) || Rails.application.config.cache_classes
|
typecast!
|
||||||
end
|
end
|
||||||
FileSystemResolverWithFallback.new(obj, :cache => cache)
|
METHOD
|
||||||
else
|
|
||||||
obj
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(*args)
|
|
||||||
super(*args).map! { |obj| self.class.type_cast(obj) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def <<(obj)
|
|
||||||
super(self.class.type_cast(obj))
|
|
||||||
end
|
|
||||||
|
|
||||||
def concat(array)
|
|
||||||
super(array.map! { |obj| self.class.type_cast(obj) })
|
|
||||||
end
|
|
||||||
|
|
||||||
def insert(index, obj)
|
|
||||||
super(index, self.class.type_cast(obj))
|
|
||||||
end
|
|
||||||
|
|
||||||
def push(*objs)
|
|
||||||
super(*objs.map { |obj| self.class.type_cast(obj) })
|
|
||||||
end
|
|
||||||
|
|
||||||
def unshift(*objs)
|
|
||||||
super(*objs.map { |obj| self.class.type_cast(obj) })
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(path, details = {}, prefix = nil, partial = false)
|
def find(path, details = {}, prefix = nil, partial = false)
|
||||||
template_path = path
|
each do |resolver|
|
||||||
|
if template = resolver.find(path, details, prefix, partial)
|
||||||
each do |load_path|
|
|
||||||
if template = load_path.find(template_path, details, prefix, partial)
|
|
||||||
return template
|
return template
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -48,33 +19,22 @@ def find(path, details = {}, prefix = nil, partial = false)
|
|||||||
raise ActionView::MissingTemplate.new(self, "#{prefix}/#{path}", details, partial)
|
raise ActionView::MissingTemplate.new(self, "#{prefix}/#{path}", details, partial)
|
||||||
end
|
end
|
||||||
|
|
||||||
def exists?(path, extension = nil, prefix = nil, partial = false)
|
def exists?(path, details = {}, prefix = nil, partial = false)
|
||||||
template_path = path.sub(/^\//, '')
|
each do |resolver|
|
||||||
|
if resolver.find(path, details, prefix, partial)
|
||||||
each do |load_path|
|
return true
|
||||||
return true if template = load_path.find(template_path, extension, prefix, partial)
|
end
|
||||||
end
|
end
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_template(original_template_path, format = nil, html_fallback = true)
|
protected
|
||||||
return original_template_path if original_template_path.respond_to?(:render)
|
|
||||||
template_path = original_template_path.sub(/^\//, '')
|
|
||||||
|
|
||||||
each do |load_path|
|
def typecast!
|
||||||
if template = load_path.find(template_path, format)
|
each_with_index do |path, i|
|
||||||
return template
|
next unless path.is_a?(String)
|
||||||
# Try to find html version if the format is javascript
|
self[i] = FileSystemResolverWithFallback.new(path)
|
||||||
elsif format == :js && html_fallback && template = load_path["#{template_path}.#{I18n.locale}.html"]
|
|
||||||
return template
|
|
||||||
elsif format == :js && html_fallback && template = load_path["#{template_path}.html"]
|
|
||||||
return template
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return Template.new(original_template_path, original_template_path.to_s =~ /\A\// ? "" : ".") if File.file?(original_template_path)
|
|
||||||
|
|
||||||
raise MissingTemplate.new(self, original_template_path, format)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -22,8 +22,7 @@ def self.register_detail(name, options = {})
|
|||||||
register_detail(:formats) { Mime::SET.symbols }
|
register_detail(:formats) { Mime::SET.symbols }
|
||||||
register_detail(:handlers) { Template::Handlers.extensions }
|
register_detail(:handlers) { Template::Handlers.extensions }
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize
|
||||||
@cache = options[:cache]
|
|
||||||
@cached = {}
|
@cached = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -43,6 +42,10 @@ def find_all(name, details = {}, prefix = nil, partial = nil)
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def caching?
|
||||||
|
@caching ||= !defined?(Rails.application) || Rails.application.config.cache_classes
|
||||||
|
end
|
||||||
|
|
||||||
# This is what child classes implement. No defaults are needed
|
# This is what child classes implement. No defaults are needed
|
||||||
# because Resolver guarantees that the arguments are present and
|
# because Resolver guarantees that the arguments are present and
|
||||||
# normalized.
|
# normalized.
|
||||||
@ -72,7 +75,7 @@ def normalize_name(name, prefix)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cached(key)
|
def cached(key)
|
||||||
return yield unless @cache
|
return yield unless caching?
|
||||||
return @cached[key] if @cached.key?(key)
|
return @cached[key] if @cached.key?(key)
|
||||||
@cached[key] = yield
|
@cached[key] = yield
|
||||||
end
|
end
|
||||||
@ -133,18 +136,18 @@ def path_to_details(path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
class FileSystemResolver < PathResolver
|
class FileSystemResolver < PathResolver
|
||||||
def initialize(path, options = {})
|
def initialize(path)
|
||||||
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
|
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
|
||||||
super(options)
|
super()
|
||||||
@path = Pathname.new(path).expand_path
|
@path = Pathname.new(path).expand_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: remove hack
|
# TODO: remove hack
|
||||||
class FileSystemResolverWithFallback < Resolver
|
class FileSystemResolverWithFallback < Resolver
|
||||||
def initialize(path, options = {})
|
def initialize(path)
|
||||||
super(options)
|
super()
|
||||||
@paths = [FileSystemResolver.new(path, options), FileSystemResolver.new("", options), FileSystemResolver.new("/", options)]
|
@paths = [FileSystemResolver.new(path), FileSystemResolver.new(""), FileSystemResolver.new("/")]
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_templates(*args)
|
def find_templates(*args)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
module ActionView #:nodoc:
|
module ActionView #:nodoc:
|
||||||
class FixtureResolver < PathResolver
|
class FixtureResolver < PathResolver
|
||||||
def initialize(hash = {}, options = {})
|
def initialize(hash = {})
|
||||||
super(options)
|
super()
|
||||||
@hash = hash
|
@hash = hash
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user