Sha256: 177406a88927b4e727bf3bbf1dd2df7c557aacfce03c4cd869e452570ec5c10a

Contents?: true

Size: 1.37 KB

Versions: 16

Compression:

Stored size: 1.37 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

      if sec < 1
        t 'good_job.duration.milliseconds', ms: (sec * 1000).floor
      elsif sec < 10
        t 'good_job.duration.less_than_10_seconds', sec: sec.floor
      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 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

16 entries across 16 versions & 1 rubygems

Version Path
good_job-3.30.0 app/helpers/good_job/application_helper.rb
good_job-3.29.5 app/helpers/good_job/application_helper.rb
good_job-3.29.4 app/helpers/good_job/application_helper.rb
good_job-3.29.3 app/helpers/good_job/application_helper.rb
good_job-3.28.2 app/helpers/good_job/application_helper.rb
good_job-3.28.1 app/helpers/good_job/application_helper.rb
good_job-3.28.0 app/helpers/good_job/application_helper.rb
good_job-3.27.4 app/helpers/good_job/application_helper.rb
good_job-3.27.3 app/helpers/good_job/application_helper.rb
good_job-3.27.2 app/helpers/good_job/application_helper.rb
good_job-3.27.1 app/helpers/good_job/application_helper.rb
good_job-3.27.0 app/helpers/good_job/application_helper.rb
good_job-3.26.2 app/helpers/good_job/application_helper.rb
good_job-3.26.1 app/helpers/good_job/application_helper.rb
good_job-3.26.0 app/helpers/good_job/application_helper.rb
good_job-3.25.0 app/helpers/good_job/application_helper.rb