Class: ControllerBase
- Inherits:
-
Object
show all
- Includes:
- CSRF
- Defined in:
- lib/laris/controller.rb,
lib/laris/controller/controller_base.rb
Defined Under Namespace
Classes: DoubleRenderError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from CSRF
#form_authenticity_token, #verify_authenticity
Constructor Details
#initialize(req, res, params = {}) ⇒ ControllerBase
Returns a new instance of ControllerBase.
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/laris/controller/controller_base.rb', line 10
def initialize(req, res, params = {})
@req = req
@res = res
@params = params.merge(req.params)
body = req.body.read
if body =~ /^{.*}$/
@params.merge!(JSON.parse(body))
end
end
|
Instance Attribute Details
Returns the value of attribute params.
8
9
10
|
# File 'lib/laris/controller/controller_base.rb', line 8
def params
@params
end
|
Returns the value of attribute req.
8
9
10
|
# File 'lib/laris/controller/controller_base.rb', line 8
def req
@req
end
|
Returns the value of attribute res.
8
9
10
|
# File 'lib/laris/controller/controller_base.rb', line 8
def res
@res
end
|
Instance Method Details
#already_built_response? ⇒ Boolean
21
22
23
|
# File 'lib/laris/controller/controller_base.rb', line 21
def already_built_response?
@already_built_response
end
|
67
68
69
|
# File 'lib/laris/controller/controller_base.rb', line 67
def flash
@flash ||= Flash.new(req)
end
|
25
26
27
|
# File 'lib/laris/controller/controller_base.rb', line 25
def h(text)
CGI::escapeHTML(text)
end
|
#invoke_action(name, method) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/laris/controller/controller_base.rb', line 71
def invoke_action(name, method)
unless method == :get || req.xhr?
verify_authenticity
end
self.send(name)
render(name) unless already_built_response?
end
|
#redirect_to(url) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/laris/controller/controller_base.rb', line 29
def redirect_to(url)
raise DoubleRenderError if already_built_response?
res['Location'] = url
res.status = 302
store_cookies(res)
@already_built_response = true
end
|
#render(template_name) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/laris/controller/controller_base.rb', line 49
def render(template_name)
path = File.join(
Laris::ROOT,
'app/views',
controller_name.remove("_controller"),
"#{template_name}.html.erb",
)
template = File.read(path)
content = ERB.new(template).result(binding)
render_content(content, "text/html")
end
|
#render_content(content, content_type) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/laris/controller/controller_base.rb', line 39
def render_content(content, content_type)
raise DoubleRenderError if already_built_response?
res['Content-Type'] = content_type
res.write(content)
store_cookies(res)
@already_built_response = true
end
|
63
64
65
|
# File 'lib/laris/controller/controller_base.rb', line 63
def session
@session ||= Session.new(req)
end
|