Class: Raamen::ControllerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/raamen/controller_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ControllerBase.



13
14
15
16
17
18
19
20
21
22
# File 'lib/raamen/controller_base.rb', line 13

def initialize(req, res, route_params = {})
  @req = req
  @res = res
  @params = route_params.merge(req.params)
  @session = Session.new(req)
  @flash = Flash.new(req)
  @already_built_response = false
  @authenticity_token = generate_authenticity_token
  @@protect_from_forgery ||= false
end

Instance Attribute Details

#already_built_responseObject

Returns the value of attribute already_built_response.



11
12
13
# File 'lib/raamen/controller_base.rb', line 11

def already_built_response
  @already_built_response
end

#authenticity_tokenObject

Returns the value of attribute authenticity_token.



11
12
13
# File 'lib/raamen/controller_base.rb', line 11

def authenticity_token
  @authenticity_token
end

#flashObject (readonly)

Returns the value of attribute flash.



10
11
12
# File 'lib/raamen/controller_base.rb', line 10

def flash
  @flash
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/raamen/controller_base.rb', line 10

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



10
11
12
# File 'lib/raamen/controller_base.rb', line 10

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



10
11
12
# File 'lib/raamen/controller_base.rb', line 10

def res
  @res
end

#sessionObject (readonly)

Returns the value of attribute session.



10
11
12
# File 'lib/raamen/controller_base.rb', line 10

def session
  @session
end

Class Method Details

.protect_from_forgeryObject



74
75
76
# File 'lib/raamen/controller_base.rb', line 74

def self.protect_from_forgery
  @@protect_from_forgery = true
end

Instance Method Details

#already_built_response?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/raamen/controller_base.rb', line 24

def already_built_response?
  self.already_built_response
end

#form_authenticity_tokenObject



66
67
68
69
70
71
72
# File 'lib/raamen/controller_base.rb', line 66

def form_authenticity_token
  self.res.set_cookie(
    "authenticity_token",
    {path: "/", value: self.authenticity_token}
  )
  self.authenticity_token
end

#invoke_action(name) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/raamen/controller_base.rb', line 58

def invoke_action(name)
  if @@protect_from_forgery && self.req.request_method != "GET"
    check_authenticity_token
  end
  self.send(name)
  render(name) unless already_built_response?
end

#redirect_to(url) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/raamen/controller_base.rb', line 28

def redirect_to(url)
  raise "double render" if already_built_response?
  self.res["location"] = url
  self.res.status = 302
  self.session.store_session(res)
  self.flash.store_flash(res)
  self.already_built_response = true
end

#render(template_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/raamen/controller_base.rb', line 46

def render(template_name)
  template_path = File.join(
    Dir.pwd,
    "app",
    "views",
    "#{self.class.name.underscore}",
    "#{template_name}.html.erb"
    )
  template_content = File.read(template_path)
  render_content(ERB.new(template_content).result(binding), "text/html")
end

#render_content(content, content_type) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/raamen/controller_base.rb', line 37

def render_content(content, content_type)
  raise "double render" if already_built_response?
  self.res["Content-Type"] = content_type
  self.res.write(content)
  self.session.store_session(res)
  self.flash.store_flash(res)
  self.already_built_response = true
end