Module: NdrUi::TimeagoHelper

Includes:
CssHelper
Defined in:
app/helpers/ndr_ui/timeago_helper.rb

Overview

Provides helper methods for the jQuery Timeago plugin

Instance Method Summary collapse

Methods included from CssHelper

#css_class_options_merge

Instance Method Details

#timeago_tag(date_or_time, options = {}) ⇒ Object

Returns an timeago tag for the given date or time. By default it returns an HTML 5 time element, but to return a legacy microformat abbr element set the :html5 key to false in the options.

timeago_tag Date.today  # =>
  <time datetime="2016-08-16" class="timeago">August 16, 2016</time>
timeago_tag Time.now  # =>
  <time datetime="2016-08-16T15:21:16+01:00" class="timeago">August 16, 2016 15:21</time>
timeago_tag Date.today, pubdate: true  # =>
  <time datetime="2016-08-16" pubdate="pubdate" class="timeago">August 16, 2016</time>
timeago_tag Date.today, class: 'apples' # =>
  <time datetime="2016-08-16" class="timeago apples">August 16, 2016</time>


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/ndr_ui/timeago_helper.rb', line 20

def timeago_tag(date_or_time, options = {})
  return if date_or_time.nil?
  options   = css_class_options_merge(options, %w(timeago))
  html5     = true unless options.delete(:html5) == false
  content   = I18n.l(date_or_time, format: :long)
  timestamp = date_or_time.iso8601

  if html5
    ('time', content, options.reverse_merge(datetime: timestamp))
  else
    ('abbr', content, options.reverse_merge(title: timestamp))
  end
end