class ControllerBase

Attributes

flash[R]
params[R]
req[R]
res[R]
token[R]

Public Class Methods

new(req, res, route_params = {}) click to toggle source
# 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
protect_from_forgery() click to toggle source
# File lib/actioncondor/controller_base.rb, line 4
def self.protect_from_forgery
  @@protect_from_forgery = true
end

Public Instance Methods

already_built_response?() click to toggle source
# File lib/actioncondor/controller_base.rb, line 19
def already_built_response?
  @already_built_response || false
end
check_authenticity_token() click to toggle source
# 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!() click to toggle source

change to custom doubleRender error

# 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() click to toggle source
# 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_token() click to toggle source
# File lib/actioncondor/controller_base.rb, line 99
def generate_authenticity_token
  SecureRandom.urlsafe_base64(16)
end
invoke_action(name) click to toggle source
# 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?() click to toggle source
# File lib/actioncondor/controller_base.rb, line 77
def protect_from_forgery?
  @@protect_from_forgery
end
redirect_to(url) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
session() click to toggle source
# File lib/actioncondor/controller_base.rb, line 62
def session
  @session ||= Session.new(req)
end