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.
-
.helper(*helpers) ⇒ nil
Adds one or more helpers to the controller.
-
.inherited(klass) ⇒ Object
:nodoc:.
-
.setting(key, value) ⇒ nil
Creates/updates a setting for the current controller class.
-
.settings ⇒ Racket::Settings::Controller
Returns the settings associated with the current controller class.
Instance Method Summary collapse
-
#__run ⇒ String
Calls hooks, action and renderer.
-
#redirect(target, status = 302) ⇒ Object
Redirects the client.
-
#redirect!(target, status = 302) ⇒ Object
Redirects the client.
-
#respond(status = 200, headers = {}, body = '') ⇒ Object
Stop processing request and send a custom response.
-
#respond!(status = 200, headers = {}, body = '') ⇒ Object
Stop processing request and send a custom response.
-
#settings ⇒ Racket::Settings::Controller
Returns the settings for a controller instance.
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.
63 64 65 |
# File 'lib/racket/controller.rb', line 63 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.
72 73 74 |
# File 'lib/racket/controller.rb', line 72 def self.before(*methods, &blk) __register_hook(:before, methods, blk) if block_given? end |
.helper(*helpers) ⇒ nil
Adds one or more helpers to the controller. All controllers get some default helpers (:routing and :view by default), but if you have your own helpers you want to load this is the preferred method.
By default Racket will look for your helpers in the helpers directory, but you can specify another location by changing the helper_dir setting.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/racket/controller.rb', line 86 def self.helper(*helpers) helper_modules = {} unless settings.fetch(:helpers) # No helpers has been loaded yet. Load the default helpers first. helper_modules.merge!( __helper_cache.load_helpers(Application.settings.default_controller_helpers) ) end # Load new helpers __load_helpers(helpers.map(&:to_sym), helper_modules) end |
.inherited(klass) ⇒ Object
:nodoc:
111 112 113 |
# File 'lib/racket/controller.rb', line 111 def self.inherited(klass) Application.settings.fetch(:last_added_controller).push(klass) end |
.setting(key, value) ⇒ nil
Creates/updates a setting for the current controller class.
127 128 129 |
# File 'lib/racket/controller.rb', line 127 def self.setting(key, value) settings.store(key, value) end |
.settings ⇒ Racket::Settings::Controller
Returns the settings associated with the current controller class.
118 119 120 |
# File 'lib/racket/controller.rb', line 118 def self.settings @settings ||= Settings::Controller.new(self) end |
Instance Method Details
#__run ⇒ String
Calls hooks, action and renderer.
184 185 186 187 188 189 |
# File 'lib/racket/controller.rb', line 184 def __run __run_hook(:before) __run_action __run_hook(:after) Application.view_manager.render(self) end |
#redirect(target, status = 302) ⇒ Object
Redirects the client. After hooks are run.
145 146 147 148 |
# File 'lib/racket/controller.rb', line 145 def redirect(target, status = 302) response.redirect(target, status) respond(response.status, response.headers, '') end |
#redirect!(target, status = 302) ⇒ Object
Redirects the client. After hooks are NOT run.
155 156 157 158 |
# File 'lib/racket/controller.rb', line 155 def redirect!(target, status = 302) response.redirect(target, status) respond!(response.status, response.headers, '') end |
#respond(status = 200, headers = {}, body = '') ⇒ Object
Stop processing request and send a custom response. After calling this method, after hooks (but no rendering) will be run.
166 167 168 169 |
# File 'lib/racket/controller.rb', line 166 def respond(status = 200, headers = {}, body = '') __run_hook(:after) respond!(status, headers, body) end |
#respond!(status = 200, headers = {}, body = '') ⇒ Object
Stop processing request and send a custom response. After calling this method, no further processing of the request is done.
177 178 179 |
# File 'lib/racket/controller.rb', line 177 def respond!(status = 200, headers = {}, body = '') throw :response, [status, headers, body] end |
#settings ⇒ Racket::Settings::Controller
Returns the settings for a controller instance.
136 137 138 |
# File 'lib/racket/controller.rb', line 136 def settings self.class.settings end |