Class: WhalesController::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/whales_controller/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#paramsObject (readonly)

Returns the value of attribute params.



13
14
15
# File 'lib/whales_controller/base.rb', line 13

def params
  @params
end

#reqObject

Returns the value of attribute req.



14
15
16
# File 'lib/whales_controller/base.rb', line 14

def req
  @req
end

#resObject

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

Returns:

  • (Boolean)


26
27
28
# File 'lib/whales_controller/base.rb', line 26

def already_built_response?
  @already_built_response
end

#flashObject



30
31
32
# File 'lib/whales_controller/base.rb', line 30

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

#form_authenticity_tokenObject



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.expand_path("../../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

#sessionObject



68
69
70
# File 'lib/whales_controller/base.rb', line 68

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