Class: SolidObjects::Web::Action
- Inherits:
-
Object
- Object
- SolidObjects::Web::Action
show all
- Includes:
- Helpers
- Defined in:
- lib/solid_objects/web/action.rb,
sig/generated/lib/solid_objects/web/action.rbs
Overview
One request. A route handler runs inside an instance of this class, so a
handler and the template it renders share the same helpers and the same
request.
Constant Summary
Constants included
from Helpers
Helpers::FORWARDED_PARAMS, Helpers::TRUNCATION_LIMIT
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
#actor_label, #chart, #chart_library_integrity_attributes, #chart_library_source, #csp_nonce, #csrf_tag, #current_path, #current_tab?, #duration, #environment_name, #filter_value, #filtered_instances, #find_instance, #form_to, #h, #instance_link, #json_block, #lease_state, #mailbox_messages, #number, #page_link, #paginate, #path_to, #query_string, #relative_time, #root_path, #statistics, #status_label, #tabs, #truncate
Constructor Details
#initialize(env:, route:) ⇒ Action
Returns a new instance of Action.
25
26
27
28
29
30
31
|
# File 'lib/solid_objects/web/action.rb', line 25
def initialize(env:, route:)
@env = env
@route = route
@captures = route.capture(env["PATH_INFO"].to_s)
@layout_rendered = false
@response_status = 200
end
|
Instance Attribute Details
#env ⇒ Object
22
23
24
|
# File 'lib/solid_objects/web/action.rb', line 22
def env
@env
end
|
#response_status ⇒ Object
22
23
24
|
# File 'lib/solid_objects/web/action.rb', line 22
def response_status
@response_status
end
|
#route ⇒ Object
22
23
24
|
# File 'lib/solid_objects/web/action.rb', line 22
def route
@route
end
|
Instance Method Details
#call ⇒ Object
62
63
64
|
# File 'lib/solid_objects/web/action.rb', line 62
def call
instance_exec(&route.handler)
end
|
#erb(name, locals = {}) ⇒ String
The layout is rendered once per request. The flag is raised before the
page body runs so a partial the body renders returns its own fragment
rather than a second whole page. The layout reads the page it wraps
from locals.fetch(:content).
71
72
73
74
75
76
77
|
# File 'lib/solid_objects/web/action.rb', line 71
def erb(name, locals = {})
return evaluate(Web.template(name), locals) if @layout_rendered
@layout_rendered = true
content = evaluate(Web.template(name), locals)
evaluate(Web.template(:layout), { content: })
end
|
#evaluate(template, locals) ⇒ String
locals is a local variable of this method, so a template reads it by
name, and every helper is reachable because the template runs against
this object.
104
105
106
|
# File 'lib/solid_objects/web/action.rb', line 104
def evaluate(template, locals)
template.result(binding)
end
|
#halt(status, body = ::Rack::Utils::HTTP_STATUS_CODES.fetch(status, "Error")) ⇒ void
This method returns an undefined value.
80
81
82
|
# File 'lib/solid_objects/web/action.rb', line 80
def halt(status, body = ::Rack::Utils::HTTP_STATUS_CODES.fetch(status, "Error"))
throw :halt, [ status, { "content-type" => "text/plain" }, [ body ] ]
end
|
#json(payload) ⇒ void
This method returns an undefined value.
90
91
92
93
94
95
96
|
# File 'lib/solid_objects/web/action.rb', line 90
def json(payload)
throw :halt, [
200,
{ "content-type" => "application/json", "cache-control" => "private, no-store" },
[ JSON.generate(payload) ]
]
end
|
#redirect(path) ⇒ void
This method returns an undefined value.
85
86
87
|
# File 'lib/solid_objects/web/action.rb', line 85
def redirect(path)
throw :halt, [ 302, { "location" => path_to(path) }, [] ]
end
|
#request ⇒ Object
42
43
44
|
# File 'lib/solid_objects/web/action.rb', line 42
def request
@request ||= ::Rack::Request.new(env)
end
|
#route_params(key) ⇒ String?
52
53
54
|
# File 'lib/solid_objects/web/action.rb', line 52
def route_params(key)
@captures[key]
end
|
#session ⇒ Hash[untyped, untyped]?
47
48
49
|
# File 'lib/solid_objects/web/action.rb', line 47
def session
env["rack.session"]
end
|
#status(code) ⇒ void
This method returns an undefined value.
Sets the status a rendered page is served with. halt and redirect
stop the handler, so a page that must render its own body and still
report a failure needs this instead.
37
38
39
|
# File 'lib/solid_objects/web/action.rb', line 37
def status(code)
@response_status = code
end
|
#url_params(key) ⇒ Object
57
58
59
|
# File 'lib/solid_objects/web/action.rb', line 57
def url_params(key)
request.params[key]
end
|