class TGauge::TControllerBase

Attributes

params[R]
req[R]
res[R]

Public Class Methods

new(req, res, route_params = {}) click to toggle source

Setup the controller

# File lib/app/controllers/tcontroller_base.rb, line 17
def initialize(req, res, route_params = {})
  @req = req
  @res = res
  @params = @req.params.merge(route_params)
end
protect_from_forgery() click to toggle source
# File lib/app/controllers/tcontroller_base.rb, line 11
def self.protect_from_forgery
  @@defender = true
end

Public Instance Methods

already_built_response?() click to toggle source

Helper method to alias @already_built_response

# File lib/app/controllers/tcontroller_base.rb, line 24
def already_built_response?
  @rendered
end
flash() click to toggle source
# File lib/app/controllers/tcontroller_base.rb, line 62
def flash
  @flash ||= Flash.new(@req)
end
form_authenticity_token() click to toggle source
# File lib/app/controllers/tcontroller_base.rb, line 78
def form_authenticity_token
  flash[:_csurf_master_code] = SecureRandom.urlsafe_base64
end
invoke_action(name) click to toggle source

use this with the router to call action_name (:index, :show, :create…)

# File lib/app/controllers/tcontroller_base.rb, line 67
def invoke_action(name)
  if @@defender && check_authenticity_token
    @res.write("ATTACK ATTACK!! RUN AND HIDE!")
    @res.status = 403
    @res['Content-Type'] = "text/html"
  else
    self.send(name)
    render(name) unless already_built_response?
  end
end
redirect_to(url) click to toggle source

Set the response status code and header

# File lib/app/controllers/tcontroller_base.rb, line 29
def redirect_to(url)
  @rendered ? raise {'Cannote render twice'} : @rendered = true
  @res['Location'] = url
  @res.status = 302
  @session.store_session(res) if @session
end
render(template_name) click to toggle source

use ERB and binding to evaluate templates pass the rendered html to render_content

# File lib/app/controllers/tcontroller_base.rb, line 49
def render(template_name)
  class_name = self.class.to_s.underscore
  class_name.slice! "_controller"
  view_path = "app/views/#{class_name}/#{template_name}.html.erb"
  erb = ERB.new(File.read(view_path)).result(binding)
  render_content(erb, 'text/html')
end
render_content(content, content_type) click to toggle source

Populate the response with content. Set the response's content type to the given type. Raise an error if the developer tries to double render.

# File lib/app/controllers/tcontroller_base.rb, line 39
def render_content(content, content_type)
  @rendered ? raise {'Cannote render twice'} : @rendered = true
  @res.write(content)
  @session.store_session(res) if @session
  @flash.store_flash(res) if @flash
  @res['Content-Type'] = content_type
end
session() click to toggle source

method exposing a `Session` object

# File lib/app/controllers/tcontroller_base.rb, line 58
def session
  @session ||= Session.new(@req)
end

Private Instance Methods

check_authenticity_token() click to toggle source
# File lib/app/controllers/tcontroller_base.rb, line 83
def check_authenticity_token
  !@req.get? && (master_code.nil? || master_code != @req.params["authenticity_token"])
end
master_code() click to toggle source
# File lib/app/controllers/tcontroller_base.rb, line 87
def master_code
  flash[:_csurf_master_code]
end