Module: SidekiqUniqueJobs::Web::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/sidekiq_unique_jobs/web/helpers.rb

Overview

Provides view helpers for the Sidekiq::Web extension

Author:

  • Mikael Henriksson <mikael@mhenrixon.com>

Constant Summary collapse

VIEW_PATH =

Returns the path to gem specific views.

Returns:

  • (String)

    the path to gem specific views

File.expand_path("../web/views", __dir__).freeze
SAFE_CPARAMS =

Returns safe params.

Returns:

  • (Array<String>)

    safe params

%w[
  filter count cursor prev_cursor poll direction
].freeze

Instance Method Summary collapse

Instance Method Details

#_relative_time(time) ⇒ String

Gets a relative time as html

Parameters:

  • time (Time)

    an instance of Time

Returns:

  • (String)

    a html safe string with relative time information



112
113
114
115
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 112

def _relative_time(time)
  stamp = time.getutc.iso8601
  %(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>)
end

#_safe_relative_time(time) ⇒ String

Gets a relative time as html without crashing

Parameters:

  • time (Float, Integer, String, Time)

    a representation of a timestamp

Returns:

  • (String)

    a html safe string with relative time information



124
125
126
127
128
129
130
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 124

def _safe_relative_time(time)
  return unless time

  time = parse_time(time)

  _relative_time(time)
end

#cparams(options) ⇒ String

Creates url safe parameters

Parameters:

  • options (Hash)

    the key/value to parameterize

Returns:

  • (String)

    a url safe parameter string



63
64
65
66
67
68
69
70
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 63

def cparams(options)
  stringified_options = options.transform_keys(&:to_s)
  params.merge(stringified_options).filter_map do |key, value|
    next unless SAFE_CPARAMS.include?(key)

    "#{key}=#{CGI.escape(value.to_s)}"
  end.join("&")
end

#digestsSidekiqUniqueJobs::Digests

The collection of digests

Returns:



50
51
52
# 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) ⇒ String

Used to avoid incompatibility with older sidekiq versions

Parameters:

  • args (Array)

    the unique arguments to display

  • truncate_after_chars (Integer) (defaults to: 2000)

Returns:

  • (String)

    a string containing all non-truncated arguments



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 81

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) ⇒ Time

Constructs a time from a number of different types

Parameters:

  • time (Float, Integer, String, Time)

    a representation of a timestamp

Returns:

  • (Time)


139
140
141
142
143
144
145
146
147
148
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 139

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) ⇒ Object

Redirect to with falback

Parameters:

  • subpath (String)

    the path to redirect to

Returns:

  • a redirect to the new subpath



101
102
103
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 101

def redirect_to(subpath)
  redirect "#{root_path}#{subpath}"
end

#unique_filename(name) ⇒ String

Construct template file name

Parameters:

  • name (Symbol)

    the name of the template

Returns:

  • (String)

    the full name of the file



40
41
42
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 40

def unique_filename(name)
  File.join(VIEW_PATH, "#{name}.erb")
end

#unique_template(name) ⇒ String

Opens a template file contained within this gem

Parameters:

  • name (Symbol)

    the name of the template

Returns:

  • (String)

    the file contents of the template



29
30
31
# File 'lib/sidekiq_unique_jobs/web/helpers.rb', line 29

def unique_template(name)
  File.read(unique_filename(name))
end