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
- #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
- #h(text) ⇒ Object
- #number_with_delimiter(num) ⇒ Object
- #product_version ⇒ Object
- #redis ⇒ 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.
-
#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).
-
#to_display(arg) ⇒ Object
Best-effort arg display, matching Sidekiq’s ‘to_display`.
-
#truncate(text, max = 2000) ⇒ Object
Truncate long text (display safety) — mirrors Sidekiq’s default cap.
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_nonce ⇒ Object
142 |
# File 'lib/wurk/web/extension.rb', line 142 def csp_nonce = (@csp_nonce ||= ::SecureRandom.hex(8)) |
#csrf_tag ⇒ Object
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_path ⇒ Object
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_version ⇒ Object
152 |
# File 'lib/wurk/web/extension.rb', line 152 def product_version = ::Wurk::VERSION |
#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_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 ‘“#'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, = {}) val = (@ext_strings || {}).dig(*key.to_s.split('.')) str = val.is_a?(::String) ? val : key.to_s.tr('_', ' ').capitalize .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 |