2008-07-12 15:33:46 -04:00
|
|
|
module ActionView #:nodoc:
|
2008-08-21 01:28:25 -04:00
|
|
|
class PathSet < Array #:nodoc:
|
2008-07-12 15:33:46 -04:00
|
|
|
def self.type_cast(obj)
|
2008-07-12 16:31:50 -04:00
|
|
|
if obj.is_a?(String)
|
|
|
|
Path.new(obj)
|
|
|
|
else
|
|
|
|
obj
|
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
|
2008-08-21 01:28:25 -04:00
|
|
|
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
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
class Path #:nodoc:
|
|
|
|
attr_reader :path, :paths
|
2008-12-05 12:24:28 -05:00
|
|
|
delegate :hash, :inspect, :to => :path
|
2008-07-12 15:33:46 -04:00
|
|
|
|
2008-11-28 12:18:28 -05:00
|
|
|
def initialize(path, load = false)
|
2008-07-19 16:27:55 -04:00
|
|
|
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
|
2008-07-12 15:33:46 -04:00
|
|
|
@path = path.freeze
|
2008-07-23 11:41:28 -04:00
|
|
|
reload! if load
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
|
2008-12-05 12:24:28 -05:00
|
|
|
def to_s
|
|
|
|
if defined?(RAILS_ROOT)
|
|
|
|
path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
|
|
|
|
else
|
|
|
|
path.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-11 11:17:29 -05:00
|
|
|
def to_str
|
|
|
|
path.to_str
|
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def ==(path)
|
|
|
|
to_str == path.to_str
|
|
|
|
end
|
|
|
|
|
2008-07-22 12:12:16 -04:00
|
|
|
def eql?(path)
|
|
|
|
to_str == path.to_str
|
|
|
|
end
|
|
|
|
|
2008-11-28 12:18:28 -05:00
|
|
|
# Returns a ActionView::Template object for the given path string. The
|
|
|
|
# input path should be relative to the view path directory,
|
|
|
|
# +hello/index.html.erb+. This method also has a special exception to
|
|
|
|
# match partial file names without a handler extension. So
|
|
|
|
# +hello/index.html+ will match the first template it finds with a
|
|
|
|
# known template extension, +hello/index.html.erb+. Template extensions
|
|
|
|
# should not be confused with format extensions +html+, +js+, +xml+,
|
|
|
|
# etc. A format must be supplied to match a formated file. +hello/index+
|
|
|
|
# will never match +hello/index.html.erb+.
|
|
|
|
#
|
|
|
|
# This method also has two different implementations, one that is "lazy"
|
|
|
|
# and makes file system calls every time and the other is cached,
|
|
|
|
# "eager" which looks up the template in an in memory index. The "lazy"
|
|
|
|
# version is designed for development where you want to automatically
|
|
|
|
# find new templates between requests. The "eager" version is designed
|
|
|
|
# for production mode and it is much faster but requires more time
|
|
|
|
# upfront to build the file index.
|
2008-07-12 15:33:46 -04:00
|
|
|
def [](path)
|
2008-11-28 12:18:28 -05:00
|
|
|
if loaded?
|
|
|
|
@paths[path]
|
|
|
|
else
|
|
|
|
Dir.glob("#{@path}/#{path}*").each do |file|
|
|
|
|
template = create_template(file)
|
2008-12-21 18:23:53 -05:00
|
|
|
if template.accessible_paths.include?(path)
|
2008-11-28 12:18:28 -05:00
|
|
|
return template
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
|
2008-07-23 11:41:28 -04:00
|
|
|
def loaded?
|
|
|
|
@loaded ? true : false
|
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
|
|
|
reload! unless loaded?
|
2008-07-31 20:51:43 -04:00
|
|
|
self
|
2008-07-23 11:41:28 -04:00
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
# Rebuild load path directory cache
|
|
|
|
def reload!
|
|
|
|
@paths = {}
|
|
|
|
|
|
|
|
templates_in_path do |template|
|
2008-11-28 15:31:54 -05:00
|
|
|
template.load!
|
2008-12-21 18:23:53 -05:00
|
|
|
template.accessible_paths.each do |path|
|
|
|
|
@paths[path] = template
|
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@paths.freeze
|
2008-07-23 11:41:28 -04:00
|
|
|
@loaded = true
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def templates_in_path
|
|
|
|
(Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
|
2008-11-28 12:18:28 -05:00
|
|
|
yield create_template(file) unless File.directory?(file)
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
end
|
2008-11-28 12:18:28 -05:00
|
|
|
|
|
|
|
def create_template(file)
|
|
|
|
Template.new(file.split("#{self}/").last, self)
|
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
|
2008-07-23 11:41:28 -04:00
|
|
|
def load
|
|
|
|
each { |path| path.load }
|
|
|
|
end
|
|
|
|
|
2008-07-12 15:33:46 -04:00
|
|
|
def reload!
|
|
|
|
each { |path| path.reload! }
|
|
|
|
end
|
|
|
|
|
2008-12-21 18:23:53 -05:00
|
|
|
def find_template(original_template_path, format = nil)
|
|
|
|
return original_template_path if original_template_path.respond_to?(:render)
|
|
|
|
template_path = original_template_path.sub(/^\//, '')
|
2008-11-28 12:18:28 -05:00
|
|
|
|
2008-12-21 18:23:53 -05:00
|
|
|
each do |load_path|
|
|
|
|
if format && (template = load_path["#{template_path}.#{format}"])
|
|
|
|
return template
|
|
|
|
elsif template = load_path[template_path]
|
2008-11-28 12:18:28 -05:00
|
|
|
return template
|
|
|
|
end
|
|
|
|
end
|
2008-12-21 18:23:53 -05:00
|
|
|
|
|
|
|
Template.new(original_template_path, self)
|
2008-11-28 12:18:28 -05:00
|
|
|
end
|
2008-07-12 15:33:46 -04:00
|
|
|
end
|
|
|
|
end
|