Class: RailzLite::ControllerBase
- Inherits:
-
Object
- Object
- RailzLite::ControllerBase
- Defined in:
- lib/railz_lite/controllers/controller_base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#req ⇒ Object
readonly
Returns the value of attribute req.
-
#res ⇒ Object
readonly
Returns the value of attribute res.
Class Method Summary collapse
Instance Method Summary collapse
-
#already_built_response? ⇒ Boolean
Helper method to alias @already_built_response.
- #check_authenticity_token ⇒ Object
-
#flash ⇒ Object
method exposing a ‘Flash` object.
- #form_authenticity_token ⇒ Object
- #generate_authenticity_token ⇒ Object
-
#initialize(req, res, route_params = {}) ⇒ ControllerBase
constructor
Setup the controller.
-
#invoke_action(name) ⇒ Object
use this with the router to call action_name (:index, :show, :create…).
- #protect_from_forgery ⇒ Object
-
#redirect_to(url) ⇒ Object
Set the response status code and header.
-
#render(template_name) ⇒ Object
use ERB and binding to evaluate templates pass the rendered html to render_content.
-
#render_content(content, content_type) ⇒ Object
Populate the response with content.
-
#session ⇒ Object
method exposing a ‘Session` object.
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
#params ⇒ Object (readonly)
Returns the value of attribute params.
13 14 15 |
# File 'lib/railz_lite/controllers/controller_base.rb', line 13 def params @params end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
13 14 15 |
# File 'lib/railz_lite/controllers/controller_base.rb', line 13 def req @req end |
#res ⇒ Object (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_forgery ⇒ Object
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
24 25 26 |
# File 'lib/railz_lite/controllers/controller_base.rb', line 24 def already_built_response? @already_built_response || false end |
#check_authenticity_token ⇒ Object
108 109 110 111 112 |
# File 'lib/railz_lite/controllers/controller_base.rb', line 108 def check_authenticity_token debugger = @req.['authenticity_token'] raise 'Invalid authenticity token' unless && == params['authenticity_token'] end |
#flash ⇒ Object
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_token ⇒ Object
94 95 96 97 98 |
# File 'lib/railz_lite/controllers/controller_base.rb', line 94 def form_authenticity_token @token ||= generate_authenticity_token res.('authenticity_token', value: @token, path: '/') @token end |
#generate_authenticity_token ⇒ Object
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_forgery ⇒ Object
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 |