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.



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

#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

#loggerObject



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

def logger = ::Wurk.logger

#paramsObject



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

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

#redirect(location) ⇒ Object



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

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

#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'] ||= {})

#url_params(key) ⇒ Object



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

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