Class: RailzLite::ControllerBase

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

Direct Known Subclasses

WelcomeController

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Setup the controller



16
17
18
19
20
21
# File 'lib/railz_lite/controllers/controller_base.rb', line 16

def initialize(req, res, route_params = {})
  @req = req
  @res = res
  @params = req.params.merge(route_params)
  @@protect_from_forgery ||= false
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/railz_lite/controllers/controller_base.rb', line 13

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



13
14
15
# File 'lib/railz_lite/controllers/controller_base.rb', line 13

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



13
14
15
# File 'lib/railz_lite/controllers/controller_base.rb', line 13

def res
  @res
end

Class Method Details

.protect_from_forgeryObject



100
101
102
# File 'lib/railz_lite/controllers/controller_base.rb', line 100

def self.protect_from_forgery
  @@protect_from_forgery = true
end

Instance Method Details

#already_built_response?Boolean

Helper method to alias @already_built_response

Returns:

  • (Boolean)


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

def already_built_response?
  @already_built_response || false
end

#check_authenticity_tokenObject



108
109
110
111
112
# File 'lib/railz_lite/controllers/controller_base.rb', line 108

def check_authenticity_token
  debugger
  cookie = @req.cookies['authenticity_token']
  raise 'Invalid authenticity token' unless cookie && cookie == params['authenticity_token']
end

#flashObject

method exposing a ‘Flash` object



79
80
81
# File 'lib/railz_lite/controllers/controller_base.rb', line 79

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

#form_authenticity_tokenObject



94
95
96
97
98
# File 'lib/railz_lite/controllers/controller_base.rb', line 94

def form_authenticity_token
  @token ||= generate_authenticity_token
  res.set_cookie('authenticity_token', value: @token, path: '/')
  @token
end

#generate_authenticity_tokenObject



114
115
116
# File 'lib/railz_lite/controllers/controller_base.rb', line 114

def generate_authenticity_token
  SecureRandom.urlsafe_base64(16)
end

#invoke_action(name) ⇒ Object

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



84
85
86
87
88
89
90
91
92
# File 'lib/railz_lite/controllers/controller_base.rb', line 84

def invoke_action(name)
  if protect_from_forgery && req.request_method != 'GET'
    check_authenticity_token
  else
    form_authenticity_token 
  end
  send(name)
  render(name) unless @already_built_response
end

#protect_from_forgeryObject



104
105
106
# File 'lib/railz_lite/controllers/controller_base.rb', line 104

def protect_from_forgery
  @@protect_from_forgery
end

#redirect_to(url) ⇒ Object

Set the response status code and header



29
30
31
32
33
34
35
36
# File 'lib/railz_lite/controllers/controller_base.rb', line 29

def redirect_to(url)
  raise "Double render detected." if already_built_response?
  res['Location'] = url
  res.status = 302
  session.store_session(res)
  flash.store_flash(res)
  @already_built_response = true
end

#render(template_name) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/railz_lite/controllers/controller_base.rb', line 51

def render(template_name)
  dir_path = Dir.pwd

  layout_path = File.join(dir_path, 'views', 'application', 'application.html.erb')
  inner_file_path = File.join(dir_path, 'views', "#{self.class.name.underscore.split('_controller').first}", "#{template_name.to_s}.html.erb")

  layout_template = File.read(layout_path)
  inner_template = File.read(inner_file_path)

  layout = ERB.new(layout_template)
  inner = ERB.new(inner_template)

  layout.def_method(LayoutRenderer, 'render') # dummy method used so that blocks can be passed to ERB result

  result = LayoutRenderer.new.render do
    inner_html = inner.result(binding)
    Loofah.fragment(inner_html).scrub!(:prune).to_s # prevent non-safe html from being executed
  end

  render_content(result, '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.



41
42
43
44
45
46
47
# File 'lib/railz_lite/controllers/controller_base.rb', line 41

def render_content(content, content_type)
  raise "Double render detected." if already_built_response?
  res.write(content)
  res['Content-Type'] = content_type
  session.store_session(res)
  @already_built_response = true
end

#sessionObject

method exposing a ‘Session` object



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

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