From 4d0394b9bd2934a56640ed767fa05b59221a2301 Mon Sep 17 00:00:00 2001 From: Erin Staniland Date: Sat, 28 Nov 2009 14:20:47 +0000 Subject: [PATCH] Support for HTML5 data elements in content_for --- .../lib/action_view/helpers/record_tag_helper.rb | 15 +++++++++++++++ actionpack/test/template/record_tag_helper_test.rb | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_view/helpers/record_tag_helper.rb b/actionpack/lib/action_view/helpers/record_tag_helper.rb index 31411dc..080c8fd 100644 --- a/actionpack/lib/action_view/helpers/record_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/record_tag_helper.rb @@ -47,9 +47,24 @@ module ActionView # #
  • ... # + # The hash of options also supports HTML5 data elements. For example: + # + # <% content_tag_for(:li, @person, :data => [:first_name, :last_name]) %>... + # + # would produce the following HTML (assuming @person has the methods first_name + # and last_name with the following values): + # + #
  • ... + # def content_tag_for(tag_name, record, *args, &block) prefix = args.first.is_a?(Hash) ? nil : args.shift options = args.extract_options! + if data = options.delete(:data) + data = [data] if data.is_a?(Symbol) + data.each do |attribute| + options["data-#{attribute}"] = record.send(attribute) + end + end options.merge!({ :class => "#{dom_class(record, prefix)} #{options[:class]}".strip, :id => dom_id(record, prefix) }) content_tag(tag_name, options, &block) end diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index 1cd18c0..fc7cc76 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -7,6 +7,12 @@ class Post def id 45 end + def size + 42 + end + def created_at + DateTime.new(2009,11,11,11,11) + end def body super || "What a wonderful world!" end @@ -38,6 +44,18 @@ class RecordTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end + def test_content_tag_for_with_a_html5_data_attribute + expected = %(
  • ) + actual = content_tag_for(:li, @post, :data => :size) { } + assert_dom_equal expected, actual + end + + def test_content_tag_for_with_two_html5_data_attributes + expected = %(
  • ) + actual = content_tag_for(:li, @post, :data => [:size, :created_at]) { } + assert_dom_equal expected, actual + end + def test_block_not_in_erb_multiple_calls expected = %(
    #{@post.body}
    ) actual = div_for(@post, :class => "bar") { @post.body } -- 1.6.5.2