Class: Wurk::Web::Extension::Action

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/wurk/web/extension.rb

Overview

Per-request render context: instance-evals a matched route block, then renders ERB with the block's ivars + helpers in scope.

Defined Under Namespace

Classes: Redirect

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, route_params:, ext:, mount:, embed: true) ⇒ Action

Returns a new instance of Action.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/wurk/web/extension.rb', line 164

def initialize(env:, route_params:, ext:, mount:, embed: true)
  @env = env
  @request = ::Rack::Request.new(env)
  @route_params = route_params
  @ext_name = ext[:name].to_s
  @root_dir = ext[:root_dir]
  @ext_strings = ext[:strings]
  @mount = mount.to_s
  @embed = embed
  @subpath = env['wurk.ext.subpath']
  extend_helpers(ext[:helpers])
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



177
178
179
# File 'lib/wurk/web/extension.rb', line 177

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



177
178
179
# File 'lib/wurk/web/extension.rb', line 177

def request
  @request
end

Instance Method Details

#asset_path(file) ⇒ Object Originally defined in module Helpers

#csp_nonceObject Originally defined in module Helpers

#csrf_tagObject Originally defined in module Helpers

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

#current_pathObject Originally defined in module Helpers

#erb(content, _options = {}) ⇒ Object

Render an ERB template. content is either the template String (the common path — the ext's own helper reads the file) or a Symbol naming a *.erb under the ext's root_dir/views.



189
190
191
192
# File 'lib/wurk/web/extension.rb', line 189

def erb(content, _options = {})
  template = content.is_a?(::Symbol) ? read_view(content) : content.to_s
  ::ERB.new(template, trim_mode: '-').result(binding)
end

#h(text) ⇒ Object Originally defined in module Helpers

#loggerObject



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

def logger = ::Wurk.logger

#number_with_delimiter(num) ⇒ Object Originally defined in module Helpers

#paramsObject



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

def params = @params ||= symbolize(@request.params).merge(@route_params)

#product_versionObject Originally defined in module Helpers

#redirect(location) ⇒ Object



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

def redirect(location) = throw(:wurk_ext_halt, Redirect.new(location))

#redisObject Originally defined in module Helpers

#relative_time(time) ⇒ Object Originally defined in module Helpers

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

#root_pathObject Originally defined in module Helpers

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.

#route_params(key = nil) ⇒ Object



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

def route_params(key = nil) = key ? @route_params[key.to_sym] : @route_params

#run(block) ⇒ Object

Run the route block in this context, capturing redirects. Returns the rendered HTML String, or a Redirect.



196
197
198
# File 'lib/wurk/web/extension.rb', line 196

def run(block)
  catch(:wurk_ext_halt) { instance_exec(&block) }
end

#sessionObject



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

def session = (@env['rack.session'] ||= {})

#t(key, options = {}) ⇒ Object Originally defined in module Helpers

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 Originally defined in module Helpers

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

#truncate(text, max = 2000) ⇒ Object Originally defined in module Helpers

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

#url_params(key) ⇒ Object



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

def url_params(key) = @request.params[key.to_s]