Class: Wurk::Web::Extension::Action
- Inherits:
-
Object
- Object
- Wurk::Web::Extension::Action
- 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.('views', __dir__)
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#env ⇒ Object
Returns the value of attribute env.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #asset_path(file) ⇒ Object included from Helpers
- #config ⇒ Object
- #csp_nonce ⇒ Object included from Helpers
-
#csrf_tag ⇒ Object
included
from Helpers
GET-form embeds don't need CSRF; return an empty, benign tag.
- #current_path ⇒ Object included from Helpers
- #display_args(args, max = 2000) ⇒ Object included from Helpers
-
#erb(content, options = {}) ⇒ Object
Render an ERB template.
- #filtering(which) ⇒ Object included from Helpers
- #h(text) ⇒ Object included from Helpers
-
#halt(res) ⇒ Object
Upstream:
throw :halt, [code, text/plain, [code.to_s]]. - #header(key, value) ⇒ Object
-
#initialize(env:, route_params:, ext:, mount:, embed: true, block: nil) ⇒ Action
constructor
A new instance of Action.
-
#job_params(job, score) ⇒ Object
included
from 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.
- #locale ⇒ Object included from Helpers
- #logger ⇒ Object
- #number_with_delimiter(num) ⇒ Object included from Helpers
-
#page(key, pageidx = 1, page_count = 25, opts = nil) ⇒ Array(Integer, Integer, Array)
included
from Paginator
The page served, the collection's size and the items.
-
#page_items(items, pageidx = 1, page_count = 25) ⇒ Object
included
from Paginator
Paginates an in-memory collection.
- #params ⇒ Object
- #parse_key(key) ⇒ Object (also: #parse_params) included from Helpers
- #processes ⇒ Object included from Helpers
- #product_version ⇒ Object included from Helpers
-
#qparams(options) ⇒ Object
included
from Helpers
The current safe query params with
optionsover them. - #redirect(location) ⇒ Object
- #redirect_to(url) ⇒ Object
-
#redirect_with_query(location) ⇒ Object
included
from Helpers
Back to
locationkeeping the query of the page the form was posted from. - #redis(idempotent: false) ⇒ Object included from Helpers
-
#relative_time(time) ⇒ Object
included
from 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.
- #render(engine, content, options = {}) ⇒ Object
- #response_headers ⇒ Object
-
#root_path ⇒ Object
included
from Helpers
Base path for this extension, trailing slash.
- #route_params(key = nil) ⇒ Object
- #rtl? ⇒ Boolean included from Helpers
-
#run(block = @block) ⇒ Object
Run the route block in this context.
- #session ⇒ Object
- #sorted_processes ⇒ Object included from Helpers
- #stats ⇒ Object included from Helpers
-
#t(key, options = {}) ⇒ Object
included
from 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 included from Helpers
-
#to_display(arg) ⇒ Object
included
from Helpers
Best-effort arg display, matching Sidekiq's
to_display. - #to_query_string(pairs) ⇒ Object included from Helpers
- #truncate(text, max = 2000) ⇒ Object included from Helpers
- #url_params(key) ⇒ Object
- #workset ⇒ Object included from Helpers
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 = @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
#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, = {}) template = content.is_a?(::Symbol) ? read_view(content) : content.to_s ::ERB.new(template, trim_mode: '-').result(view_binding([: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 "
#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
#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.
#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
196 197 198 199 200 |
# File 'lib/wurk/web/extension.rb', line 196 def render(engine, content, = {}) raise ::ArgumentError, "Only erb templates are supported (got #{engine.inspect})" unless engine == :erb erb(content, ) 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
#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] |