Class: ControllerBase

Inherits:
Object
  • Object
show all
Extended by:
ControllerCallbacks
Includes:
UrlHelpers
Defined in:
lib/scaffold/lib/controller/controller_base.rb

Direct Known Subclasses

ApplicationController

Constant Summary

Constants included from ControllerCallbacks

ControllerCallbacks::METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ControllerCallbacks

before_action

Constructor Details

#initialize(req, res, route_params = {}, patterns) ⇒ ControllerBase

Returns a new instance of ControllerBase.



23
24
25
26
27
28
29
# File 'lib/scaffold/lib/controller/controller_base.rb', line 23

def initialize(req, res, route_params = {}, patterns)
  @req = req
  @res = res
  @params = StrongParams.new_syms(req.params.merge(route_params))
  @already_built_response = false
  self.class.make_helpers(patterns)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



17
18
19
# File 'lib/scaffold/lib/controller/controller_base.rb', line 17

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



17
18
19
# File 'lib/scaffold/lib/controller/controller_base.rb', line 17

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



17
18
19
# File 'lib/scaffold/lib/controller/controller_base.rb', line 17

def res
  @res
end

Class Method Details

.protect_from_forgeryObject



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

def self.protect_from_forgery
  @@protect_from_forgery = true
end

Instance Method Details

#form_authenticity_tokenObject



44
45
46
47
48
49
# File 'lib/scaffold/lib/controller/controller_base.rb', line 44

def form_authenticity_token
  @form_authenticity_token ||= SecureRandom::urlsafe_base64
  cookie = { path: '/', value: @form_authenticity_token }
  res.set_cookie("#{@form_authenticity_token[0..5]}authenticity_token", cookie)
  @form_authenticity_token
end

#invoke_action(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/scaffold/lib/controller/controller_base.rb', line 31

def invoke_action(name)
  if protect_from_forgery? && req.request_method != "GET"
    check_authenticity_token
  else
    form_authenticity_token
  end
  
  self.send(name)
  render name unless already_built_response?
  
  nil
end


51
52
53
# File 'lib/scaffold/lib/controller/controller_base.rb', line 51

def link_to(name, path)
  "<a href=\"#{path}\">#{name}</a>"
end

#root_urlObject



55
56
57
# File 'lib/scaffold/lib/controller/controller_base.rb', line 55

def root_url
  '/'
end