Sha256: c3ec916616be1533f54871fb9eea710fd07fd200ec009ecd7d6bd3dceee73a83

Contents?: true

Size: 1.87 KB

Versions: 122

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Ariadne
  # Displays a time ariadne-relative to how long ago it was. This component requires JavaScript.
  class TimeAgoComponent < Ariadne::Component
    DEFAULT_TAG = :"time-ago"
    TAG_OPTIONS = [DEFAULT_TAG].freeze

    DEFAULT_CLASSES = "ariadne-whitespace-nowrap"

    # @example Default
    #
    #   <%= render(Ariadne::TimeAgoComponent.new(time: Time.now)) %>
    #
    # @param tag [Symbol, String] The rendered tag name.
    # @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 classes [String] <%%= link_to_classes_docs %>
    # @param attributes [Hash] <%= link_to_attributes_docs %>
    def initialize(tag: DEFAULT_TAG, time:, micro: false, classes: "", attributes: {})
      @tag = check_incoming_tag(DEFAULT_TAG, tag)
      @classes = merge_class_names(
        DEFAULT_CLASSES,
        classes,
      )

      @time = time
      @micro = micro
      @attributes = attributes
      @attributes[:datetime] = @time.utc.iso8601
      @attributes[:format] = "micro" if @micro
    end

    def call
      render(Ariadne::BaseComponent.new(tag: @tag, classes: @classes, attributes: @attributes)) { 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

122 entries across 122 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.59-x64-mingw-ucrt app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.59-aarch64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.59-x86_64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.59-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.59-arm64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.59 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.58-x86_64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.58-aarch64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.58-arm64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.58-x64-mingw-ucrt app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.58 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.58-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.57-arm64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.57-aarch64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.57-x86_64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.57-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.57-x64-mingw-ucrt app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.57 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.55-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.55-arm64-darwin app/components/ariadne/time_ago_component.rb