Sha256: d27405346c05fa5784fba430005f77155c89d3e3cbe9b91114fe45c15e5f732e

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module NdrUi
  # Provides helper methods for the jQuery Timeago plugin
  module TimeagoHelper
    include CssHelper

    # 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 <tt>:html5</tt> 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>
    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
        content_tag('time', content, options.reverse_merge(datetime: timestamp))
      else
        content_tag('abbr', content, options.reverse_merge(title: timestamp))
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ndr_ui-5.0.3 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-5.0.2 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-5.0.1 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-5.0.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-4.1.2 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-4.1.1 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-3.3.1 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-3.3.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-3.2.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-3.1.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-3.0.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-2.3.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-2.2.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-2.1.1 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-2.1.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-2.0.2 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-1.12.2 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-1.12.1 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-1.12.0 app/helpers/ndr_ui/timeago_helper.rb
ndr_ui-1.8.1 app/helpers/ndr_ui/timeago_helper.rb