Skip to content

Commit

Permalink
Merge pull request rails#5496 from atd/master
Browse files Browse the repository at this point in the history
time_tag support for blocks
  • Loading branch information
josevalim committed Mar 18, 2012
2 parents 2b5cb1c + 0540644 commit 0c18faf
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 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!
format = options.delete(:format) || :long
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

content_tag(:time, content, options.reverse_merge(:datetime => datetime))
content_tag(:time, content, options.reverse_merge(:datetime => datetime), &block)
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'))
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
time = Time.now
expected = "<time datetime=\"#{time.xmlschema}\">#{I18n.l(time, :format => :short)}</time>"
Expand Down

0 comments on commit 0c18faf

Please sign in to comment.