From 316dca039e72cbed40a563ea123d82504b3b2484 Mon Sep 17 00:00:00 2001 From: Norman Clarke Date: Tue, 14 Oct 2008 14:29:43 -0300 Subject: [PATCH] Added ActionView::Helpers::CaptureHelper#content_exists? to check for named content blocks set using content_for --- .../lib/action_view/helpers/capture_helper.rb | 13 +++++++++ actionpack/test/template/capture_helper_test.rb | 28 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 0 deletions(-) create mode 100644 actionpack/test/template/capture_helper_test.rb diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index e86ca27..b5d8462 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -122,6 +122,19 @@ module ActionView nil end + # Checks to see if named block of content has been set using + # +content_for+. This can be used to conditionally display + # default content for a named block if none has been set. For example: + # + # <% if content_exists? :navigation %> + # yield :navigation + # <% else %> + # <%= render :partial => "default_navigation" + # <% end %> + def content_exists?(name) + instance_variable_defined?("@content_for_#{name.to_s}") + end + # Use an alternate output buffer for the duration of the block. # Defaults to a new empty string. def with_output_buffer(buf = '') #:nodoc: diff --git a/actionpack/test/template/capture_helper_test.rb b/actionpack/test/template/capture_helper_test.rb new file mode 100644 index 0000000..f655634 --- /dev/null +++ b/actionpack/test/template/capture_helper_test.rb @@ -0,0 +1,28 @@ +require 'abstract_unit' + +class CaptureHelperTest < ActionView::TestCase + + tests ActionView::Helpers::CaptureHelper + + def test_capture + captured = capture do + "hello world" + end + assert_equal "hello world", captured + end + + def test_content_for + content_for :my_test do + "hello world" + end + assert_equal "hello world", @content_for_my_test + end + + def test_content_exists + content_for :my_test do + "hello world" + end + assert content_exists?(:my_test) + end + +end \ No newline at end of file -- 1.6.1