Sha256: d69b5da6eb6d2ef7e941bfd44dfd49237c163075d6f7d751644629f2a38986c9

Contents?: true

Size: 1.42 KB

Versions: 24

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    module Cells
      # Formats the value as a datetime
      # @param format [String] datetime format
      # @param relative [Boolean] if true, the datetime may be(if today) shown as a relative date/time
      class DateTimeComponent < CellComponent
        include ActionView::Helpers::DateHelper

        def initialize(format:, relative:, **)
          super(**)

          @format = format
          @relative = relative
        end

        def value
          super&.to_datetime
        end

        def rendered_value
          @relative ? relative_time : absolute_time
        end

        private

        def default_html_attributes
          {
            class: "type-datetime",
            title: (absolute_time if row.body? && @relative && today?),
          }
        end

        def absolute_time
          value.present? ? I18n.l(value, format: @format) : ""
        end

        def today?
          value&.to_date == Date.current
        end

        def relative_time
          return "" if value.blank?

          if today?
            if value > DateTime.current
              "#{distance_of_time_in_words(value, DateTime.current)} from now".capitalize
            else
              "#{distance_of_time_in_words(value, DateTime.current)} ago".capitalize
            end
          else
            absolute_time
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
katalyst-tables-3.7.0 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.6.2 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.6.1 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.6.0 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.5.5 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.5.4 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.5.3 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.5.2 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.5.1 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.5.0 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.6 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.5 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.4 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.3 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.2 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.1 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.4.0 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.3.4 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.3.3 app/components/katalyst/tables/cells/date_time_component.rb
katalyst-tables-3.3.2 app/components/katalyst/tables/cells/date_time_component.rb