Class: SolidObjects::Web::Action

Inherits:
Object
  • Object
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.

RBS:

  • (env: Hash[String, untyped], route: Route) -> void

Parameters:

  • env: (Hash[String, untyped])
  • route: (Route)


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

#envObject (readonly)

RBS:

  • @env: Hash[String, untyped]

  • @route: Route

  • @captures: Hash[Symbol, String?]

  • @request: untyped

  • @layout_rendered: bool

  • @response_status: Integer

Returns:

  • (Object)


22
23
24
# File 'lib/solid_objects/web/action.rb', line 22

def env
  @env
end

#response_statusObject (readonly)

RBS:

  • @env: Hash[String, untyped]

  • @route: Route

  • @captures: Hash[Symbol, String?]

  • @request: untyped

  • @layout_rendered: bool

  • @response_status: Integer

Returns:

  • (Object)


22
23
24
# File 'lib/solid_objects/web/action.rb', line 22

def response_status
  @response_status
end

#routeObject (readonly)

RBS:

  • @env: Hash[String, untyped]

  • @route: Route

  • @captures: Hash[Symbol, String?]

  • @request: untyped

  • @layout_rendered: bool

  • @response_status: Integer

Returns:

  • (Object)


22
23
24
# File 'lib/solid_objects/web/action.rb', line 22

def route
  @route
end

Instance Method Details

#callObject

RBS:

  • () -> untyped

Returns:

  • (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).

RBS:

  • (Symbol, ?Hash[Symbol, untyped]) -> String

Parameters:

  • (Symbol)
  • (Hash[Symbol, untyped])

Returns:

  • (String)


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.

RBS:

  • (untyped, Hash[Symbol, untyped]) -> String

Parameters:

  • (Object)
  • (Hash[Symbol, untyped])

Returns:

  • (String)


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.

RBS:

  • (Integer, ?String) -> void

Parameters:

  • (Integer)
  • (String)


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.

RBS:

  • (untyped) -> void

Parameters:

  • (Object)


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.

RBS:

  • (String) -> void

Parameters:

  • (String)


85
86
87
# File 'lib/solid_objects/web/action.rb', line 85

def redirect(path)
  throw :halt, [ 302, { "location" => path_to(path) }, [] ]
end

#requestObject

RBS:

  • () -> untyped

Returns:

  • (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?

RBS:

  • (Symbol) -> String?

Parameters:

  • (Symbol)

Returns:

  • (String, nil)


52
53
54
# File 'lib/solid_objects/web/action.rb', line 52

def route_params(key)
  @captures[key]
end

#sessionHash[untyped, untyped]?

RBS:

  • () -> Hash[untyped, untyped]?

Returns:

  • (Hash[untyped, untyped], nil)


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.

RBS:

  • (Integer) -> void

Parameters:

  • (Integer)


37
38
39
# File 'lib/solid_objects/web/action.rb', line 37

def status(code)
  @response_status = code
end

#url_params(key) ⇒ Object

RBS:

  • (String) -> untyped

Parameters:

  • (String)

Returns:

  • (Object)


57
58
59
# File 'lib/solid_objects/web/action.rb', line 57

def url_params(key)
  request.params[key]
end