Module: Wurk::Web::Extension::Helpers

Included in:
Action
Defined in:
lib/wurk/web/extension.rb

Overview

The WebHelpers subset real extension views/routes call. Mixed into every Action. Anything an exotic gem needs beyond this can be added incrementally; the common surface (escaping, paths, time, i18n) is here.

Instance Method Summary collapse

Instance Method Details

#asset_path(file) ⇒ Object



141
142
143
# File 'lib/wurk/web/extension.rb', line 141

def asset_path(file)
  @embed ? "#{@mount}/ext-assets/#{@ext_name}/#{file}" : "#{@mount}/#{@ext_name}/#{file}"
end

#csp_nonceObject



147
# File 'lib/wurk/web/extension.rb', line 147

def csp_nonce = (@csp_nonce ||= ::SecureRandom.hex(8))

#csrf_tagObject

GET-form embeds don't need CSRF; return an empty, benign tag.



146
# File 'lib/wurk/web/extension.rb', line 146

def csrf_tag = ''

#current_pathObject



139
# File 'lib/wurk/web/extension.rb', line 139

def current_path = @subpath.to_s.sub(%r{\A/}, '')

#h(text) ⇒ Object



110
# File 'lib/wurk/web/extension.rb', line 110

def h(text) = ::CGI.escapeHTML(text.to_s)

#number_with_delimiter(num) ⇒ Object



158
# File 'lib/wurk/web/extension.rb', line 158

def number_with_delimiter(num) = num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse

#product_versionObject



157
# File 'lib/wurk/web/extension.rb', line 157

def product_version = ::Wurk::VERSION

#redis(idempotent: false) ⇒ Object



156
# File 'lib/wurk/web/extension.rb', line 156

def redis(idempotent: false, &) = ::Wurk.redis(idempotent:, &)

#relative_time(time) ⇒ Object

<time> element like Sidekiq's relative_time; JS upgrades it client-side.



150
151
152
153
154
# File 'lib/wurk/web/extension.rb', line 150

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_pathObject

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.



138
# File 'lib/wurk/web/extension.rb', line 138

def root_path = @embed ? "#{@mount}/ext/#{@ext_name}/" : "#{@mount}/"

#t(key, options = {}) ⇒ Object

i18n: look up the extension's own locale strings if present, else the humanized key (so a missing translation degrades, never raises).



126
127
128
129
130
131
# File 'lib/wurk/web/extension.rb', line 126

def t(key, options = {})
  val = (@ext_strings || {}).dig(*key.to_s.split('.'))
  str = val.is_a?(::String) ? val : key.to_s.tr('_', ' ').capitalize
  options.each { |k, v| str = str.gsub("%{#{k}}", v.to_s) }
  str
end

#to_display(arg) ⇒ Object

Best-effort arg display, matching Sidekiq's to_display.



119
120
121
122
# File 'lib/wurk/web/extension.rb', line 119

def to_display(arg)
  str = arg.inspect
  str.size > 100 ? "#{str[0, 100]}" : str
end

#truncate(text, max = 2000) ⇒ Object

Truncate long text (display safety) — mirrors Sidekiq's default cap.



113
114
115
116
# File 'lib/wurk/web/extension.rb', line 113

def truncate(text, max = 2000)
  str = text.to_s
  str.size > max ? "#{str[0, max]}" : str
end