From 9b5d11e80b6d4b37ee63c223fc06ee66fce32973 Mon Sep 17 00:00:00 2001 From: nex3 Date: Tue, 15 May 2007 15:51:50 +0000 Subject: [PATCH] Fixed bug with variables being passed to captured blocks being forced into arrays. Thanks, Robin Ward! git-svn-id: svn://hamptoncatlin.com/haml/trunk@516 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/helpers.rb | 6 +++--- test/haml/helper_test.rb | 14 +++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 40c21c81..3ba394ff 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -70,7 +70,7 @@ module Haml # def list_of(array, &block) # :yields: item to_return = array.collect do |i| - result = capture_haml(*i, &block) + result = capture_haml(i, &block) if result.count("\n") > 1 result.gsub!("\n", "\n ") @@ -286,10 +286,10 @@ module Haml # Performs the function of capture_haml, assuming local_buffer # is where the output of block goes. - def capture_haml_with_buffer(local_buffer, *args) + def capture_haml_with_buffer(local_buffer, *args, &block) position = local_buffer.length - yield args + block.call *args captured = local_buffer.slice!(position..-1) diff --git a/test/haml/helper_test.rb b/test/haml/helper_test.rb index eeab33b0..735b4872 100644 --- a/test/haml/helper_test.rb +++ b/test/haml/helper_test.rb @@ -35,7 +35,7 @@ class HelperTest < Test::Unit::TestCase def test_list_of_should_render_correctly assert_equal("
  • 1
  • \n
  • 2
  • \n", render("= list_of([1, 2]) do |i|\n = i")) - assert_equal("
  • 1
  • \n", render("= list_of([[1]]) do |i|\n = i.first")) + assert_equal("
  • [1]
  • \n", render("= list_of([[1]]) do |i|\n = i.inspect")) assert_equal("
  • \n

    Fee

    \n

    A word!

    \n
  • \n
  • \n

    Fi

    \n

    A word!

    \n
  • \n
  • \n

    Fo

    \n

    A word!

    \n
  • \n
  • \n

    Fum

    \n

    A word!

    \n
  • \n", render("= list_of(['Fee', 'Fi', 'Fo', 'Fum']) do |title|\n %h1= title\n %p A word!")) end @@ -119,5 +119,17 @@ class HelperTest < Test::Unit::TestCase def test_indented_capture assert_equal(" \n Foo\n ", @base.render(:inline => " <% res = capture do %>\n Foo\n <% end %><%= res %>")) end + + def test_capture_deals_properly_with_collections + Haml::Helpers.module_eval do + def trc(collection, &block) + collection.each do |record| + puts capture_haml(record, &block) + end + end + end + + assert_equal("1\n\n2\n\n3\n\n", render("- trc([1, 2, 3]) do |i|\n = i.inspect")) + end end