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

Inherits:
Object
  • Object
show all
Includes:
Helpers, Paginator
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

Constant Summary collapse

JSON_HEADERS =
{ 'Content-Type' => 'application/json', 'Cache-Control' => 'private, no-store' }.freeze
TEXT_HEADERS =
{ 'Content-Type' => 'text/plain' }.freeze
BUILTIN_VIEWS =

The built-in partials extension views render by name.

::File.expand_path('views', __dir__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Action.



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/wurk/web/extension.rb', line 146

def initialize(env:, route_params:, ext:, mount:, embed: true, block: nil)
  @block = block
  @env = env
  @request = ::Rack::Request.new(env)
  @route_params = route_params
  @mount = mount.to_s
  @embed = embed
  @subpath = env['wurk.ext.subpath']
  assign_ext(ext)
  extend_helpers(ext[:helpers])
end

Instance Attribute Details

#block ⇒ Object

Returns the value of attribute block.



161
162
163
# File 'lib/wurk/web/extension.rb', line 161

def block
  @block
end

#env ⇒ Object

Returns the value of attribute env.



161
162
163
# File 'lib/wurk/web/extension.rb', line 161

def env
  @env
end

#request ⇒ Object (readonly)

Returns the value of attribute request.



162
163
164
# File 'lib/wurk/web/extension.rb', line 162

def request
  @request
end

Instance Method Details

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

#config ⇒ Object



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

def config = ::Wurk::Web.config

#csp_nonce ⇒ Object Originally defined in module Helpers

#csrf_tag ⇒ Object Originally defined in module Helpers

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

#current_path ⇒ Object Originally defined in module Helpers

#display_args(args, max = 2000) ⇒ Object 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, or a built-in partial.



191
192
193
194
# File 'lib/wurk/web/extension.rb', line 191

def erb(content, options = {})
  template = content.is_a?(::Symbol) ? read_view(content) : content.to_s
  ::ERB.new(template, trim_mode: '-').result(view_binding(options[:locals]))
end

#filtering(which) ⇒ Object Originally defined in module Helpers

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

#halt(res) ⇒ Object

Upstream: throw :halt, [code, text/plain, [code.to_s]].



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

def halt(res) = throw(:halt, [res, TEXT_HEADERS.dup, res.to_s])

#header(key, value) ⇒ Object



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

def header(key, value) = (response_headers[key.to_s] = value.to_s)

#job_params(job, score) ⇒ Object Originally defined in module Helpers

The "-" key an extension round-trips through a URL or form.

#json(payload) ⇒ Object

A route block that returns this Rack triple is served as JSON.



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

def json(payload) = [200, JSON_HEADERS.dup, ::JSON.generate(payload)]

#locale ⇒ Object Originally defined in module Helpers

#logger ⇒ Object



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

def logger = ::Wurk.logger

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

#page(key, pageidx = 1, page_count = 25, opts = nil) ⇒ Array(Integer, Integer, Array) Originally defined in module Paginator

Returns the page served, the collection's size and the items. A sorted set yields [member, score] pairs; a list yields members. A missing key is an empty first page.

Returns:

  • (Array(Integer, Integer, Array)) —

    the page served, the collection's size and the items. A sorted set yields [member, score] pairs; a list yields members. A missing key is an empty first page.

#page_items(items, pageidx = 1, page_count = 25) ⇒ Object Originally defined in module Paginator

Paginates an in-memory collection.

#params ⇒ Object



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

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

#parse_key(key) ⇒ Object Also known as: parse_params Originally defined in module Helpers

#processes ⇒ Object Originally defined in module Helpers

#product_version ⇒ Object Originally defined in module Helpers

#qparams(options) ⇒ Object Originally defined in module Helpers

The current safe query params with options over them.

#redirect(location) ⇒ Object



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

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

#redirect_to(url) ⇒ Object



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

def redirect_to(url) = throw(:halt, Redirect.new(url, true))

#redirect_with_query(location) ⇒ Object Originally defined in module Helpers

Back to location keeping the query of the page the form was posted from.

#redis(idempotent: false) ⇒ Object 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.

#reload_page ⇒ Object

Back to the page the form was posted from. Standalone, that is the same-origin Referer like upstream; embedded, the Referer is the SPA's own URL, so the extension's index is the only page we can name.



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

def reload_page
  redirect(reload_location)
end

#render(engine, content, options = {}) ⇒ Object

Raises:

  • (::ArgumentError)


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

def render(engine, content, options = {})
  raise ::ArgumentError, "Only erb templates are supported (got #{engine.inspect})" unless engine == :erb

  erb(content, options)
end

#response_headers ⇒ Object



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

def response_headers = (@response_headers ||= {})

#root_path ⇒ Object 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



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

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

#rtl? ⇒ Boolean Originally defined in module Helpers

Returns:

  • (Boolean)

#run(block = @block) ⇒ Object

Run the route block in this context. Returns the rendered String, a Redirect, or a Rack triple from halt/json/the block itself.



204
205
206
# File 'lib/wurk/web/extension.rb', line 204

def run(block = @block)
  catch(:halt) { instance_exec(&block) }
end

#session ⇒ Object



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

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

#sorted_processes ⇒ Object Originally defined in module Helpers

#stats ⇒ Object Originally defined in module Helpers

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

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

#to_display(arg) ⇒ Object Originally defined in module Helpers

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

#to_query_string(pairs) ⇒ Object Originally defined in module Helpers

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

#url_params(key) ⇒ Object



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

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

#workset ⇒ Object Originally defined in module Helpers