Class: Rory::Controller
- Inherits:
-
Object
- Object
- Rory::Controller
- Includes:
- PathGeneration
- Defined in:
- lib/rory/controller.rb
Overview
Interface for Controller class. Subclass this to create controllers with actions that will be called by the Dispatcher when a route matches.
Instance Attribute Summary collapse
-
#locals ⇒ Object
Returns the value of attribute locals.
Class Method Summary collapse
-
.after_action(method_name, opts = {}) ⇒ Object
Register a method to run after the action method.
- .after_actions ⇒ Object
- .ancestor_actions(action_type) ⇒ Object
-
.before_action(method_name, opts = {}) ⇒ Object
Register a method to run before the action method.
- .before_actions ⇒ Object
Instance Method Summary collapse
- #base_path ⇒ Object
- #default_renderer_options ⇒ Object
- #expose(hsh) ⇒ Object
-
#initialize(request, routing, app = nil) ⇒ Controller
constructor
A new instance of Controller.
- #layout ⇒ Object
- #params ⇒ Object
- #present ⇒ Object
- #redirect(path) ⇒ Object
- #render(template_name, opts = {}) ⇒ Object
- #render_not_found ⇒ Object
- #route_template ⇒ Object
Methods included from PathGeneration
Constructor Details
#initialize(request, routing, app = nil) ⇒ Controller
Returns a new instance of Controller.
39 40 41 42 43 44 45 46 |
# File 'lib/rory/controller.rb', line 39 def initialize(request, routing, app = nil) @request = request @dispatcher = routing[:dispatcher] @route = routing[:route] @params = request.params @app = app @locals = {} end |
Instance Attribute Details
#locals ⇒ Object
Returns the value of attribute locals.
10 11 12 |
# File 'lib/rory/controller.rb', line 10 def locals @locals end |
Class Method Details
.after_action(method_name, opts = {}) ⇒ Object
Register a method to run after the action method.
27 28 29 |
# File 'lib/rory/controller.rb', line 27 def after_action(method_name, opts = {}) after_actions << opts.merge(:method_name => method_name) end |
.after_actions ⇒ Object
17 18 19 |
# File 'lib/rory/controller.rb', line 17 def after_actions @after_actions ||= ancestor_actions(:after) end |
.ancestor_actions(action_type) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/rory/controller.rb', line 31 def ancestor_actions(action_type) (ancestors - [self]).reverse.map { |c| query_method = :"#{action_type}_actions" c.send(query_method) if c.respond_to?(query_method) }.flatten.compact end |
.before_action(method_name, opts = {}) ⇒ Object
Register a method to run before the action method.
22 23 24 |
# File 'lib/rory/controller.rb', line 22 def before_action(method_name, opts = {}) before_actions << opts.merge(:method_name => method_name) end |
.before_actions ⇒ Object
13 14 15 |
# File 'lib/rory/controller.rb', line 13 def before_actions @before_actions ||= ancestor_actions(:before) end |
Instance Method Details
#base_path ⇒ Object
64 65 66 |
# File 'lib/rory/controller.rb', line 64 def base_path @request.script_name end |
#default_renderer_options ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/rory/controller.rb', line 68 def { :layout => layout, :locals => locals, :app => @app, :base_path => base_path } end |
#expose(hsh) ⇒ Object
48 49 50 |
# File 'lib/rory/controller.rb', line 48 def expose(hsh) locals.merge!(hsh) end |
#layout ⇒ Object
60 61 62 |
# File 'lib/rory/controller.rb', line 60 def layout nil end |
#params ⇒ Object
52 53 54 |
# File 'lib/rory/controller.rb', line 52 def params @converted_params ||= Rory::HashWithDubiousSemantics.new(@params) end |
#present ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rory/controller.rb', line 91 def present # Call all before and after filters, and if a method exists on the # controller for the requested action, call it in between. call_filtered_action(@route.action.to_sym) if @response # that method may have resulted in a response already being generated # (such as a redirect, or 404, or other non-HTML response). if so, # just return that response. @response else # even if there wasn't a full response generated, we might already have # a @body, if render was explicitly called to render an alternate # template, or if @body was explicitly assigned for some other reason. # don't render the default template, in that case. @body ||= render(route_template) [200, {'Content-type' => 'text/html', 'charset' => 'UTF-8'}, [@body]] end end |
#redirect(path) ⇒ Object
83 84 85 |
# File 'lib/rory/controller.rb', line 83 def redirect(path) @response = @dispatcher.redirect(path) end |
#render(template_name, opts = {}) ⇒ Object
77 78 79 80 81 |
# File 'lib/rory/controller.rb', line 77 def render(template_name, opts = {}) opts = .merge(opts) renderer = Rory::Renderer.new(template_name, opts) @body = renderer.render end |
#render_not_found ⇒ Object
87 88 89 |
# File 'lib/rory/controller.rb', line 87 def render_not_found @response = @dispatcher.render_not_found end |
#route_template ⇒ Object
56 57 58 |
# File 'lib/rory/controller.rb', line 56 def route_template "#{@route.controller}/#{@route.action}" end |