1 module ActionView
#:nodoc:
4 attr_reader
:path, :paths
5 delegate
:hash, :inspect, :to => :path
8 raise ArgumentError
, "path already is a Path class" if path
.is_a
?(Path
)
9 @path = (path
.ends_with
?(File
::SEPARATOR) ? path
.to(-2) : path
).freeze
13 if defined?(RAILS_ROOT
)
14 path
.to_s
.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
32 # Returns a ActionView::Template object for the given path string. The
33 # input path should be relative to the view path directory,
34 # +hello/index.html.erb+. This method also has a special exception to
35 # match partial file names without a handler extension. So
36 # +hello/index.html+ will match the first template it finds with a
37 # known template extension, +hello/index.html.erb+. Template extensions
38 # should not be confused with format extensions +html+, +js+, +xml+,
39 # etc. A format must be supplied to match a formated file. +hello/index+
40 # will never match +hello/index.html.erb+.
47 def self.new_and_loaded(path
)
48 returning
new(path
) do |path
|
54 def relative_path_for_template_file(full_file_path
)
55 full_file_path
.split("#{@path}/").last
59 class EagerPath
< Path
64 templates_in_path
do |template
|
66 template
.accessible_paths
.each
do |path
|
67 @paths[path
] = template
81 (Dir
.glob("#{@path}/**/*/**") | Dir
.glob("#{@path}/**")).each
do |file
|
82 yield create_template(file
) unless File
.directory
?(file
)
86 def create_template(file
)
87 Template
.new(relative_path_for_template_file(file
), self)
91 extend TemplateHandlers
92 extend ActiveSupport
::Memoizable
95 # Templates that are exempt from layouts
96 @
@exempt_from_layout = Set
.new([/\.rjs$/])
98 # Don't render layouts for templates with the given extensions.
99 def self.exempt_from_layout(*extensions
)
100 regexps
= extensions
.collect
do |extension
|
101 extension
.is_a
?(Regexp
) ? extension
: /\.#{Regexp.escape(extension.to_s)}$/
103 @
@exempt_from_layout.merge(regexps
)
106 attr_accessor
:template_path, :filename, :load_path, :base_path
107 attr_accessor
:locale, :name, :format, :extension
108 delegate
:to_s, :to => :path
110 def initialize(template_path
, load_path
)
111 @template_path = template_path
.dup
112 @load_path, @filename = load_path
, File
.join(load_path
, template_path
)
113 @base_path, @name, @locale, @format, @extension = split(template_path
)
114 @base_path.to_s
.gsub
!(/\/$/, '') # Push to split method
116 # Extend with partial super powers
117 extend RenderablePartial
if @name =~
/^_/
123 if valid_extension
?(extension
)
125 paths
<< path_without_extension
127 formats
= format
.split(".")
128 paths
<< "#{path_without_format_and_extension}.#{formats.first}"
129 paths
<< "#{path_without_format_and_extension}.#{formats.second}"
132 # template without explicit template handler should only be reachable through its exact path
133 paths
<< template_path
139 def format_and_extension
140 (extensions
= [format
, extension
].compact
.join(".")).blank
? ? nil : extensions
142 memoize
:format_and_extension
145 format
&& format
.include?('.')
149 format
.gsub('.', '/')
153 Mime
::Type.lookup_by_extension(format
) if format
&& defined?(::Mime)
158 [base_path
, [name
, locale
, format
, extension
].compact
.join('.')].compact
.join('/')
162 def path_without_extension
163 [base_path
, [name
, locale
, format
].compact
.join('.')].compact
.join('/')
165 memoize
:path_without_extension
167 def path_without_format_and_extension
168 [base_path
, [name
, locale
].compact
.join('.')].compact
.join('/')
170 memoize
:path_without_format_and_extension
173 path
= File
.expand_path(filename
)
174 path
.sub
!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT
)
177 memoize
:relative_path
179 def exempt_from_layout
?
180 @
@exempt_from_layout.any
? { |exempted
| path
=~ exempted
}
189 relative_path
.to_s
.gsub(/([^a-zA-Z0-9_])/) { $1.ord
}
191 memoize
:method_segment
193 def render_template(view
, local_assigns
= {})
194 render(view
, local_assigns
)
195 rescue Exception
=> e
196 raise e
unless filename
197 if TemplateError
=== e
198 e
.sub_template_of(self)
201 raise TemplateError
.new(self, view
.assigns
, e
)
210 def valid_extension
?(extension
)
211 !Template
.registered_template_handler(extension
).nil?
214 def valid_locale
?(locale
)
215 I18n
.available_locales
.include?(locale
.to_sym
)
218 # Returns file split into an array
219 # [base_path, name, locale, format, extension]
221 if m
= file
.to_s
.match(/^(.*\/)?([^\
.]+)\
.(.*)$/)
233 if m
= extensions
.split(".")
234 if valid_locale
?(m
[0]) && m
[1] && valid_extension
?(m
[2]) # All three
238 elsif m
[0] && m
[1] && valid_extension
?(m
[2]) # Multipart formats
239 format
= "#{m[0]}.#{m[1]}"
241 elsif valid_locale
?(m
[0]) && valid_extension
?(m
[1]) # locale and extension
244 elsif valid_extension
?(m
[1]) # format and extension
247 elsif valid_extension
?(m
[0]) # Just extension
254 [base_path
, name
, locale
, format
, extension
]