Class: Mgt::BaseController

Inherits:
Object show all
Defined in:
lib/mgt/base_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.


5
6
7
# File 'lib/mgt/base_controller.rb', line 5

def request
  @request
end

Instance Method Details

#controller_nameObject


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

def controller_name
  self.class.to_s.gsub(/Controller$/, "").to_snake_case
end

#get_instance_variablesObject


34
35
36
37
38
39
40
41
# File 'lib/mgt/base_controller.rb', line 34

def get_instance_variables
  vars = {}
  instance_variables.each do |var|
    key = var.to_s.delete("@").to_sym
    vars[key] = instance_variable_get(var)
  end
  vars
end

#get_responseObject


15
16
17
# File 'lib/mgt/base_controller.rb', line 15

def get_response
  @response
end

#paramsObject


7
8
9
# File 'lib/mgt/base_controller.rb', line 7

def params
  request.params
end

#redirect_to(url) ⇒ Object


30
31
32
# File 'lib/mgt/base_controller.rb', line 30

def redirect_to(url)
  @response = Rack::Response.new({}, 302, "location" => url)
end

#render(*args) ⇒ Object


19
20
21
# File 'lib/mgt/base_controller.rb', line 19

def render(*args)
  response(render_template(*args))
end

#render_template(view_name, locals = {}) ⇒ Object


23
24
25
26
27
28
# File 'lib/mgt/base_controller.rb', line 23

def render_template(view_name, locals = {})
  template = Tilt::ERBTemplate.new(
    File.join(APP_ROOT, "app", "views",
              controller_name, "#{view_name}.html.erb"))
  template.render(self, locals.merge(get_instance_variables))
end

#response(body, status = 200, header = {}) ⇒ Object


11
12
13
# File 'lib/mgt/base_controller.rb', line 11

def response(body, status = 200, header = {})
  @response = Rack::Response.new(body, status, header)
end