aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template/path.rb
diff options
context:
space:
mode:
authorYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-04-23 15:58:38 -0700
committerYehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>2009-04-27 11:49:11 -0700
commitcecafc52ee0a4a53c903ddbaba95683261f88e5f (patch)
tree4cc1f0d246a313b2a2fa78747b3265bdbd901c22 /actionpack/lib/action_view/template/path.rb
parentda3c21ead59cb47b8f4c69c6bd95f225a9c8b479 (diff)
downloadrails-cecafc52ee0a4a53c903ddbaba95683261f88e5f.tar.gz
rails-cecafc52ee0a4a53c903ddbaba95683261f88e5f.tar.bz2
rails-cecafc52ee0a4a53c903ddbaba95683261f88e5f.zip
Refactor ActionView::Template
ActionView::Template is now completely independent from template storage, which allows different back ends such as the database. ActionView::Template's only responsibility is to take in the template source (passed in from ActionView::Path), compile it, and render it.
Diffstat (limited to 'actionpack/lib/action_view/template/path.rb')
-rw-r--r--actionpack/lib/action_view/template/path.rb68
1 files changed, 45 insertions, 23 deletions
diff --git a/actionpack/lib/action_view/template/path.rb b/actionpack/lib/action_view/template/path.rb
index 660a7e91a2..478bf96c9a 100644
--- a/actionpack/lib/action_view/template/path.rb
+++ b/actionpack/lib/action_view/template/path.rb
@@ -1,3 +1,5 @@
+require "pathname"
+
module ActionView
class Template
# Abstract super class
@@ -26,13 +28,6 @@ module ActionView
def find_templates(name, details, prefix, partial)
raise NotImplementedError
end
-
- # TODO: Refactor this to abstract out the file system
- def initialize_template(file)
- t = Template.new(file.split("#{self}/").last, self)
- t.load!
- t
- end
def valid_handlers
@valid_handlers ||= TemplateHandlers.extensions
@@ -44,10 +39,10 @@ module ActionView
/\.(?:#{e})$/
end
end
-
+
def handler_glob
- e = TemplateHandlers.extensions.join(',')
- ".{#{e}}"
+ e = TemplateHandlers.extensions.map{|h| ".#{h},"}.join
+ "{#{e}}"
end
def formats_glob
@@ -69,23 +64,19 @@ module ActionView
def initialize(path, options = {})
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
super(options)
- @path = path
+ @path = Pathname.new(path).expand_path
end
-
+
# TODO: This is the currently needed API. Make this suck less
# ==== <suck>
attr_reader :path
def to_s
- if defined?(RAILS_ROOT)
- path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
- else
- path.to_s
- end
+ path.to_s
end
def to_str
- path.to_str
+ path.to_s
end
def ==(path)
@@ -97,11 +88,15 @@ module ActionView
end
# ==== </suck>
- def find_templates(name, details, prefix, partial)
- if glob = parts_to_glob(name, details, prefix, partial)
+ def find_templates(name, details, prefix, partial, root = "#{@path}/")
+ if glob = details_to_glob(name, details, prefix, partial, root)
cached(glob) do
Dir[glob].map do |path|
- initialize_template(path) unless File.directory?(path)
+ next if File.directory?(path)
+ source = File.read(path)
+ identifier = Pathname.new(path).expand_path.to_s
+
+ Template.new(source, identifier, *path_to_details(path))
end.compact
end
end
@@ -109,7 +104,8 @@ module ActionView
private
- def parts_to_glob(name, details, prefix, partial)
+ # :api: plugin
+ def details_to_glob(name, details, prefix, partial, root)
path = ""
path << "#{prefix}/" unless prefix.empty?
path << (partial ? "_#{name}" : name)
@@ -123,8 +119,34 @@ module ActionView
end
end
- "#{@path}/#{path}#{extensions}#{handler_glob}"
+ "#{root}#{path}#{extensions}#{handler_glob}"
+ end
+
+ # TODO: fix me
+ # :api: plugin
+ def path_to_details(path)
+ # [:erb, :format => :html, :locale => :en, :partial => true/false]
+ if m = path.match(%r'/(_)?[\w-]+(\.[\w-]+)*\.(\w+)$')
+ partial = m[1] == '_'
+ details = (m[2]||"").split('.').reject { |e| e.empty? }
+ handler = Template.handler_class_for_extension(m[3])
+
+ format = Mime[details.last] && details.pop.to_sym
+ locale = details.last && details.pop.to_sym
+
+ return handler, :format => format, :locale => locale, :partial => partial
+ end
end
end
+
+ class FileSystemPathWithFallback < FileSystemPath
+
+ def find_templates(name, details, prefix, partial)
+ templates = super
+ return super(name, details, prefix, partial, '') if templates.empty?
+ templates
+ end
+
+ end
end
end \ No newline at end of file