1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Added capability for overriding ActionView helpers that don't work

well with Haml.


git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@139 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-11-14 18:35:02 +00:00
parent f9048e8518
commit eb5e0b03c7
5 changed files with 84 additions and 3 deletions

View file

@ -1,3 +1,5 @@
require File.dirname(__FILE__) + '/helpers/action_view_mods'
module Haml
# This module contains various helpful methods to make it easier to do
# various tasks. Haml::Helpers is automatically included in the context
@ -5,6 +7,19 @@ module Haml
# disposal from within the template.
module Helpers
self.extend self
@@action_view = false
@@force_no_action_view = false
# Returns whether or not ActionView is available.
def self.action_view?
@@action_view
end
# Sets whether or not ActionView is available.
def self.action_view(value) # :nodoc:
@@action_view = value
end
# Takes any string, finds all the endlines and converts them to
# html entities for endlines so they'll render correctly in
@ -49,5 +64,7 @@ module Haml
def buffer # :nodoc:
@haml_stack[-1]
end
include ActionViewMods if self.const_defined? "ActionViewMods"
end
end

View file

@ -0,0 +1,32 @@
begin
require 'rubygems'
require 'active_support'
require 'action_view'
action_view_included = true
rescue LoadError
action_view_included = false
end
if action_view_included
class ActionView::Base
alias_method :old_concat, :concat unless instance_methods.include? "old_concat"
end
module Haml
module Helpers
# This module overrides various helpers in ActionView to make them
# work more effectively with Haml. It's not available unless ActionView
# is installed.
module ActionViewMods
def self.included(othermod)
othermod.action_view(true)
end
def concat(string, binding = nil)
buffer.buffer.concat(string)
end
end
end
end
end

View file

@ -35,17 +35,45 @@ class HelperTest < Test::Unit::TestCase
assert_equal(render("foo\n- tab_up\nbar\n- tab_down\nbaz"), "foo\n bar\nbaz\n")
end
def test_helper_leak
def test_helpers_dont_leak
# Haml helpers shouldn't be accessible from ERB
render("foo")
proper_behavior = false
begin
ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
rescue NoMethodError
proper_behavior = true
end
assert(proper_behavior)
begin
ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
rescue ArgumentError
proper_behavior = true
end
assert(proper_behavior)
end
def test_action_view_included
assert(Haml::Helpers.action_view?)
end
def test_action_view_not_included
#This is for 100% rcov, rather than any real testing purposes.
Kernel.module_eval do
alias_method :old_require, :require
def require(string)
raise LoadError if string == "action_view"
old_require string
end
end
load File.dirname(__FILE__) + '/../lib/haml/helpers/action_view_mods.rb'
Kernel.module_eval do
alias_method :require, :old_require
end
end
end

View file

@ -29,3 +29,5 @@
</p>
<p>baz</p>
<p>boom</p>
foo

View file

@ -15,3 +15,5 @@
%p baz
- buffer.tabulation = 10
%p boom
- concat "foo\n"