Class: Microframe::ApplicationController
- Inherits:
-
Object
- Object
- Microframe::ApplicationController
- Defined in:
- lib/microframe/controller/application_controller.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#child ⇒ Object
readonly
Returns the value of attribute child.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#view_rendered ⇒ Object
readonly
Returns the value of attribute view_rendered.
Instance Method Summary collapse
- #default_render_option ⇒ Object
- #get_layout(layout) ⇒ Object
- #get_view(view) ⇒ Object
-
#initialize(request, child, action) ⇒ ApplicationController
constructor
A new instance of ApplicationController.
- #protected_instance_variables_for_views ⇒ Object
- #render(options = {}) ⇒ Object
- #render_error?(view, layout) ⇒ Boolean
- #render_view ⇒ Object
- #set_instance_variables_for_views ⇒ Object
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
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/microframe/controller/application_controller.rb', line 3 def action @action end |
#child ⇒ Object (readonly)
Returns the value of attribute child.
3 4 5 |
# File 'lib/microframe/controller/application_controller.rb', line 3 def child @child end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
3 4 5 |
# File 'lib/microframe/controller/application_controller.rb', line 3 def errors @errors end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
3 4 5 |
# File 'lib/microframe/controller/application_controller.rb', line 3 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
3 4 5 |
# File 'lib/microframe/controller/application_controller.rb', line 3 def request @request end |
#view_rendered ⇒ Object (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_option ⇒ Object
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_views ⇒ Object
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( = {}) @view_rendered = true view = get_view([:view]) layout = get_layout([: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
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_view ⇒ Object
34 35 36 |
# File 'lib/microframe/controller/application_controller.rb', line 34 def render_view render default_render_option end |
#set_instance_variables_for_views ⇒ Object
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 |