aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template
diff options
context:
space:
mode:
authorMikel Lindsaar <raasdnil@gmail.com>2010-03-28 14:44:34 +1100
committerMikel Lindsaar <raasdnil@gmail.com>2010-03-28 14:44:34 +1100
commit2bcc2ebf44b59e46c104c92d621e8051c97bfcf5 (patch)
treed2a3f04fd3020c1b5d88847af62d52f2d5e5bd61 /actionpack/lib/action_view/template
parentf5774e3e3f70a3acfa559b9ff889e9417fb71d4b (diff)
parent8398f21880a952769ccd6437a4344922fe596dab (diff)
downloadrails-2bcc2ebf44b59e46c104c92d621e8051c97bfcf5.tar.gz
rails-2bcc2ebf44b59e46c104c92d621e8051c97bfcf5.tar.bz2
rails-2bcc2ebf44b59e46c104c92d621e8051c97bfcf5.zip
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'actionpack/lib/action_view/template')
-rw-r--r--actionpack/lib/action_view/template/error.rb24
-rw-r--r--actionpack/lib/action_view/template/handlers/erb.rb62
-rw-r--r--actionpack/lib/action_view/template/handlers/rjs.rb3
-rw-r--r--actionpack/lib/action_view/template/resolver.rb45
-rw-r--r--actionpack/lib/action_view/template/text.rb14
5 files changed, 64 insertions, 84 deletions
diff --git a/actionpack/lib/action_view/template/error.rb b/actionpack/lib/action_view/template/error.rb
index 648f708d3d..5222ffa89c 100644
--- a/actionpack/lib/action_view/template/error.rb
+++ b/actionpack/lib/action_view/template/error.rb
@@ -1,6 +1,26 @@
require "active_support/core_ext/enumerable"
module ActionView
+ class ActionViewError < StandardError #:nodoc:
+ end
+
+ class MissingTemplate < ActionViewError #:nodoc:
+ attr_reader :path
+
+ def initialize(paths, path, details, partial)
+ @path = path
+ display_paths = paths.compact.map{ |p| p.to_s.inspect }.join(", ")
+ template_type = if partial
+ "partial"
+ elsif path =~ /layouts/i
+ 'layout'
+ else
+ 'template'
+ end
+
+ super("Missing #{template_type} #{path} with #{details.inspect} in view paths #{display_paths}")
+ end
+ end
class Template
# The Template::Error exception is raised when the compilation of the template fails. This exception then gathers a
# bunch of intimate details and uses it to report a very precise exception message.
@@ -73,11 +93,11 @@ module ActionView
end
def to_s
- "\n#{self.class} (#{message}) #{source_location}:\n" +
+ "\n#{self.class} (#{message}) #{source_location}:\n" +
"#{source_extract}\n #{clean_backtrace.join("\n ")}\n\n"
end
- # don't do anything nontrivial here. Any raised exception from here becomes fatal
+ # don't do anything nontrivial here. Any raised exception from here becomes fatal
# (and can't be rescued).
def backtrace
@backtrace
diff --git a/actionpack/lib/action_view/template/handlers/erb.rb b/actionpack/lib/action_view/template/handlers/erb.rb
index 937694ce8e..705c2bf82e 100644
--- a/actionpack/lib/action_view/template/handlers/erb.rb
+++ b/actionpack/lib/action_view/template/handlers/erb.rb
@@ -3,46 +3,16 @@ require 'active_support/core_ext/string/output_safety'
require 'erubis'
module ActionView
- class OutputBuffer
- def initialize
- @buffer = ActiveSupport::SafeBuffer.new
- end
-
- def safe_concat(value)
- @buffer.safe_concat(value)
- end
-
+ class OutputBuffer < ActiveSupport::SafeBuffer
def <<(value)
- @buffer << value.to_s
- end
-
- def length
- @buffer.length
+ super(value.to_s)
end
+ alias :append= :<<
- def [](*args)
- @buffer[*args]
- end
-
- def to_s
- @buffer.to_s
- end
-
- def to_str
- @buffer.to_str
- end
-
- def empty?
- @buffer.empty?
- end
-
- def html_safe?
- @buffer.html_safe?
- end
-
- if "".respond_to?(:force_encoding)
- def force_encoding(encoding)
- @buffer.force_encoding(encoding)
+ def append_if_string=(value)
+ if value.is_a?(String) && !value.is_a?(NonConcattingString)
+ ActiveSupport::Deprecation.warn("<% %> style block helpers are deprecated. Please use <%= %>", caller)
+ self << value
end
end
end
@@ -58,16 +28,26 @@ module ActionView
src << "@output_buffer.safe_concat('" << escape_text(text) << "');"
end
+ BLOCK_EXPR = /(do|\{)(\s*\|[^|]*\|)?\s*\Z/
+
def add_expr_literal(src, code)
- if code =~ /(do|\{)(\s*\|[^|]*\|)?\s*\Z/
- src << '@output_buffer << ' << code
+ if code =~ BLOCK_EXPR
+ src << '@output_buffer.append= ' << code
+ else
+ src << '@output_buffer.append= (' << code << ');'
+ end
+ end
+
+ def add_stmt(src, code)
+ if code =~ BLOCK_EXPR
+ src << '@output_buffer.append_if_string= ' << code
else
- src << '@output_buffer << (' << code << ');'
+ super
end
end
def add_expr_escaped(src, code)
- src << '@output_buffer << ' << escaped_expr(code) << ';'
+ src << '@output_buffer.append= ' << escaped_expr(code) << ';'
end
def add_postamble(src)
diff --git a/actionpack/lib/action_view/template/handlers/rjs.rb b/actionpack/lib/action_view/template/handlers/rjs.rb
index 63e7dc0902..128be5077c 100644
--- a/actionpack/lib/action_view/template/handlers/rjs.rb
+++ b/actionpack/lib/action_view/template/handlers/rjs.rb
@@ -6,8 +6,7 @@ module ActionView
self.default_format = Mime::JS
def compile(template)
- "controller.response.content_type ||= Mime::JS;" +
- "update_page do |page|;#{template.source}\nend"
+ "update_page do |page|;#{template.source}\nend"
end
def default_format
diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb
index a43597e728..8e8afaa43f 100644
--- a/actionpack/lib/action_view/template/resolver.rb
+++ b/actionpack/lib/action_view/template/resolver.rb
@@ -1,6 +1,5 @@
require "pathname"
require "active_support/core_ext/class"
-require "active_support/core_ext/array/wrap"
require "action_view/template"
module ActionView
@@ -14,15 +13,8 @@ module ActionView
@cached.clear
end
- def find(*args)
- find_all(*args).first
- end
-
# Normalizes the arguments and passes it on to find_template.
def find_all(name, prefix=nil, partial=false, details={}, key=nil)
- name, prefix = normalize_name(name, prefix)
- details = details.merge(:handlers => default_handlers)
-
cached(key, prefix, name, partial) do
find_templates(name, prefix, partial, details)
end
@@ -34,10 +26,6 @@ module ActionView
@caching ||= !defined?(Rails.application) || Rails.application.config.cache_classes
end
- def default_handlers
- Template::Handlers.extensions + [nil]
- end
-
# This is what child classes implement. No defaults are needed
# because Resolver guarantees that the arguments are present and
# normalized.
@@ -45,17 +33,6 @@ module ActionView
raise NotImplementedError
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.
- def normalize_name(name, prefix)
- handlers = Template::Handlers.extensions.join('|')
- name = name.to_s.gsub(/\.(?:#{handlers})$/, '')
-
- parts = name.split('/')
- return parts.pop, [prefix, *parts].compact.join("/")
- end
-
def cached(key, prefix, name, partial)
return yield unless key && caching?
scope = @cached[key][prefix][name]
@@ -79,7 +56,7 @@ module ActionView
def find_templates(name, prefix, partial, details)
path = build_path(name, prefix, partial, details)
- query(partial, path, EXTENSION_ORDER.map { |ext| details[ext] })
+ query(path, EXTENSION_ORDER.map { |ext| details[ext] }, details[:formats])
end
def build_path(name, prefix, partial, details)
@@ -89,26 +66,32 @@ module ActionView
path
end
- def query(partial, path, exts)
+ def query(path, exts, formats)
query = File.join(@path, path)
exts.each do |ext|
- query << '{' << ext.map {|e| e && ".#{e}" }.join(',') << '}'
+ query << '{' << ext.map {|e| e && ".#{e}" }.join(',') << ',}'
end
Dir[query].reject { |p| File.directory?(p) }.map do |p|
- handler, format = extract_handler_and_format(p)
+ handler, format = extract_handler_and_format(p, formats)
Template.new(File.read(p), File.expand_path(p), handler,
- :partial => partial, :virtual_path => path, :format => format)
+ :virtual_path => path, :format => format)
end
end
- def extract_handler_and_format(path)
+ # Extract handler and formats from path. If a format cannot be a found neither
+ # from the path, or the handler, we should return the array of formats given
+ # to the resolver.
+ def extract_handler_and_format(path, default_formats)
pieces = File.basename(path).split(".")
pieces.shift
- handler = Template.handler_class_for_extension(pieces.pop)
- format = pieces.last && Mime[pieces.last] && pieces.pop.to_sym
+ handler = Template.handler_class_for_extension(pieces.pop)
+ format = pieces.last && Mime[pieces.last] && pieces.pop.to_sym
+ format ||= handler.default_format if handler.respond_to?(:default_format)
+ format ||= default_formats
+
[handler, format]
end
end
diff --git a/actionpack/lib/action_view/template/text.rb b/actionpack/lib/action_view/template/text.rb
index df394b0fb0..269340514c 100644
--- a/actionpack/lib/action_view/template/text.rb
+++ b/actionpack/lib/action_view/template/text.rb
@@ -1,10 +1,12 @@
module ActionView #:nodoc:
class Template
class Text < String #:nodoc:
- def initialize(string, content_type = nil)
+ attr_accessor :mime_type
+
+ def initialize(string, mime_type = nil)
super(string.to_s)
- @content_type = Mime[content_type] || content_type if content_type
- @content_type ||= Mime::TEXT
+ @mime_type = Mime[mime_type] || mime_type if mime_type
+ @mime_type ||= Mime::TEXT
end
def identifier
@@ -19,12 +21,8 @@ module ActionView #:nodoc:
to_s
end
- def mime_type
- @content_type
- end
-
def formats
- [@content_type.to_sym]
+ [@mime_type.to_sym]
end
def partial?