Class: Junior::Controller

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#attachment, #body, #content_type, #error, #halt, #headers, #last_modified, #mime_type, #not_found, #redirect, #send_file, #session, #status

Constructor Details

#initialize(app = nil, id = nil) ⇒ Controller

Returns a new instance of Controller.



9
10
11
12
13
14
15
16
# File 'lib/junior/controller.rb', line 9

def initialize(app = nil, id = nil)
  @env = app.env
  @response = app.response
  @request = app.request
  @params = @request.params

  @resource_id = id
end

Class Attribute Details

.layout_pathObject (readonly)

Returns the value of attribute layout_path.



47
48
49
# File 'lib/junior/controller.rb', line 47

def layout_path
  @layout_path
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#layout_pathObject

Returns the value of attribute layout_path.



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

def layout_path
  @layout_path
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Class Method Details

.layout(layout_path = nil) ⇒ Object



49
50
51
# File 'lib/junior/controller.rb', line 49

def layout(layout_path = nil)
  @layout_path = layout_path
end

Instance Method Details

#partial(path, *args) ⇒ Object



41
42
43
# File 'lib/junior/controller.rb', line 41

def partial(path, *args)
  render path, :layout_path => nil
end

#render(path, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/junior/controller.rb', line 18

def render(path, *args)
  
  layout_path = ( args.first && ( args.first[ :layout_path ] || args.first[ :layout_path ].nil? ) ) ? args.first[ :layout_path ] : self.class.layout_path
  
  begin
    template = Tilt.new(path)
    
    output = template.render(self)
    
    if layout_path
      layout_template = Tilt.new(layout_path)
      output = layout_template.render(self) { output }
    end
    
    response.body = output
    response.status = 200
  rescue Exception => exception
    error(500, "#{exception}")
  end
  
  output
end