module SidekiqUniqueJobs::Web::Helpers

Provides view helpers for the Sidekiq::Web extension

@author Mikael Henriksson <mikael@mhenrixon.com>

Constants

SAFE_CPARAMS

@return [Array<String>] safe params

VIEW_PATH

@return [String] the path to gem specific views

Public Instance Methods

changelog() click to toggle source

The collection of changelog entries

@return [SidekiqUniqueJobs::Digests] the sorted set with digests

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 60
def changelog
  @changelog ||= SidekiqUniqueJobs::Changelog.new
end
cparams(options) click to toggle source

Creates url safe parameters

@param [Hash] options the key/value to parameterize

@return [String] a url safe parameter string

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 71
def cparams(options)
  stringified_options = options.transform_keys(&:to_s)
  params.merge(stringified_options).map do |key, value|
    next unless SAFE_CPARAMS.include?(key)

    "#{key}=#{CGI.escape(value.to_s)}"
  end.compact.join("&")
end
digests() click to toggle source

The collection of digests

@return [SidekiqUniqueJobs::Digests] the sorted set with digests

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 50
def digests
  @digests ||= SidekiqUniqueJobs::Digests.new
end
display_lock_args(args, truncate_after_chars = 2000) click to toggle source

Used to avoid incompatibility with older sidekiq versions

@param [Array] args the unique arguments to display @param [Integer] truncate_after_chars

@return [String] a string containing all non-truncated arguments

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 89
def display_lock_args(args, truncate_after_chars = 2000)
  return "Invalid job payload, args is nil" if args.nil?
  return "Invalid job payload, args must be an Array, not #{args.class.name}" unless args.is_a?(Array)

  begin
    args.map do |arg|
      h(truncate(to_display(arg), truncate_after_chars))
    end.join(", ")
  rescue StandardError
    "Illegal job arguments: #{h args.inspect}"
  end
end
parse_time(time) click to toggle source

Constructs a time from a number of different types

@param [Float, Integer, String, Time] time a representation of a timestamp

@return [Time]

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 151
def parse_time(time)
  case time
  when Time
    time
  when Integer, Float
    Time.at(time)
  else
    Time.parse(time.to_s)
  end
end
redirect_to(subpath) click to toggle source

Redirect to with falback

@param [String] subpath the path to redirect to

@return a redirect to the new subpath

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 109
def redirect_to(subpath)
  if respond_to?(:to)
    # Sinatra-based web UI
    redirect to(subpath)
  else
    # Non-Sinatra based web UI (Sidekiq 4.2+)
    redirect "#{root_path}#{subpath}"
  end
end
relative_time(time) click to toggle source

Gets a relative time as html

@param [Time] time an instance of Time

@return [String] a html safe string with relative time information

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 126
def relative_time(time)
  stamp = time.getutc.iso8601
  %(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>)
end
safe_relative_time(time) click to toggle source

Gets a relative time as html without crashing

@param [Float, Integer, String, Time] time a representation of a timestamp

@return [String] a html safe string with relative time information

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 138
def safe_relative_time(time)
  time = parse_time(time)

  relative_time(time)
end
unique_filename(name) click to toggle source

Construct template file name

@param [Symbol] name the name of the template

@return [String] the full name of the file

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 40
def unique_filename(name)
  File.join(VIEW_PATH, "#{name}.erb")
end
unique_template(name) click to toggle source

Opens a template file contained within this gem

@param [Symbol] name the name of the template

@return [String] the file contents of the template

# File lib/sidekiq_unique_jobs/web/helpers.rb, line 29
def unique_template(name)
  File.open(unique_filename(name)).read
end