Class: ControllerBase

Inherits:
Object show all
Defined in:
lib/actioncondor/controller_base.rb

Direct Known Subclasses

ActionCondor::Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#flashObject (readonly)

Returns the value of attribute flash.



2
3
4
# File 'lib/actioncondor/controller_base.rb', line 2

def flash
  @flash
end

#paramsObject (readonly)

Returns the value of attribute params.



2
3
4
# File 'lib/actioncondor/controller_base.rb', line 2

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



2
3
4
# File 'lib/actioncondor/controller_base.rb', line 2

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



2
3
4
# File 'lib/actioncondor/controller_base.rb', line 2

def res
  @res
end

#tokenObject (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_forgeryObject



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

Returns:

  • (Boolean)


19
20
21
# File 'lib/actioncondor/controller_base.rb', line 19

def already_built_response?
  @already_built_response || false
end

#check_authenticity_tokenObject



81
82
83
84
85
86
# File 'lib/actioncondor/controller_base.rb', line 81

def check_authenticity_token
  cookie = req.cookies['authenticity_token']
  unless cookie && cookie == 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_tokenObject



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.set_cookie(
    'authenticity_token',
    path: '/',
    value: token
  )

  @token
end

#generate_authenticity_tokenObject



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

Returns:

  • (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

#sessionObject



62
63
64
# File 'lib/actioncondor/controller_base.rb', line 62

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