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



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

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

#csp_nonceObject



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

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

#csrf_tagObject

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



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

def csrf_tag = ''

#current_pathObject



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

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

#h(text) ⇒ Object



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

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

#number_with_delimiter(num) ⇒ Object



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

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

#product_versionObject



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

def product_version = ::Wurk::VERSION

#redisObject



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

def redis(&) = ::Wurk.redis(&)

#relative_time(time) ⇒ Object

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



145
146
147
148
149
# File 'lib/wurk/web/extension.rb', line 145

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 ‘“#'SCRIPT_NAME'/”` semantics.



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

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).



121
122
123
124
125
126
# File 'lib/wurk/web/extension.rb', line 121

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`.



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

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.



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

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