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

Methods included from Helpers

#asset_path, #csp_nonce, #csrf_tag, #current_path, #h, #number_with_delimiter, #product_version, #redis, #relative_time, #root_path, #t, #to_display, #truncate

Constructor Details

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

Returns a new instance of Action.



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/wurk/web/extension.rb', line 169

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.



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

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

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



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

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

#loggerObject



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

def logger = ::Wurk.logger

#paramsObject



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

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

#redirect(location) ⇒ Object



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

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

#route_params(key = nil) ⇒ Object



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

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.



201
202
203
# File 'lib/wurk/web/extension.rb', line 201

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

#sessionObject



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

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

#url_params(key) ⇒ Object



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

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