Class: RapidRunty::BaseController
- Defined in:
- lib/rapid_runty/controller/base_controller.rb
Overview
Application base controller
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #call_action(action) ⇒ Object
-
#file(path) ⇒ Path
Find template file.
-
#initialize(env, request) ⇒ BaseController
constructor
A new instance of BaseController.
-
#layout ⇒ Path
Define Layout template location.
-
#params ⇒ Hash
Fetch request params.
-
#redirect_to(location) ⇒ Object
Redirect response method.
-
#render(view = controller_action) ⇒ Object
Render the template with a default layout.
-
#render_template(path, &block) ⇒ Object
Tilt method to render specific template.
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
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/rapid_runty/controller/base_controller.rb', line 5 def env @env end |
#request ⇒ Object (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
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 |
#layout ⇒ Path
Define Layout template location
60 61 62 |
# File 'lib/rapid_runty/controller/base_controller.rb', line 60 def layout File.join('layouts', 'application') end |
#params ⇒ Hash
Fetch request params
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.
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
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 |