From f2a021dc06d934f569977a05fa4e579e3f593d34 Mon Sep 17 00:00:00 2001
From: David Heinemeier Hansson
Date: Tue, 21 Jun 2005 06:43:14 +0000
Subject: Added option to pass in parameters to CaptureHelper#capture, so you
can create more advanced view helper methods #1466 [duane.johnson@gmail.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1459 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
---
actionpack/CHANGELOG | 7 +++++++
actionpack/lib/action_view/helpers/capture_helper.rb | 12 +++++++-----
actionpack/test/template/active_record_helper_test.rb | 8 ++++----
3 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index ed967db665..4d7862b811 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,12 @@
*SVN*
+* Added option to pass in parameters to CaptureHelper#capture, so you can create more advanced view helper methods #1466 [duane.johnson@gmail.com]. Example:
+
+ <% show_calendar(:year => 2005, :month => 6) do |day, options| %>
+ <% options[:bgcolor] = '#dfd' if 10..15.include? day %>
+ [<%= day %>]
+ <% end %>
+
* Changed the default name of the input tag generated by FormTagHelper#submit_tag from "submit" to "commit" so it doesn't clash with form.submit() calls in Javascript #1271
* Fixed relative urls support for lighttpd #1048 [Nicholas Seckar/maznawak@nerim.net]
diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb
index aba4a85355..1877730c7e 100644
--- a/actionpack/lib/action_view/helpers/capture_helper.rb
+++ b/actionpack/lib/action_view/helpers/capture_helper.rb
@@ -48,11 +48,11 @@ module ActionView
# <% @greeting = capture do %>
# Welcome To my shiny new web page!
# <% end %>
- def capture(&block)
+ def capture(*args, &block)
# execute the block
buffer = eval("_erbout", block.binding)
pos = buffer.length
- block.call
+ block.call(*args)
# extract the block
data = buffer[pos..-1]
@@ -77,7 +77,10 @@ module ActionView
# alert('hello world')
# <% end %>
#
- # You can use @content_for_header anywhere in your templates
+ # You can use @content_for_header anywhere in your templates.
+ #
+ # NOTE: Beware that content_for is ignored in caches. So you shouldn't use it
+ # for elements that are going to be fragment cached.
def content_for(name, &block)
base = controller.instance_variable_get(instance_var_name(name)) || ""
data = capture(&block)
@@ -85,8 +88,7 @@ module ActionView
data
end
- private
-
+ private
def instance_var_name(name) #:nodoc#
"@content_for_#{name}"
end
diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb
index 8a4e45b65f..a57df0d031 100644
--- a/actionpack/test/template/active_record_helper_test.rb
+++ b/actionpack/test/template/active_record_helper_test.rb
@@ -79,7 +79,7 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
def test_form_with_string
assert_equal(
- %(
),
+ %(),
form("post")
)
@@ -89,7 +89,7 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
def id() 1 end
end
assert_equal(
- %(),
+ %(),
form("post")
)
end
@@ -98,7 +98,7 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
assert_equal(
- %(),
+ %(),
form("post")
)
end
@@ -108,7 +108,7 @@ class ActiveRecordHelperTest < Test::Unit::TestCase
@post.written_on = Time.gm(2004, 6, 15, 16, 30)
assert_equal(
- %(),
+ %(),
form("post")
)
end
--
cgit v1.2.3