[Haml] Abstract out references to ActionView::Template*.

Closes gh-67
This commit is contained in:
Nathan Weizenbaum 2009-12-10 12:29:06 -08:00
parent 8d49318ca2
commit 425d75370d
6 changed files with 30 additions and 6 deletions

View File

@ -3,6 +3,13 @@
* Table of contents
{:toc}
## 2.2.16 (Unreleased)
* Abstract out references to `ActionView::TemplateError`,
`ActionView::TemplateHandler`, etc.
These have all been renamed to `ActionView::Template::*`
in Rails 3.0.
## [2.2.15](http://github.com/nex3/haml/commit/2.2.15)
* Allow `if` statements with no content followed by `else` clauses.

View File

@ -46,7 +46,7 @@ end
# Decide how we want to load Haml into Rails.
# Patching was necessary for versions <= 2.0.1,
# but we can make it a normal handler for higher versions.
if defined?(ActionView::TemplateHandler)
if defined?(ActionView::TemplateHandler) || defined?(ActionView::Template::Handler)
require 'haml/template/plugin'
else
require 'haml/template/patch'

View File

@ -2,8 +2,11 @@
# using the > 2.0.1 template handler API.
module Haml
class Plugin < ActionView::TemplateHandler
include ActionView::TemplateHandlers::Compilable if defined?(ActionView::TemplateHandlers::Compilable)
class Plugin < Haml::Util.av_template_class(:Handler)
if defined?(ActionView::TemplateHandlers::Compilable) ||
defined?(ActionView::Template::Handlers::Compilable)
include Haml::Util.av_template_class(:Handlers)::Compilable
end
def compile(template)
options = Haml::Template.options.dup
@ -38,7 +41,8 @@ end.register_template_handler(:haml, Haml::Plugin)
# In Rails 2.0.2, ActionView::TemplateError took arguments
# that we can't fill in from the Haml::Plugin context.
# Thus, we've got to monkeypatch ActionView::Base to catch the error.
if ActionView::TemplateError.instance_method(:initialize).arity == 5
if defined?(ActionView::TemplateError) &&
ActionView::TemplateError.instance_method(:initialize).arity == 5
class ActionView::Base
def compile_template(handler, template, file_name, local_assigns)
render_symbol = assign_method_name(handler, template, file_name)

View File

@ -146,6 +146,19 @@ module Haml
return nil
end
# Returns an ActionView::Template* class.
# In pre-3.0 versions of Rails, most of these classes
# were of the form `ActionView::TemplateFoo`,
# while afterwards they were of the form `ActionView;:Template::Foo`.
#
# @param name [#to_s] The name of the class to get.
# For example, `:Error` will return `ActionView::TemplateError`
# or `ActionView::Template::Error`.
def av_template_class(name)
return ActionView.const_get("Template#{name}") if ActionView.const_defined?("Template#{name}")
return ActionView::Template.const_get(name.to_s)
end
## Rails XSS Safety
# Whether or not ActionView's XSS protection is available and enabled,

View File

@ -73,7 +73,7 @@ class HelperTest < Test::Unit::TestCase
begin
ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
rescue NoMethodError, ActionView::TemplateError
rescue NoMethodError, Haml::Util.av_template_class(:Error)
proper_behavior = true
end
assert(proper_behavior)

View File

@ -109,7 +109,7 @@ class TemplateTest < Test::Unit::TestCase
message = "template: #{name}\nline: #{line}"
assert_equal(pair.first, pair.last, message)
end
rescue ActionView::TemplateError => e
rescue Haml::Util.av_template_class(:Error) => e
if e.message =~ /Can't run [\w:]+ filter; required (one of|file) ((?:'\w+'(?: or )?)+)(, but none were found| not found)/
puts "\nCouldn't require #{$2}; skipping a test."
else