Class: Microframe::ApplicationController

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#form_for, #image_tag, #javascript_tag, #link_to, #stylesheet_tag

Constructor Details

#initialize(request, child, action) ⇒ ApplicationController

Returns a new instance of ApplicationController.



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

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.



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

def action
  @action
end

#childObject (readonly)

Returns the value of attribute child.



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

def child
  @child
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#view_renderedObject (readonly)

Returns the value of attribute view_rendered.



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

def view_rendered
  @view_rendered
end

#view_varsObject (readonly)

Returns the value of attribute view_vars.



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

def view_vars
  @view_vars
end

Instance Method Details

#default_render_optionObject



16
17
18
# File 'lib/microframe/controller/application_controller.rb', line 16

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

#get_layout(layout = nil) ⇒ Object



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

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

#get_view(view = nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/microframe/controller/application_controller.rb', line 56

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

#protected_instance_variables_for_viewsObject



85
86
87
# File 'lib/microframe/controller/application_controller.rb', line 85

def protected_instance_variables_for_views
  [:@request, :@action, :@view_rendered, :@child]
end

#redirect_to(location) ⇒ Object



20
21
22
23
# File 'lib/microframe/controller/application_controller.rb', line 20

def redirect_to(location)
  @view_rendered = true
  [302, {"Location" => location}, []]
end

#render(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/microframe/controller/application_controller.rb', line 25

def render(options = {})
 @view_rendered = true
 view = get_view(options[:view])
 layout = get_layout(options[:layout])
 obj = set_up_view_object
 status = 200

 if(render_error?(view, layout))
   response = Tilt.new(File.join(APP_PATH, "public", "404.html.erb"))
   status = 404
   response = response.render(obj, errors: @errors)
 else
   template = Tilt::ERBTemplate.new(layout)
   view = Tilt::ERBTemplate.new(view)
   response = template.render(obj){ view.render(obj)}
 end

 [status, {}, [response]]
end

#render_error?(view, layout) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/microframe/controller/application_controller.rb', line 49

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_partial(partial) ⇒ Object



70
71
72
73
74
75
# File 'lib/microframe/controller/application_controller.rb', line 70

def render_partial(partial)
  partial = partial.split("/")
  partial[-1] = "_#{partial[-1]}"
  partial = Tilt::ERBTemplate.new(get_view(partial.join("/")))
  partial.render(self)
end

#render_viewObject



45
46
47
# File 'lib/microframe/controller/application_controller.rb', line 45

def render_view
  render default_render_option
end

#set_instance_variables_for_viewsObject



77
78
79
80
81
82
83
# File 'lib/microframe/controller/application_controller.rb', line 77

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

#set_up_view_objectObject



89
90
91
92
93
94
95
# File 'lib/microframe/controller/application_controller.rb', line 89

def set_up_view_object
  obj = ViewObject.new(self)
  obj.instance_exec(set_instance_variables_for_views) do |inst_vars|
    inst_vars.each{|key, value| instance_variable_set("@#{key}", value) }
  end
  obj
end