Sha256: ab536f573c6f2cbf5126cce77c0b1fb27241515c95b1dfba0bfdcc26648273bc

Contents?: true

Size: 1.63 KB

Versions: 23

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Primer
  # Use `TimeAgo` to display a time relative to how long ago it was. This component requires JavaScript.
  class TimeAgoComponent < Primer::Component
    status :beta

    # @example Default
    #   <%= render(Primer::TimeAgoComponent.new(time: Time.at(628232400))) %>
    #
    # @param time [Time] The time to be formatted
    # @param micro [Boolean] If true then the text will be formatted in "micro" mode, using as few characters as possible
    # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
    def initialize(time:, micro: false, **system_arguments)
      @system_arguments = system_arguments
      @system_arguments[:datetime] = time.utc.iso8601
      @system_arguments[:classes] = class_names("no-wrap", @system_arguments[:classes])
      @system_arguments[:tag] = "time-ago"
      @system_arguments[:format] = "micro" if micro
      @time = time
      @micro = micro
    end

    def call
      render(Primer::BaseComponent.new(**@system_arguments)) { time_in_words }
    end

    private

    def time_in_words
      return @time.in_time_zone.strftime("%b %-d, %Y") unless @micro

      seconds_ago = Time.current - @time

      if seconds_ago < 1.minute
        "1m"
      elsif seconds_ago >= 1.minute && seconds_ago < 1.hour
        "#{(seconds_ago / 60).floor}m"
      elsif seconds_ago >= 1.hour && seconds_ago < 1.day
        "#{(seconds_ago / 60 / 60).floor}h"
      elsif seconds_ago >= 1.day && seconds_ago < 1.year
        "#{(seconds_ago / 60 / 60 / 24).floor}d"
      elsif seconds_ago >= 1.year
        "#{(seconds_ago / 60 / 60 / 24 / 365).floor}y"
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
primer_view_components-0.0.61 app/components/primer/time_ago_component.rb
primer_view_components-0.0.60 app/components/primer/time_ago_component.rb
primer_view_components-0.0.59 app/components/primer/time_ago_component.rb
primer_view_components-0.0.58 app/components/primer/time_ago_component.rb
primer_view_components-0.0.57 app/components/primer/time_ago_component.rb
primer_view_components-0.0.56 app/components/primer/time_ago_component.rb
primer_view_components-0.0.55 app/components/primer/time_ago_component.rb
primer_view_components-0.0.54 app/components/primer/time_ago_component.rb
primer_view_components-0.0.53 app/components/primer/time_ago_component.rb
primer_view_components-0.0.52 app/components/primer/time_ago_component.rb
primer_view_components-0.0.51 app/components/primer/time_ago_component.rb
primer_view_components-0.0.50 app/components/primer/time_ago_component.rb
primer_view_components-0.0.49 app/components/primer/time_ago_component.rb
primer_view_components-0.0.48 app/components/primer/time_ago_component.rb
primer_view_components-0.0.47 app/components/primer/time_ago_component.rb
primer_view_components-0.0.46 app/components/primer/time_ago_component.rb
primer_view_components-0.0.45 app/components/primer/time_ago_component.rb
primer_view_components-0.0.44 app/components/primer/time_ago_component.rb
primer_view_components-0.0.43 app/components/primer/time_ago_component.rb
primer_view_components-0.0.42 app/components/primer/time_ago_component.rb