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:
parent
f9048e8518
commit
eb5e0b03c7
5 changed files with 84 additions and 3 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
require File.dirname(__FILE__) + '/helpers/action_view_mods'
|
||||||
|
|
||||||
module Haml
|
module Haml
|
||||||
# This module contains various helpful methods to make it easier to do
|
# This module contains various helpful methods to make it easier to do
|
||||||
# various tasks. Haml::Helpers is automatically included in the context
|
# various tasks. Haml::Helpers is automatically included in the context
|
||||||
|
@ -5,6 +7,19 @@ module Haml
|
||||||
# disposal from within the template.
|
# disposal from within the template.
|
||||||
module Helpers
|
module Helpers
|
||||||
self.extend self
|
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
|
# Takes any string, finds all the endlines and converts them to
|
||||||
# html entities for endlines so they'll render correctly in
|
# html entities for endlines so they'll render correctly in
|
||||||
|
@ -49,5 +64,7 @@ module Haml
|
||||||
def buffer # :nodoc:
|
def buffer # :nodoc:
|
||||||
@haml_stack[-1]
|
@haml_stack[-1]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
include ActionViewMods if self.const_defined? "ActionViewMods"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
32
lib/haml/helpers/action_view_mods.rb
Normal file
32
lib/haml/helpers/action_view_mods.rb
Normal 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
|
||||||
|
|
|
@ -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")
|
assert_equal(render("foo\n- tab_up\nbar\n- tab_down\nbaz"), "foo\n bar\nbaz\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_helper_leak
|
def test_helpers_dont_leak
|
||||||
# Haml helpers shouldn't be accessible from ERB
|
# Haml helpers shouldn't be accessible from ERB
|
||||||
render("foo")
|
render("foo")
|
||||||
proper_behavior = false
|
proper_behavior = false
|
||||||
|
|
||||||
begin
|
begin
|
||||||
ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
|
ActionView::Base.new.render(:inline => "<%= flatten('Foo\\nBar') %>")
|
||||||
rescue NoMethodError
|
rescue NoMethodError
|
||||||
proper_behavior = true
|
proper_behavior = true
|
||||||
end
|
end
|
||||||
|
assert(proper_behavior)
|
||||||
|
|
||||||
|
begin
|
||||||
|
ActionView::Base.new.render(:inline => "<%= concat('foo') %>")
|
||||||
|
rescue ArgumentError
|
||||||
|
proper_behavior = true
|
||||||
|
end
|
||||||
assert(proper_behavior)
|
assert(proper_behavior)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -29,3 +29,5 @@
|
||||||
</p>
|
</p>
|
||||||
<p>baz</p>
|
<p>baz</p>
|
||||||
<p>boom</p>
|
<p>boom</p>
|
||||||
|
foo
|
||||||
|
|
||||||
|
|
|
@ -15,3 +15,5 @@
|
||||||
%p baz
|
%p baz
|
||||||
- buffer.tabulation = 10
|
- buffer.tabulation = 10
|
||||||
%p boom
|
%p boom
|
||||||
|
- concat "foo\n"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue