Sha256: 3b7dc91b73402d2c7ad0d5098d24237e7fdf8ba66546dee9e1f8c43cba0ff462

Contents?: true

Size: 1.69 KB

Versions: 15

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module GoodJob
  module ApplicationHelper
    # Explicit helper inclusion because ApplicationController inherits from the host app.
    #
    # We can't rely on +config.action_controller.include_all_helpers = true+ in the host app.
    include IconsHelper

    def format_duration(sec)
      return unless sec
      return "" if sec.is_a?(String) # pg interval support added in Rails 6.1

      if sec < 1
        t 'good_job.duration.milliseconds', ms: (sec * 1000).floor
      elsif sec < 10
        t 'good_job.duration.less_than_10_seconds', sec: number_with_delimiter(sec.floor(1))
      elsif sec < 60
        t 'good_job.duration.seconds', sec: sec.floor
      elsif sec < 3600
        t 'good_job.duration.minutes', min: (sec / 60).floor, sec: (sec % 60).floor
      else
        t 'good_job.duration.hours', hour: (sec / 3600).floor, min: ((sec % 3600) / 60).floor
      end
    end

    def relative_time(timestamp, **options)
      options = options.reverse_merge({ scope: "good_job.datetime.distance_in_words" })
      text = t("good_job.helpers.relative_time.#{timestamp.future? ? 'future' : 'past'}", time: time_ago_in_words(timestamp, **options))
      tag.time(text, datetime: timestamp, title: timestamp)
    end

    def number_to_human(count)
      super(count, **translate_hash("good_job.number.human.decimal_units"))
    end

    def number_with_delimiter(count)
      super(count, **translate_hash('good_job.number.format'))
    end

    def translate_hash(key, **options)
      translation_exists?(key, **options) ? translate(key, **options) : {}
    end

    def translation_exists?(key, **options)
      I18n.exists?(scope_key_by_partial(key), **options)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
good_job-4.8.2 app/helpers/good_job/application_helper.rb
good_job-4.8.1 app/helpers/good_job/application_helper.rb
good_job-4.8.0 app/helpers/good_job/application_helper.rb
good_job-4.7.0 app/helpers/good_job/application_helper.rb
good_job-4.6.0 app/helpers/good_job/application_helper.rb
good_job-4.5.1 app/helpers/good_job/application_helper.rb
good_job-4.5.0 app/helpers/good_job/application_helper.rb
good_job-4.4.2 app/helpers/good_job/application_helper.rb
good_job-4.4.1 app/helpers/good_job/application_helper.rb
good_job-4.4.0 app/helpers/good_job/application_helper.rb
good_job-4.3.0 app/helpers/good_job/application_helper.rb
good_job-4.2.1 app/helpers/good_job/application_helper.rb
good_job-4.2.0 app/helpers/good_job/application_helper.rb
good_job-4.1.1 app/helpers/good_job/application_helper.rb
good_job-4.1.0 app/helpers/good_job/application_helper.rb