Module: Cosmo::Web::Renderer

Included in:
Cosmo::Web, Controllers::Application, Helpers::Application
Defined in:
lib/cosmo/web/renderer.rb

Constant Summary collapse

ASSETS_ROOT =
File.expand_path("assets", __dir__).freeze
VIEWS_ROOT =
File.expand_path("views",  __dir__).freeze

Instance Method Summary collapse

Instance Method Details

#no_contentObject



25
26
27
# File 'lib/cosmo/web/renderer.rb', line 25

def no_content
  respond_with 204, body: ""
end

#not_foundObject



29
30
31
32
33
# File 'lib/cosmo/web/renderer.rb', line 29

def not_found
  body = "<div class='alert alert-danger'>404 — Not Found</div>"
  headers = { "content-type" => "text/html; charset=utf-8" }
  respond_with 404, body:, headers:
end

#ok(body = "") ⇒ Object



20
21
22
23
# File 'lib/cosmo/web/renderer.rb', line 20

def ok(body = "")
  headers = { "content-type" => "text/html; charset=utf-8" }
  respond_with 200, body:, headers:
end

#redirect_to(url, status = 302) ⇒ Object



9
10
11
# File 'lib/cosmo/web/renderer.rb', line 9

def redirect_to(url, status = 302)
  [status, { "location" => url_for(url) }, []]
end

#serve(filename, content_type, headers = nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/cosmo/web/renderer.rb', line 13

def serve(filename, content_type, headers = nil)
  path = File.join(ASSETS_ROOT, filename)
  return not_found unless File.exist?(path)

  respond_with 200, body: File.read(path), headers: { "content-type" => content_type }.merge(headers || {})
end

#url_for(path, params = nil) ⇒ Object

Prepend the mount prefix to an internal route.

url_for("/jobs")  # => "/admin/cosmo/jobs"  (mounted)
url_for("/jobs")  # => "/jobs"              (standalone)


38
39
40
41
42
# File 'lib/cosmo/web/renderer.rb', line 38

def url_for(path, params = nil)
  url = "#{@request.script_name}#{path}"
  url = "#{url}?#{params.to_a.map { _1.join("=") }.join("&")}" if params
  url
end