Sha256: 49cb78403292df2bf334c73e5f72b79fe1c4c1040fa79509b2799dfca3748f17

Contents?: true

Size: 1.86 KB

Versions: 148

Compression:

Stored size: 1.86 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 = 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

148 entries across 148 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.29-arm64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.29-aarch64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28-x86_64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28-x64-mingw32 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28-x64-mingw-ucrt app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28-arm64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.28-aarch64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27-x86_64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27-x64-mingw32 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27-x64-mingw-ucrt app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27-arm64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.27-aarch64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.26 app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.26-x86_64-linux app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.26-x86_64-darwin app/components/ariadne/time_ago_component.rb
ariadne_view_components-0.0.26-x64-mingw32 app/components/ariadne/time_ago_component.rb