Class: LeanWeb::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/leanweb/controller.rb

Overview

Controller is a base controller with @route, @request and @response private attributes that will be shared with your views when you #render_response.

Even if you don't #render_response, you can use the .finish method from Rack::Response on @response to return a proper Rack response.

Instance Method Summary collapse

Constructor Details

#initialize(route, request = nil) ⇒ Controller

Returns a new instance of Controller.

Parameters:

  • route (Route)
  • request (Rack::Request) (defaults to: nil)


23
24
25
26
27
28
# File 'lib/leanweb/controller.rb', line 23

def initialize(route, request = nil)
  @route = route
  @request = request
  @response = Rack::Response.new(nil, 200)
  @content_for = {}
end

Instance Method Details

#absolute_view_path(path) ⇒ String

Get absolute path for a file within VIEW_PATH.

  • A full path, starts with /.
  • A path relative to VIEW_PATH.
  • A path relative to current @route.path directory, starts with ./.

Parameters:

  • path (String)

    Can be:

Returns:

  • (String)

    Absolute path.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/leanweb/controller.rb', line 89

def absolute_view_path(path)
  return path if path.start_with?('/')

  view_path = String.new(LeanWeb::VIEW_PATH)

  if path.start_with?('./')
    view_path << @route.path.sub(%r{/[^/]*$}, '')
    path = path[2..]
  end

  path == '' ? view_path : "#{view_path}/#{path}"
end

#base_url(path = '.') ⇒ Object

Relative route to path from public directory considering current route.

Parameters:

  • path (String) (defaults to: '.')

    path from public directory, never begins with /.



75
76
77
78
79
80
81
# File 'lib/leanweb/controller.rb', line 75

def base_url(path = '.')
  @base_url ||= @route.str_path[1..]
    .sub(%r{[^/]*$}, '')
    .gsub(%r{[^/]+}, '..')

  @base_url + path
end

#create_template(path, options = nil) ⇒ String

Template rendering engine. Useful for partials / nested views.

Parameters:

  • path (String)

    Same as on #render_response.

  • options (Hash) (defaults to: nil)

    Options for Tilt, defaults to template_defaults[extension].

Returns:

  • (String)

    Rendered path.



59
60
61
62
63
64
# File 'lib/leanweb/controller.rb', line 59

def create_template(path, options = nil)
  path = absolute_view_path(path)
  ext = File.extname(path)[1..] || ''
  ext = 'erb' unless Tilt.registered?(ext)
  Tilt[ext].new(path, 1, options || template_defaults[ext] || {})
end

#default_static_action(view_path) ⇒ Object

Render response for missing static action methods. Called from Route#respond.



68
69
70
# File 'lib/leanweb/controller.rb', line 68

def default_static_action(view_path)
  render_response(view_path)
end

#paramsObject

Request params.



103
104
105
# File 'lib/leanweb/controller.rb', line 103

def params
  @request&.params
end

#render_response(path, locals = {}, content_type = nil) { ... } ⇒ Rack::Request#finish

Render magic. Supports every file extension that Tilt supports. Defaults to ERB when file extension is unknown.

Parameters:

  • path (String)

    Might be an absolute path or a relative path to src/views/.

  • locals (Hash) (defaults to: {})

    Local variables for rendered document.

  • content_type (String) (defaults to: nil)

    Defaults to the proper Content-Type for file extension, text/plain on unknown files.

Yields:

  • Optionally pass a block for nested rendering.

Returns:

  • (Rack::Request#finish)

    A valid Rack response.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/leanweb/controller.rb', line 40

def render_response(path, locals = {}, content_type = nil, &block)
  template = create_template(path)
  @response.set_header(
    'Content-Type',
    content_type || template.class.[:mime_type] ||
      template.class.superclass.[:mime_type] || 'text/plain'
  )
  @response.write(
    template.render(self, locals){ block.call if block_given? }
  )
  @response.finish
end