Sha256: 4d90f623a30df92faf7249bcad1a2a4dbc250a64c5d8f977cfee990070b7d90b

Contents?: true

Size: 1.86 KB

Versions: 18

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true
module GoodJob
  module ApplicationHelper
    def format_duration(sec)
      return unless sec

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

    def relative_time(timestamp, **args)
      text = timestamp.future? ? "in #{time_ago_in_words(timestamp, **args)}" : "#{time_ago_in_words(timestamp, **args)} ago"
      tag.time(text, datetime: timestamp, title: timestamp)
    end

    STATUS_ICONS = {
      discarded: "exclamation",
      succeeded: "check",
      queued: "dash_circle",
      retried: "arrow_clockwise",
      running: "play",
      scheduled: "clock",
    }.freeze

    STATUS_COLOR = {
      discarded: "danger",
      succeeded: "success",
      queued: "secondary",
      retried: "warning",
      running: "primary",
      scheduled: "secondary",
    }.freeze

    def status_badge(status)
      content_tag :span, status_icon(status, class: "text-white") + t(status, scope: 'good_job.status'),
                  class: "badge rounded-pill bg-#{STATUS_COLOR.fetch(status)} d-inline-flex gap-2 ps-1 pe-3 align-items-center"
    end

    def status_icon(status, **options)
      options[:class] ||= "text-#{STATUS_COLOR.fetch(status)}"
      icon = render_icon STATUS_ICONS.fetch(status)
      content_tag :span, icon, **options
    end

    def render_icon(name)
      # workaround to render svg icons without all of the log messages
      partial = lookup_context.find_template("good_job/shared/icons/#{name}", [], true)
      partial.render(self, {})
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
good_job-3.12.1 app/helpers/good_job/application_helper.rb
good_job-3.12.0 app/helpers/good_job/application_helper.rb
good_job-3.11.1 app/helpers/good_job/application_helper.rb
good_job-3.11.0 app/helpers/good_job/application_helper.rb
good_job-3.10.1 app/helpers/good_job/application_helper.rb
good_job-3.10.0 app/helpers/good_job/application_helper.rb
good_job-3.9.0 app/helpers/good_job/application_helper.rb
good_job-3.8.0 app/helpers/good_job/application_helper.rb
good_job-3.7.4 app/helpers/good_job/application_helper.rb
good_job-3.7.3 app/helpers/good_job/application_helper.rb
good_job-3.7.2 app/helpers/good_job/application_helper.rb
good_job-3.7.1 app/helpers/good_job/application_helper.rb
good_job-3.7.0 app/helpers/good_job/application_helper.rb
good_job-3.6.2 app/helpers/good_job/application_helper.rb
good_job-3.6.1 app/helpers/good_job/application_helper.rb
good_job-3.6.0 app/helpers/good_job/application_helper.rb
good_job-3.5.1 app/helpers/good_job/application_helper.rb
good_job-3.5.0 app/helpers/good_job/application_helper.rb