Class: TGauge::TControllerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/app/controllers/tcontroller_base.rb

Constant Summary collapse

@@defender =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, res, route_params = {}) ⇒ TControllerBase

Setup the controller



17
18
19
20
21
# 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

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



15
16
17
# File 'lib/app/controllers/tcontroller_base.rb', line 15

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



15
16
17
# File 'lib/app/controllers/tcontroller_base.rb', line 15

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



15
16
17
# File 'lib/app/controllers/tcontroller_base.rb', line 15

def res
  @res
end

Class Method Details

.protect_from_forgeryObject



11
12
13
# File 'lib/app/controllers/tcontroller_base.rb', line 11

def self.protect_from_forgery
  @@defender = true
end

Instance Method Details

#already_built_response?Boolean

Helper method to alias @already_built_response

Returns:

  • (Boolean)


24
25
26
# File 'lib/app/controllers/tcontroller_base.rb', line 24

def already_built_response?
  @rendered
end

#flashObject



62
63
64
# File 'lib/app/controllers/tcontroller_base.rb', line 62

def flash
  @flash ||= Flash.new(@req)
end

#form_authenticity_tokenObject



78
79
80
# File 'lib/app/controllers/tcontroller_base.rb', line 78

def form_authenticity_token
  flash[:_csurf_master_code] = SecureRandom.urlsafe_base64
end

#invoke_action(name) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object

Set the response status code and header



29
30
31
32
33
34
# 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) ⇒ Object

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



49
50
51
52
53
54
55
# 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) ⇒ Object

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.



39
40
41
42
43
44
45
# 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

#sessionObject

method exposing a ‘Session` object



58
59
60
# File 'lib/app/controllers/tcontroller_base.rb', line 58

def session
  @session ||= Session.new(@req)
end