Class: Primer::Beta::RelativeTime

Inherits:
Component
  • Object
show all
Defined in:
app/components/primer/beta/relative_time.rb

Overview

Formats a timestamp as a localized string or as relative text that auto-updates in the user's browser.

Constant Summary collapse

TENSE_DEFAULT =
:auto
TENSE_OPTIONS =
[TENSE_DEFAULT, :past, :future].freeze
FORMAT_DEFAULT =
:auto
FORMAT_OPTIONS =
[FORMAT_DEFAULT, :micro, :elapsed].freeze
FORMAT_STYLE_DEFAULT =
nil
FORMAT_STYLE_OPTIONS =
[FORMAT_STYLE_DEFAULT, :long, :short, :narrow].freeze
SECOND_DEFAULT =
nil
SECOND_MAPPINGS =
{
  SECOND_DEFAULT => nil,
  :numeric => "numeric",
  :two_digit => "2-digit"
}.freeze
SECOND_OPTIONS =
SECOND_MAPPINGS.keys
MINUTE_DEFAULT =
nil
MINUTE_MAPPINGS =
{
  MINUTE_DEFAULT => nil,
  :numeric => "numeric",
  :two_digit => "2-digit"
}.freeze
MINUTE_OPTIONS =
MINUTE_MAPPINGS.keys
HOUR_DEFAULT =
nil
HOUR_MAPPINGS =
{
  HOUR_DEFAULT => nil,
  :numeric => "numeric",
  :two_digit => "2-digit"
}.freeze
HOUR_OPTIONS =
HOUR_MAPPINGS.keys
HOUR_CYCLE_DEFAULT =
nil
HOUR_CYCLE_OPTIONS =
[HOUR_CYCLE_DEFAULT, :h11, :h12, :h23, :h24].freeze
WEEKDAY_DEFAULT =
nil
WEEKDAY_OPTIONS =
[WEEKDAY_DEFAULT, :long, :short, :narrow].freeze
DAY_DEFAULT =
nil
DAY_MAPPINGS =
{
  DAY_DEFAULT => nil,
  :numeric => "numeric",
  :two_digit => "2-digit"
}.freeze
DAY_OPTIONS =
DAY_MAPPINGS.keys
MONTH_DEFAULT =
nil
MONTH_MAPPINGS =
{
  MONTH_DEFAULT => nil,
  :numeric => "numeric",
  :two_digit => "2-digit",
  :short => "short",
  :long => "long",
  :narrow => "narrow"
}.freeze
MONTH_OPTIONS =
MONTH_MAPPINGS.keys
YEAR_DEFAULT =
nil
YEAR_MAPPINGS =
{
  YEAR_DEFAULT => nil,
  :numeric => "numeric",
  :two_digit => "2-digit"
}.freeze
YEAR_OPTIONS =
YEAR_MAPPINGS.keys
TIMEZONENAME_DEFAULT =
nil
TIMEZONE_MAPPINGS =
{
  TIMEZONENAME_DEFAULT => nil,
  :long => "long",
  :short => "short",
  :short_offset => "shortOffset",
  :long_offset => "longOffset",
  :short_generic => "shortGeneric",
  :long_generic => "longGeneric"
}.freeze
TIMEZONENAME_OPTIONS =
TIMEZONE_MAPPINGS.keys
PRECISION_DEFAULT =
nil
PRECISION_OPTIONS =
[PRECISION_DEFAULT, :second, :minute, :hour, :day, :month, :year].freeze

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from SafeHrefHelper

SafeHrefHelper::DISALLOWED_HREF_SCHEMES

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Constants included from AttributesHelper

AttributesHelper::PLURAL_ARIA_ATTRIBUTES, AttributesHelper::PLURAL_DATA_ATTRIBUTES

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from SafeHrefHelper

unsafe_href?, #unsafe_href?

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Methods included from AttributesHelper

#aria, #data, #extract_data, #merge_aria, #merge_data, #merge_prefixed_attribute_hashes

Methods included from ExperimentalSlotHelpers

included

Methods included from ExperimentalRenderHelpers

included

Constructor Details

#initialize(datetime:, tense: TENSE_DEFAULT, prefix: nil, second: SECOND_DEFAULT, minute: MINUTE_DEFAULT, hour: HOUR_DEFAULT, hour_cycle: HOUR_CYCLE_DEFAULT, weekday: WEEKDAY_DEFAULT, day: DAY_DEFAULT, month: MONTH_DEFAULT, year: YEAR_DEFAULT, time_zone_name: TIMEZONENAME_DEFAULT, time_zone: nil, threshold: nil, precision: PRECISION_DEFAULT, format: nil, format_style: nil, lang: nil, title: nil, no_title: false, aria_hidden: nil, **system_arguments) ⇒ RelativeTime

Returns a new instance of RelativeTime.

Parameters:

  • datetime (Time) —

    The time to be formatted.

  • tense (Symbol) (defaults to: TENSE_DEFAULT) —

    Which tense to use. <%= one_of(Primer::Beta::RelativeTime::TENSE_OPTIONS) %>

  • prefix (String) (defaults to: nil) —

    What to prefix the relative time display with.

  • second (Symbol) (defaults to: SECOND_DEFAULT) —

    What format seconds should take. <%= one_of(Primer::Beta::RelativeTime::SECOND_OPTIONS) %>

  • minute (Symbol) (defaults to: MINUTE_DEFAULT) —

    What format minutes should take. <%= one_of(Primer::Beta::RelativeTime::MINUTE_OPTIONS) %>

  • hour (Symbol) (defaults to: HOUR_DEFAULT) —

    What format hours should take. <%= one_of(Primer::Beta::RelativeTime::HOUR_OPTIONS) %>

  • hour_cycle (Symbol) (defaults to: HOUR_CYCLE_DEFAULT) —

    What hour cycle should be used. <%= one_of(Primer::Beta::RelativeTime::HOUR_CYCLE_OPTIONS) %>

  • weekday (Symbol) (defaults to: WEEKDAY_DEFAULT) —

    What format weekdays should take. <%= one_of(Primer::Beta::RelativeTime::WEEKDAY_OPTIONS) %>

  • day (Symbol) (defaults to: DAY_DEFAULT) —

    What format days should take. <%= one_of(Primer::Beta::RelativeTime::DAY_OPTIONS) %>

  • month (Symbol) (defaults to: MONTH_DEFAULT) —

    What format months should take. <%= one_of(Primer::Beta::RelativeTime::MONTH_OPTIONS) %>

  • year (Symbol) (defaults to: YEAR_DEFAULT) —

    What format years should take. <%= one_of(Primer::Beta::RelativeTime::YEAR_OPTIONS) %>

  • time_zone_name (Symbol) (defaults to: TIMEZONENAME_DEFAULT) —

    What format the time zone should take. <%= one_of(Primer::Beta::RelativeTime::TIMEZONENAME_OPTIONS) %>

  • time_zone (String) (defaults to: nil) —

    The IANA time zone identifier to use for formatting (e.g., "America/New_York").

  • threshold (String) (defaults to: nil) —

    The threshold, in ISO-8601 'durations' format, at which relative time displays become absolute.

  • precision (Symbol) (defaults to: PRECISION_DEFAULT) —

    The precision elapsed time should display. <%= one_of(Primer::Beta::RelativeTime::PRECISION_OPTIONS) %>

  • format (Symbol) (defaults to: nil) —

    The format the display should take. <%= one_of(Primer::Beta::RelativeTime::FORMAT_OPTIONS) %>

  • format_style (Symbol) (defaults to: nil) —

    The format the display should take. <%= one_of(Primer::Beta::RelativeTime::FORMAT_STYLE_OPTIONS) %>

  • lang (String) (defaults to: nil) —

    The language to use.

  • title (String) (defaults to: nil) —

    Provide a custom title to the element.

  • no_title (Boolean) (defaults to: false) —

    Removes the title attribute provided on the element by default.

  • aria_hidden (Boolean) (defaults to: nil) —

    Set if the element is hidden or not.

  • system_arguments (Hash) —

    <%= link_to_system_arguments_docs %>



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/components/primer/beta/relative_time.rb', line 112

def initialize(
  datetime:,
  tense: TENSE_DEFAULT,
  prefix: nil,
  second: SECOND_DEFAULT,
  minute: MINUTE_DEFAULT,
  hour: HOUR_DEFAULT,
  hour_cycle: HOUR_CYCLE_DEFAULT,
  weekday: WEEKDAY_DEFAULT,
  day: DAY_DEFAULT,
  month: MONTH_DEFAULT,
  year: YEAR_DEFAULT,
  time_zone_name: TIMEZONENAME_DEFAULT,
  time_zone: nil,
  threshold: nil,
  precision: PRECISION_DEFAULT,
  format: nil,
  format_style: nil,
  lang: nil,
  title: nil,
  no_title: false,
  aria_hidden: nil,
  **system_arguments
)
  @system_arguments = deny_tag_argument(**system_arguments)
  @system_arguments[:tag] = "relative-time"
  @system_arguments[:tense] = tense if tense.present?
  @system_arguments[:prefix] = prefix if prefix.present?
  @system_arguments[:second] = fetch_or_fallback(SECOND_OPTIONS, second, SECOND_DEFAULT) if second.present?
  @system_arguments[:minute] = fetch_or_fallback(MINUTE_OPTIONS, minute, MINUTE_DEFAULT) if minute.present?
  @system_arguments[:hour] = fetch_or_fallback(HOUR_OPTIONS, hour, HOUR_DEFAULT) if hour.present?
  @system_arguments[:"hour-cycle"] = fetch_or_fallback(HOUR_CYCLE_OPTIONS, hour_cycle, HOUR_CYCLE_DEFAULT) if hour_cycle.present?
  @system_arguments[:weekday] = fetch_or_fallback(WEEKDAY_OPTIONS, weekday, WEEKDAY_DEFAULT) if weekday.present?
  @system_arguments[:day] = fetch_or_fallback(DAY_OPTIONS, day, DAY_DEFAULT) if day.present?
  @system_arguments[:month] = fetch_or_fallback(MONTH_OPTIONS, month, MONTH_DEFAULT) if month.present?
  @system_arguments[:year] = fetch_or_fallback(YEAR_OPTIONS, year, YEAR_DEFAULT) if year.present?
  @system_arguments[:"time-zone-name"] = fetch_or_fallback(TIMEZONENAME_OPTIONS, time_zone_name, TIMEZONENAME_DEFAULT) if time_zone_name.present?
  @system_arguments[:"time-zone"] = time_zone if time_zone.present?
  @system_arguments[:threshold] = threshold if threshold.present?
  @system_arguments[:precision] = precision if precision.present?
  @system_arguments[:title] = title if title.present?
  @system_arguments[:"no-title"] = no_title if no_title
  @system_arguments[:lang] = lang if lang.present?
  @system_arguments[:format] = fetch_or_fallback(FORMAT_OPTIONS, format, FORMAT_DEFAULT) if format.present?
  @system_arguments[:"format-style"] = format_style if format_style.present?
  @system_arguments[:"aria-hidden"] = aria_hidden if aria_hidden.present?
  if datetime.present? && datetime.respond_to?(:iso8601)
    @datetime = datetime
    @system_arguments[:datetime] = datetime.iso8601
  elsif datetime.present?
    @datetime = Time.iso8601 datetime
    @system_arguments[:datetime] = @datetime
  end
end

Instance Method Details

#call ⇒ Object



167
168
169
# File 'app/components/primer/beta/relative_time.rb', line 167

def call
  render(Primer::BaseComponent.new(**@system_arguments).with_content(@datetime.strftime("%B %-d, %Y %H:%M")))
end