Class: ControllerBase
Direct Known Subclasses
Instance Attribute Summary collapse
-
#flash ⇒ Object
readonly
Returns the value of attribute flash.
-
#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.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #already_built_response? ⇒ Boolean
- #check_authenticity_token ⇒ Object
-
#check_for_repeat_action! ⇒ Object
change to custom doubleRender error.
- #form_authenticity_token ⇒ Object
- #generate_authenticity_token ⇒ Object
-
#initialize(req, res, route_params = {}) ⇒ ControllerBase
constructor
A new instance of ControllerBase.
- #invoke_action(name) ⇒ Object
- #protect_from_forgery? ⇒ Boolean
- #redirect_to(url) ⇒ Object
- #render(template_name) ⇒ Object
- #render_content(content, content_type) ⇒ Object
- #session ⇒ Object
Constructor Details
#initialize(req, res, route_params = {}) ⇒ ControllerBase
Returns a new instance of ControllerBase.
8 9 10 11 12 13 |
# File 'lib/actioncondor/controller_base.rb', line 8 def initialize(req, res, route_params = {}) @req = req @res = res @params = route_params.merge(req.params) @@protect_from_forgery ||= false end |
Instance Attribute Details
#flash ⇒ Object (readonly)
Returns the value of attribute flash.
2 3 4 |
# File 'lib/actioncondor/controller_base.rb', line 2 def flash @flash end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
2 3 4 |
# File 'lib/actioncondor/controller_base.rb', line 2 def params @params end |
#req ⇒ Object (readonly)
Returns the value of attribute req.
2 3 4 |
# File 'lib/actioncondor/controller_base.rb', line 2 def req @req end |
#res ⇒ Object (readonly)
Returns the value of attribute res.
2 3 4 |
# File 'lib/actioncondor/controller_base.rb', line 2 def res @res end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
2 3 4 |
# File 'lib/actioncondor/controller_base.rb', line 2 def token @token end |
Class Method Details
.protect_from_forgery ⇒ Object
4 5 6 |
# File 'lib/actioncondor/controller_base.rb', line 4 def self.protect_from_forgery @@protect_from_forgery = true end |
Instance Method Details
#already_built_response? ⇒ Boolean
19 20 21 |
# File 'lib/actioncondor/controller_base.rb', line 19 def already_built_response? @already_built_response || false end |
#check_authenticity_token ⇒ Object
81 82 83 84 85 86 |
# File 'lib/actioncondor/controller_base.rb', line 81 def check_authenticity_token = req.['authenticity_token'] unless && == params['authenticity_token'] raise "Invalid authenticity token" end end |
#check_for_repeat_action! ⇒ Object
change to custom doubleRender error
46 47 48 |
# File 'lib/actioncondor/controller_base.rb', line 46 def check_for_repeat_action! raise "Cannot call render/redirect twice in one action" if already_built_response? end |
#form_authenticity_token ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/actioncondor/controller_base.rb', line 88 def form_authenticity_token @token ||= generate_authenticity_token res.( 'authenticity_token', path: '/', value: token ) @token end |
#generate_authenticity_token ⇒ Object
99 100 101 |
# File 'lib/actioncondor/controller_base.rb', line 99 def generate_authenticity_token SecureRandom.urlsafe_base64(16) end |
#invoke_action(name) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/actioncondor/controller_base.rb', line 66 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? ⇒ Boolean
77 78 79 |
# File 'lib/actioncondor/controller_base.rb', line 77 def protect_from_forgery? @@protect_from_forgery end |
#redirect_to(url) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/actioncondor/controller_base.rb', line 23 def redirect_to(url) check_for_repeat_action! res.status = 302 res['location'] = url session.store_session(res) flash.store_flash(res) @already_built_response = true end |
#render(template_name) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/actioncondor/controller_base.rb', line 50 def render(template_name) controller_name = self.class.to_s.underscore[0..-("_controller".length + 1)] file_path = "app/views/#{controller_name}/#{template_name}.html.erb" file_content = File.read(file_path) application = File.read("app/views/layout/application.html.erb") application.sub!(/__YIELD__/, file_content) content = ERB.new(application).result(binding) render_content(content, 'text/html') end |
#render_content(content, content_type) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/actioncondor/controller_base.rb', line 33 def render_content(content, content_type) check_for_repeat_action! res['Content-Type'] = content_type content = content.to_json if content_type.match(/json/) res.write(content) session.store_session(res) flash.store_flash(res) @already_built_response = true end |