Class: GrailsController::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, res, params = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
# File 'lib/controller_base.rb', line 12

def initialize(req, res, params = {})
  @req = req
  @res = res
  @params = params.merge(req.params)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/controller_base.rb', line 10

def params
  @params
end

#reqObject (readonly)

Returns the value of attribute req.



10
11
12
# File 'lib/controller_base.rb', line 10

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



10
11
12
# File 'lib/controller_base.rb', line 10

def res
  @res
end

Instance Method Details

#already_built_response?Boolean

Returns:

  • (Boolean)


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

def already_built_response?
  @already_built_response
end

#invoke_action(name) ⇒ Object



47
48
49
50
# File 'lib/controller_base.rb', line 47

def invoke_action(name)
  self.send(name)
  render(name) unless already_built_response?
end

#redirect_to(url) ⇒ Object



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

def redirect_to(url)
  raise 'Already rendered' if already_built_response?
  @already_built_response = true
  @res.set_header('Location', url)
  @res.status = 302
  session.store_session(@res)
end

#render(template_name) ⇒ Object



38
39
40
41
# File 'lib/controller_base.rb', line 38

def render(template_name)
  content = ERB.new(File.read("views/#{self.class.name.underscore}/#{template_name}.html.erb")).result(binding)
  render_content(content, 'text/html')
end

#render_content(content, content_type) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/controller_base.rb', line 30

def render_content(content, content_type)
  raise 'Already rendered' if already_built_response?
  @already_built_response = true
  @res['Content-Type'] = content_type
  @res.write(content)
  session.store_session(@res)
end

#sessionObject



43
44
45
# File 'lib/controller_base.rb', line 43

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