diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index dcc7e510..93f06e3d 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -65,6 +65,14 @@ module Haml @haml_stack[-1] end + # Gives a proc the same local "_hamlout" and "_erbout" variables + # that the current template has. + def bind_proc(&proc) + _hamlout = buffer + _erbout = _hamlout.buffer + proc { |*args| proc.call(*args) } + end + include ActionViewMods if self.const_defined? "ActionViewMods" end end diff --git a/lib/haml/helpers/action_view_mods.rb b/lib/haml/helpers/action_view_mods.rb index 0a727b0d..970080ba 100644 --- a/lib/haml/helpers/action_view_mods.rb +++ b/lib/haml/helpers/action_view_mods.rb @@ -10,6 +10,7 @@ end if action_view_included class ActionView::Base alias_method :old_concat, :concat unless instance_methods.include? "old_concat" + alias_method :old_form_tag, :form_tag unless instance_methods.include? "old_form_tag" end module Haml @@ -25,6 +26,19 @@ if action_view_included def concat(string, binding = nil) buffer.buffer.concat(string) end + + def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc) + if block_given? + oldproc = proc + proc = bind_proc do |*args| + concat "\n" + tab_up + oldproc.call(*args) + tab_down + end + end + old_form_tag(url_for_options, options, *parameters_for_url, &proc) + end end end end diff --git a/test/helper_test.rb b/test/helper_test.rb index a754184f..630b019b 100644 --- a/test/helper_test.rb +++ b/test/helper_test.rb @@ -5,9 +5,19 @@ require File.dirname(__FILE__) + '/../lib/haml/helpers' class HelperTest < Test::Unit::TestCase include Haml::Helpers + + def setup + ActionView::Base.register_template_handler("haml", Haml::Template) + @base = ActionView::Base.new + @base.controller = ActionController::Base.new + end def render(text, options = {}) - Haml::Engine.new(text, options).to_html + if options == :action_view + @base.render :inline => text, :type => :haml + else + Haml::Engine.new(text, options).to_html + end end def test_flatten @@ -75,5 +85,15 @@ class HelperTest < Test::Unit::TestCase alias_method :require, :old_require end end + + def test_form_tag + # Until the next Rails is released, form_tag with a block can have one of + # two behaviors. + + result = render("- form_tag 'foo' do\n %p bar\n %strong baz", :action_view) + new_rails = "
\n" + old_rails = "" + assert(result == new_rails || result == old_rails) + end end