Class: RapidRunty::BaseController

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

Overview

Application base controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, request) ⇒ BaseController

Returns a new instance of BaseController.



7
8
9
10
# File 'lib/rapid_runty/controller/base_controller.rb', line 7

def initialize(env, request)
  @env = env
  @request = request
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/rapid_runty/controller/base_controller.rb', line 5

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/rapid_runty/controller/base_controller.rb', line 5

def request
  @request
end

Instance Method Details

#call_action(action) ⇒ Object



12
13
14
15
16
# File 'lib/rapid_runty/controller/base_controller.rb', line 12

def call_action(action)
  send(action)
  render unless @response
  @response
end

#file(path) ⇒ Path

Find template file

Returns:

  • (Path)

    template path



52
53
54
# File 'lib/rapid_runty/controller/base_controller.rb', line 52

def file(path)
  Dir[File.join(ROOT_DIR, 'app', 'views', "#{path}.html.*")].first
end

#layoutPath

Define Layout template location

Returns:

  • (Path)

    the layout template location



60
61
62
# File 'lib/rapid_runty/controller/base_controller.rb', line 60

def layout
  File.join('layouts', 'application')
end

#paramsHash

Fetch request params

Returns:

  • (Hash)

    Hash of url parameters



22
23
24
25
26
# 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) ⇒ Object

Redirect response method



66
67
68
# File 'lib/rapid_runty/controller/base_controller.rb', line 66

def redirect_to(location)
  response([], 302, "Location" => location)
end

#render(view = controller_action) ⇒ Object

Render the template with a default layout.

Parameters:

  • file (String)

    name for the template



32
33
34
35
36
37
38
# 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) ⇒ Object

Tilt method to render specific template

Returns:

  • Rack::Response compatible response [body, status, header]



44
45
46
# File 'lib/rapid_runty/controller/base_controller.rb', line 44

def render_template(path, &block)
  Tilt.new(file(path)).render(self, &block)
end