class RapidRunty::BaseController

Application base controller

Attributes

env[R]
request[R]

Public Class Methods

new(env, request) click to toggle source
# File lib/rapid_runty/controller/base_controller.rb, line 7
def initialize(env, request)
  @env = env
  @request = request
end

Public Instance Methods

call_action(action) click to toggle source
# File lib/rapid_runty/controller/base_controller.rb, line 12
def call_action(action)
  send(action)
  render unless @response
  @response
end
file(path) click to toggle source

Find template file

@return [Path] template path

# File lib/rapid_runty/controller/base_controller.rb, line 52
def file(path)
  Dir[File.join(ROOT_DIR, 'app', 'views', "#{path}.html.*")].first
end
layout() click to toggle source

Define Layout template location

@return [Path] the layout template location

# File lib/rapid_runty/controller/base_controller.rb, line 60
def layout
  File.join('layouts', 'application')
end
params() click to toggle source

Fetch request params

@return [Hash] Hash of url parameters

# File lib/rapid_runty/controller/base_controller.rb, line 22
def params
  @params ||= request.params.merge(
    Rack::Utils.parse_nested_query(env['QUERY_STRING'])
  )
end
redirect_to(location) click to toggle source

Redirect response method

# File lib/rapid_runty/controller/base_controller.rb, line 66
def redirect_to(location)
  response([], 302, "Location" => location)
end
render(view = controller_action) click to toggle source

Render the template with a default layout.

@param [String] file name for the template

# File lib/rapid_runty/controller/base_controller.rb, line 32
def render(view = controller_action)
  body = render_template(layout) do
    render_template(view)
  end

  response(body, 200, {})
end
render_template(path, &block) click to toggle source

Tilt method to render specific template

@return Rack::Response compatible response [body, status, header]

# File lib/rapid_runty/controller/base_controller.rb, line 44
def render_template(path, &block)
  Tilt.new(file(path)).render(self, &block)
end

Private Instance Methods

controller_action() click to toggle source
# File lib/rapid_runty/controller/base_controller.rb, line 72
def controller_action
  File.join(env['controller'], env['action'])
end
response(body, status = 200, header = {}) click to toggle source
# File lib/rapid_runty/controller/base_controller.rb, line 76
def response(body, status = 200, header = {})
  @response = Rack::Response.new(body, status, header)
end