Class: Controller

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/jails/controller.rb

Instance Method Summary collapse

Methods included from Support

#blue, #pluralize, #titleize

Constructor Details

#initialize(request) ⇒ Controller

Set request object as an attribute accessible with @request variable.



5
6
7
8
# File 'lib/jails/controller.rb', line 5

def initialize(request)
  @request = request
  puts("\s Parameters #{@request.params}")  
end

Instance Method Details

Delete cookie with parameter key and redirect back.



62
63
64
65
66
67
# File 'lib/jails/controller.rb', line 62

def delete_cookie(key)
  Rack::Response.new do |response|  
    response.delete_cookie(key)
    response.redirect(@request.referer || @request.path)
  end
end

#paramsObject

helper method, params returns @request.params



11
12
13
# File 'lib/jails/controller.rb', line 11

def params
  @request.params
end

#redirect_to(path) ⇒ Object

Return Rack response with header field Location assigned to path in argument.



48
49
50
51
# File 'lib/jails/controller.rb', line 48

def redirect_to(path)
  response = Rack::Response.new([], 302, {"Location" => path})
  return response
end

#renderObject

Return Rack response object with the erb template.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jails/controller.rb', line 16

def render
  resource = params[:resource]
  action = params[:action]
  template = "#{resource}/#{action}.html.erb"
  file = File.join('app', 'views', template)
  if File.exist?(file)
    puts("\s Rendering #{template}") # Prints in log
    render_template = ERB.new(File.read(file)).result(binding)
    puts "\s Rendered #{template}"
    response = Rack::Response.new([render_template], 200, {"Content-Type" => "text/html"})
    puts "Completed 200 OK"
    return response
  else
    puts("\s Missing Template #{template}.")
    response = Rack::Response.new(["Nothing found"], 404, {"Content-Type" => "text/html"})
    return response
  end
end

#render_partial(template) ⇒ Object

Return rendered partial template to be inserted into the parent template that called it.



36
37
38
39
40
41
42
43
44
45
# File 'lib/jails/controller.rb', line 36

def render_partial(template)
  file = File.join('app', 'views', template)
  if File.exists?(file)
    rendered_partial = ERB.new(File.read(file)).result(binding)
    puts "\s Rendered #{template}"
    return rendered_partial
  else
    puts("\s Missing Template #{template}.")
  end
end

Set cookie with parameter key:value, then redirect back.



54
55
56
57
58
59
# File 'lib/jails/controller.rb', line 54

def set_cookie(key, value)
  Rack::Response.new do |response|
    response.set_cookie(key, value)
    response.redirect(@request.referer || @request.path)
  end
end