class Microframe::ApplicationController

Attributes

action[R]
child[R]
errors[R]
params[R]
request[R]
view_rendered[R]
view_vars[R]

Public Class Methods

new(request, child, action) click to toggle source
# File lib/microframe/controller/application_controller.rb, line 9
def initialize(request, child, action)
  @request = request
  @child = child
  @action = action
  @params = request.params
end

Public Instance Methods

default_render_option() click to toggle source
# File lib/microframe/controller/application_controller.rb, line 16
def default_render_option
  { view: action, layout: "application" }
end
get_layout(layout = nil) click to toggle source
# 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) click to toggle source
# 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_views() click to toggle source
# File lib/microframe/controller/application_controller.rb, line 85
def protected_instance_variables_for_views
  [:@request, :@action, :@view_rendered, :@child]
end
redirect_to(location) click to toggle source
# File lib/microframe/controller/application_controller.rb, line 20
def redirect_to(location)
  @view_rendered = true
  [302, {"Location" => location}, []]
end
render(options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_view() click to toggle source
# File lib/microframe/controller/application_controller.rb, line 45
def render_view
  render default_render_option
end
set_instance_variables_for_views() click to toggle source
# 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_object() click to toggle source
# 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