Module: Wurk::Web::Extension::Helpers
- Included in:
- Action
- Defined in:
- lib/wurk/web/helpers.rb
Overview
What extension views and routes call, Sidekiq::WebHelpers by its upstream
name (spec §25.7). Mixed into every Action. A class that includes it to test
one helper (unique-jobs' specs do) only has to answer params for the ones
that read the request.
Constant Summary collapse
- SAFE_QPARAMS =
Frozen would break sidekiq-status, which appends to it at load.
%w[page direction]
- RETRY_JOB_KEYS =
Job fields the job-detail partial already shows, so anything else is extra.
%w[ queue class args retry_count retried_at failed_at jid error_message error_class backtrace error_backtrace enqueued_at retry wrapped created_at ].freeze
Instance Method Summary collapse
- #asset_path(file) ⇒ Object
- #csp_nonce ⇒ Object
-
#csrf_tag ⇒ Object
GET-form embeds don't need CSRF; return an empty, benign tag.
- #current_path ⇒ Object
- #display_args(args, max = 2000) ⇒ Object
- #filtering(which) ⇒ Object
- #h(text) ⇒ Object
-
#job_params(job, score) ⇒ Object
The "
- " key an extension round-trips through a URL or form. - #locale ⇒ Object
- #number_with_delimiter(num) ⇒ Object
- #parse_key(key) ⇒ Object (also: #parse_params)
- #processes ⇒ Object
- #product_version ⇒ Object
-
#qparams(options) ⇒ Object
The current safe query params with
optionsover them. -
#redirect_with_query(location) ⇒ Object
Back to
locationkeeping the query of the page the form was posted from. - #redis(idempotent: false) ⇒ Object
-
#relative_time(time) ⇒ Object
<time>element like Sidekiq's relative_time; JS upgrades it client-side. -
#root_path ⇒ Object
Base path for this extension, trailing slash.
- #rtl? ⇒ Boolean
- #sorted_processes ⇒ Object
- #stats ⇒ Object
-
#t(key, options = {}) ⇒ Object
The extension's own locale strings, then the English an upstream key spells ("DeleteAll" reads "Delete All"), so a missing translation degrades and never raises.
- #text_direction ⇒ Object
-
#to_display(arg) ⇒ Object
Best-effort arg display, matching Sidekiq's
to_display. - #to_query_string(pairs) ⇒ Object
- #truncate(text, max = 2000) ⇒ Object
- #workset ⇒ Object
Instance Method Details
#asset_path(file) ⇒ Object
61 62 63 |
# File 'lib/wurk/web/helpers.rb', line 61 def asset_path(file) @embed ? "#{@mount}/ext-assets/#{@ext_name}/#{file}" : "#{@mount}/#{@ext_name}/#{file}" end |
#csp_nonce ⇒ Object
67 |
# File 'lib/wurk/web/helpers.rb', line 67 def csp_nonce = (@csp_nonce ||= ::SecureRandom.hex(8)) |
#csrf_tag ⇒ Object
GET-form embeds don't need CSRF; return an empty, benign tag.
66 |
# File 'lib/wurk/web/helpers.rb', line 66 def csrf_tag = '' |
#current_path ⇒ Object
59 |
# File 'lib/wurk/web/helpers.rb', line 59 def current_path = @subpath.to_s.sub(%r{\A/}, '') |
#display_args(args, max = 2000) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/wurk/web/helpers.rb', line 35 def display_args(args, max = 2000) return 'Invalid job payload, args is nil' if args.nil? return "Invalid job payload, args must be an Array, not #{args.class}" unless args.respond_to?(:map) return 'Invalid job payload, args must be an Array, not Hash' if args.is_a?(::Hash) args.map { |arg| h(truncate(arg.inspect, max)) }.join(', ') end |
#filtering(which) ⇒ Object
109 110 111 112 113 |
# File 'lib/wurk/web/helpers.rb', line 109 def filtering(which) action = "#{root_path}filter/#{h(which)}" input = %(<input class="search-query" type="search" name="substr" value="#{h(url_params('substr'))}">) %(<form method="post" class="form-inline" action="#{action}">#{csrf_tag}#{input}</form>) end |
#h(text) ⇒ Object
22 |
# File 'lib/wurk/web/helpers.rb', line 22 def h(text) = ::CGI.escapeHTML(text.to_s) |
#job_params(job, score) ⇒ Object
The "
81 |
# File 'lib/wurk/web/helpers.rb', line 81 def job_params(job, score) = "#{score}-#{job['jid']}" |
#locale ⇒ Object
117 |
# File 'lib/wurk/web/helpers.rb', line 117 def locale = 'en' |
#number_with_delimiter(num) ⇒ Object
78 |
# File 'lib/wurk/web/helpers.rb', line 78 def number_with_delimiter(num) = num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse |
#parse_key(key) ⇒ Object Also known as: parse_params
83 84 85 86 |
# File 'lib/wurk/web/helpers.rb', line 83 def parse_key(key) score, jid = key.to_s.split('-', 2) [score.to_f, jid] end |
#processes ⇒ Object
120 |
# File 'lib/wurk/web/helpers.rb', line 120 def processes = ::Wurk::ProcessSet.new |
#product_version ⇒ Object
77 |
# File 'lib/wurk/web/helpers.rb', line 77 def product_version = ::Wurk::VERSION |
#qparams(options) ⇒ Object
The current safe query params with options over them.
90 91 92 93 |
# File 'lib/wurk/web/helpers.rb', line 90 def qparams() current = params.to_h.transform_keys(&:to_s) to_query_string(current.merge(.transform_keys(&:to_s))) end |
#redirect_with_query(location) ⇒ Object
Back to location keeping the query of the page the form was posted from.
102 103 104 105 106 107 |
# File 'lib/wurk/web/helpers.rb', line 102 def redirect_with_query(location) query = ::URI.parse(request.referer.to_s).query redirect(query.to_s.empty? ? location : "#{location}?#{query}") rescue ::URI::InvalidURIError redirect(location) end |
#redis(idempotent: false) ⇒ Object
76 |
# File 'lib/wurk/web/helpers.rb', line 76 def redis(idempotent: false, &) = ::Wurk.redis(idempotent:, &) |
#relative_time(time) ⇒ Object
<time> element like Sidekiq's relative_time; JS upgrades it client-side.
70 71 72 73 74 |
# File 'lib/wurk/web/helpers.rb', line 70 def relative_time(time) t = time.is_a?(::Time) ? time : ::Time.at(time.to_f) iso = t.utc.iso8601 %(<time class="ltr" dir="ltr" title="#{iso}" datetime="#{iso}">#{iso}</time>) end |
#root_path ⇒ Object
Base path for this extension, trailing slash. Embedded in the
engine, ext links must land back on the embed endpoint
(/wurk/ext/<name>/…); standalone (run Sidekiq::Web, #204) the
ext's routes ARE the URL space, so it's the app root — upstream's
"#{env['SCRIPT_NAME']}/" semantics.
58 |
# File 'lib/wurk/web/helpers.rb', line 58 def root_path = @embed ? "#{@mount}/ext/#{@ext_name}/" : "#{@mount}/" |
#rtl? ⇒ Boolean
116 |
# File 'lib/wurk/web/helpers.rb', line 116 def rtl? = false |
#sorted_processes ⇒ Object
121 |
# File 'lib/wurk/web/helpers.rb', line 121 def sorted_processes = processes.to_a.sort_by { |process| process['identity'] } |
#stats ⇒ Object
118 |
# File 'lib/wurk/web/helpers.rb', line 118 def stats = (@stats ||= ::Wurk::Stats.new) |
#t(key, options = {}) ⇒ Object
The extension's own locale strings, then the English an upstream key spells ("DeleteAll" reads "Delete All"), so a missing translation degrades and never raises.
46 47 48 49 50 51 |
# File 'lib/wurk/web/helpers.rb', line 46 def t(key, = {}) val = (@ext_strings || {}).dig(*key.to_s.split('.')) str = val.is_a?(::String) ? val : humanize_key(key.to_s) .each { |k, v| str = str.gsub("%{#{k}}", v.to_s) } str end |
#text_direction ⇒ Object
115 |
# File 'lib/wurk/web/helpers.rb', line 115 def text_direction = 'ltr' |
#to_display(arg) ⇒ Object
Best-effort arg display, matching Sidekiq's to_display.
30 31 32 33 |
# File 'lib/wurk/web/helpers.rb', line 30 def to_display(arg) str = arg.inspect str.size > 100 ? "#{str[0, 100]}…" : str end |
#to_query_string(pairs) ⇒ Object
95 96 97 98 99 |
# File 'lib/wurk/web/helpers.rb', line 95 def to_query_string(pairs) pairs.filter_map do |key, value| "#{key}=#{::CGI.escape(value.to_s)}" if SAFE_QPARAMS.include?(key.to_s) end.join('&') end |
#truncate(text, max = 2000) ⇒ Object
24 25 26 27 |
# File 'lib/wurk/web/helpers.rb', line 24 def truncate(text, max = 2000) str = text.to_s max && str.size > max ? "#{str[0..max]}..." : str end |