Class: Racket::Controller
- Inherits:
-
Object
- Object
- Racket::Controller
- Defined in:
- lib/racket/controller.rb
Overview
Base controller class. Your controllers should inherit this class.
Class Method Summary collapse
-
.after(*methods, &blk) ⇒ nil
Adds a before hook to one or more actions.
-
.before(*methods, &blk) ⇒ nil
Adds an after hook to one or more actions.
-
.get_option(key) ⇒ Object
Returns an option for the current controller class or any of the controller classes it is inheriting from.
-
.inherited(klass) ⇒ Object
:nodoc:.
-
.set_option(key, value) ⇒ Object
Sets an option for the current controller class.
Instance Method Summary collapse
-
#controller_option(key) ⇒ Object
Returns an option from the current controller class.
-
#r(controller, action, *params) ⇒ String
Returns a route to an action within another controller.
-
#redirect(target, status = 302, run_after_hook = true) ⇒ Object
Redirects the client.
-
#render(action) ⇒ String
Renders an action.
-
#respond(status = 200, headers = {}, body = '', run_after_hook = false) ⇒ Object
Stop processing request and send a custom response.
-
#rs(action, *params) ⇒ String
Returns a route to an action within the current controller.
Class Method Details
.after(*methods, &blk) ⇒ nil
Adds a before hook to one or more actions. Actions should be given as a list of symbols. If no symbols are provided, all actions on the controller is affected.
49 50 51 |
# File 'lib/racket/controller.rb', line 49 def self.after(*methods, &blk) __register_hook(:after, methods, blk) if block_given? end |
.before(*methods, &blk) ⇒ nil
Adds an after hook to one or more actions. Actions should be given as a list of symbols. If no symbols are provided, all actions on the controller is affected.
58 59 60 |
# File 'lib/racket/controller.rb', line 58 def self.before(*methods, &blk) __register_hook(:before, methods, blk) if block_given? end |
.get_option(key) ⇒ Object
Returns an option for the current controller class or any of the controller classes it is inheriting from.
72 73 74 75 76 77 78 |
# File 'lib/racket/controller.rb', line 72 def self.get_option(key) @options ||= {} return @options[key] if @options.key?(key) # We are running out of controller options, do one final lookup in Application.options return Application..fetch(key, nil) if superclass == Controller superclass.get_option(key) end |
.inherited(klass) ⇒ Object
:nodoc:
63 64 65 |
# File 'lib/racket/controller.rb', line 63 def self.inherited(klass) Application.[:last_added_controller].push(klass) end |
.set_option(key, value) ⇒ Object
Sets an option for the current controller class.
84 85 86 87 |
# File 'lib/racket/controller.rb', line 84 def self.set_option(key, value) @options ||= {} @options[key] = value end |
Instance Method Details
#controller_option(key) ⇒ Object
Returns an option from the current controller class.
93 94 95 |
# File 'lib/racket/controller.rb', line 93 def controller_option(key) self.class.get_option(key) end |
#r(controller, action, *params) ⇒ String
Returns a route to an action within another controller.
112 113 114 |
# File 'lib/racket/controller.rb', line 112 def r(controller, action, *params) Application.get_route(controller, action, params) end |
#redirect(target, status = 302, run_after_hook = true) ⇒ Object
Redirects the client. After hooks are
122 123 124 125 |
# File 'lib/racket/controller.rb', line 122 def redirect(target, status = 302, run_after_hook = true) response.redirect(target, status) respond(response.status, response.headers, '', run_after_hook); end |
#render(action) ⇒ String
Renders an action.
144 145 146 147 |
# File 'lib/racket/controller.rb', line 144 def render(action) __execute(action) Application.view_cache.render(self) end |
#respond(status = 200, headers = {}, body = '', run_after_hook = false) ⇒ Object
Stop processing request and send a custom response. After calling this method, no further processing of the request is done unless run_after_hook is set to true. If run_after_hook is set to true, any after hook associated with the action (but no other code) will be run.
135 136 137 138 |
# File 'lib/racket/controller.rb', line 135 def respond(status = 200, headers = {}, body = '', run_after_hook = false) __run_hook(:after, racket.action) if run_after_hook throw :response, [status, headers, body] end |
#rs(action, *params) ⇒ String
Returns a route to an action within the current controller.
102 103 104 |
# File 'lib/racket/controller.rb', line 102 def rs(action, *params) Application.get_route(self.class, action, params) end |