Class: Microframe::ApplicationController

Inherits:
Object
  • Object
show all
Defined in:
lib/microframe/controller/application_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, child, action) ⇒ ApplicationController

Returns a new instance of ApplicationController.



5
6
7
8
9
10
# File 'lib/microframe/controller/application_controller.rb', line 5

def initialize(request, child, action)
  @request = request
  @child = child
  @action = action
  @params = request.params
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/microframe/controller/application_controller.rb', line 3

def action
  @action
end

#childObject (readonly)

Returns the value of attribute child.



3
4
5
# File 'lib/microframe/controller/application_controller.rb', line 3

def child
  @child
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/microframe/controller/application_controller.rb', line 3

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/microframe/controller/application_controller.rb', line 3

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/microframe/controller/application_controller.rb', line 3

def request
  @request
end

#view_renderedObject (readonly)

Returns the value of attribute view_rendered.



3
4
5
# File 'lib/microframe/controller/application_controller.rb', line 3

def view_rendered
  @view_rendered
end

Instance Method Details

#default_render_optionObject



12
13
14
# File 'lib/microframe/controller/application_controller.rb', line 12

def default_render_option
  { view: action, layout: "application" }
end

#get_layout(layout) ⇒ Object



54
55
56
57
# File 'lib/microframe/controller/application_controller.rb', line 54

def get_layout(layout)
  layout ||= default_render_option[:layout]
  File.join(".", "app", "views", "layouts", layout + ".html.erb")
end

#get_view(view) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/microframe/controller/application_controller.rb', line 45

def get_view(view)
  view ||= default_render_option[:view]
  file = File.join(".", "app", "views", child, "#{view}.html.erb")
  unless File.file? file
   file = File.join(".", "app", "views", "#{view}.html.erb")
  end
  file
end

#protected_instance_variables_for_viewsObject



67
68
69
# File 'lib/microframe/controller/application_controller.rb', line 67

def protected_instance_variables_for_views
  [@request]
end

#render(options = {}) ⇒ Object



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

def render(options = {})
 @view_rendered = true
 view = get_view(options[:view])
 layout = get_layout(options[:layout])
 
 if(render_error?(view, layout))
   response = Tilt.new(File.join(".", "public", "404.html.erb"))
   response = response.render(Object.new, errors: @errors)
 else
   template = Tilt::ERBTemplate.new(layout)
   view = Tilt::ERBTemplate.new(view)
   vars = set_instance_variables_for_views
   response = template.render(self, vars){ view.render(self, vars)}
 end

 [200, {}, [response]]
end

#render_error?(view, layout) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/microframe/controller/application_controller.rb', line 38

def render_error?(view, layout)
  @errors = []
  @errors << "Couldn't find view: #{view}" unless File.file?view
  @errors << "Couldn't find layout: #{layout}" unless File.file?layout
  @errors.size > 0
end

#render_viewObject



34
35
36
# File 'lib/microframe/controller/application_controller.rb', line 34

def render_view
  render default_render_option
end

#set_instance_variables_for_viewsObject



59
60
61
62
63
64
65
# File 'lib/microframe/controller/application_controller.rb', line 59

def set_instance_variables_for_views
  hash = {}
  vars = instance_variables
  vars -= protected_instance_variables_for_views
  vars.each { |var| hash[var[1..-1]] = instance_variable_get(var)}
  hash
end