Skip to content

Commit

Permalink
time_tag support for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
atd committed Mar 18, 2012
1 parent 2b5cb1c commit 0540644
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions actionpack/lib/action_view/helpers/date_helper.rb
Expand Up @@ -625,13 +625,18 @@ def select_year(date, options = {}, html_options = {})
# time_tag Date.today, :pubdate => true # => # time_tag Date.today, :pubdate => true # =>
# <time datetime="2010-11-04" pubdate="pubdate">November 04, 2010</time> # <time datetime="2010-11-04" pubdate="pubdate">November 04, 2010</time>
# #
def time_tag(date_or_time, *args) # <%= time_tag Time.now do %>
# <span>Right now</span>
# <% end %>
# # => <time datetime="2010-11-04T17:55:45+01:00"><span>Right now</span></time>
#
def time_tag(date_or_time, *args, &block)
options = args.extract_options! options = args.extract_options!
format = options.delete(:format) || :long format = options.delete(:format) || :long
content = args.first || I18n.l(date_or_time, :format => format) content = args.first || I18n.l(date_or_time, :format => format)
datetime = date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.rfc3339 datetime = date_or_time.acts_like?(:time) ? date_or_time.xmlschema : date_or_time.rfc3339


content_tag(:time, content, options.reverse_merge(:datetime => datetime)) content_tag(:time, content, options.reverse_merge(:datetime => datetime), &block)
end end
end end


Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/template/date_helper_test.rb
Expand Up @@ -2865,6 +2865,10 @@ def test_time_tag_with_given_text
assert_match(/<time.*>Right now<\/time>/, time_tag(Time.now, 'Right now')) assert_match(/<time.*>Right now<\/time>/, time_tag(Time.now, 'Right now'))
end end


def test_time_tag_with_given_block
assert_match(/<time.*><span>Right now<\/span><\/time>/, time_tag(Time.now){ '<span>Right now</span>'.html_safe })
end

def test_time_tag_with_different_format def test_time_tag_with_different_format
time = Time.now time = Time.now
expected = "<time datetime=\"#{time.xmlschema}\">#{I18n.l(time, :format => :short)}</time>" expected = "<time datetime=\"#{time.xmlschema}\">#{I18n.l(time, :format => :short)}</time>"
Expand Down

0 comments on commit 0540644

Please sign in to comment.