Class: WhalesController::Base
- Inherits:
-
Object
- Object
- WhalesController::Base
- Defined in:
- lib/whales_controller/base.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#req ⇒ Object
Returns the value of attribute req.
-
#res ⇒ Object
Returns the value of attribute res.
Instance Method Summary collapse
- #already_built_response? ⇒ Boolean
- #flash ⇒ Object
- #form_authenticity_token ⇒ Object
-
#initialize(request, response, route_params = {}) ⇒ Base
constructor
A new instance of Base.
- #invoke_action(action_name) ⇒ Object
- #redirect_to(url) ⇒ Object
- #render(template_name, file_location = nil) ⇒ Object
- #render_content(content, content_type) ⇒ Object
- #session ⇒ Object
Constructor Details
#initialize(request, response, route_params = {}) ⇒ Base
Returns a new instance of Base.
16 17 18 19 20 21 22 23 24 |
# File 'lib/whales_controller/base.rb', line 16 def initialize(request, response, route_params = {}) @req = request @res = response @params = WhalesDispatch::Params.new(req, route_params) unless req.request_method == "GET" verify_authenticity_token end end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
13 14 15 |
# File 'lib/whales_controller/base.rb', line 13 def params @params end |
#req ⇒ Object
Returns the value of attribute req.
14 15 16 |
# File 'lib/whales_controller/base.rb', line 14 def req @req end |
#res ⇒ Object
Returns the value of attribute res.
14 15 16 |
# File 'lib/whales_controller/base.rb', line 14 def res @res end |
Instance Method Details
#already_built_response? ⇒ Boolean
26 27 28 |
# File 'lib/whales_controller/base.rb', line 26 def already_built_response? @already_built_response end |
#flash ⇒ Object
30 31 32 |
# File 'lib/whales_controller/base.rb', line 30 def flash @flash ||= WhalesDispatch::Flash.new(req) end |
#form_authenticity_token ⇒ Object
34 35 36 |
# File 'lib/whales_controller/base.rb', line 34 def form_authenticity_token session["authenticity_token"] = SecureRandom.urlsafe_base64 end |
#invoke_action(action_name) ⇒ Object
38 39 40 41 |
# File 'lib/whales_controller/base.rb', line 38 def invoke_action(action_name) send(action_name) render(name) unless already_built_response? end |
#redirect_to(url) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/whales_controller/base.rb', line 52 def redirect_to(url) raise if already_built_response? self.res["Location"] = url self.res.status = 302 @already_built_response = true session.store_session(res) flash.store_flash(res) end |
#render(template_name, file_location = nil) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/whales_controller/base.rb', line 61 def render(template_name, file_location = nil) file_location ||= __FILE__ path = File.("../../views/#{self.class.to_s[0..-11].underscore}/#{template_name}.html.erb", file_location) template = ERB.new(File.read(path)) render_content(template.result(binding), "text/html") end |
#render_content(content, content_type) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/whales_controller/base.rb', line 43 def render_content(content, content_type) raise if already_built_response? self.res.content_type = content_type self.res.body = content @already_built_response = true session.store_session(res) flash.store_flash(res) end |
#session ⇒ Object
68 69 70 |
# File 'lib/whales_controller/base.rb', line 68 def session @session ||= WhalesDispatch::Session.new(req) end |