Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time_tag support for blocks #5496

Merged
merged 1 commit into from
Mar 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions actionpack/lib/action_view/helpers/date_helper.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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