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.
-
.context ⇒ Module
Returns the current context.
-
.context=(context) ⇒ Object
Injects context in Controller class.
-
.get_route(action = nil, *params) ⇒ Object
Returns the route representing the parameters.
-
.helper(*helpers) ⇒ nil
Adds one or more helpers to the controller.
-
.inherited(klass) ⇒ Object
:@private.
-
.layout_settings ⇒ Hash
Returns the layout settings for the current controller.
-
.setting(key, val) ⇒ Object
Add a setting for the current controller class.
-
.settings ⇒ Racket::Settings::Controller
Returns the settings for the current controller class.
-
.template_setting(key, value, type = :common) ⇒ Object
Add a setting used by Tilt when rendering views/layouts.
-
.view_settings ⇒ Hash
Returns the view settings for the current controller.
Instance Method Summary collapse
-
#__run ⇒ String
Calls hooks, action and renderer.
-
#layout_settings ⇒ Object
Returns layout settings associated with the current controller.
-
#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 ⇒ Object
Returns settings associated with the current controller.
-
#view_settings ⇒ Object
Returns view settings associated with 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.
52 53 54 |
# File 'lib/racket/controller.rb', line 52 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.
61 62 63 |
# File 'lib/racket/controller.rb', line 61 def self.before(*methods, &blk) __register_hook(:before, methods, blk) if block_given? end |
.context ⇒ Module
Returns the current context.
68 69 70 |
# File 'lib/racket/controller.rb', line 68 def self.context Controller.instance_variable_get(:@context) end |
.context=(context) ⇒ Object
Injects context in Controller class. Context represents the current application state.
76 77 78 79 |
# File 'lib/racket/controller.rb', line 76 def self.context=(context) raise 'Context should only be set on Controller class' unless self == Controller @context = context end |
.get_route(action = nil, *params) ⇒ Object
Returns the route representing the parameters.
85 86 87 |
# File 'lib/racket/controller.rb', line 85 def self.get_route(action = nil, *params) context.get_route(self, action, params) 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.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/racket/controller.rb', line 99 def self.helper(*helpers) helper_modules = {} unless settings.fetch(:helpers) # No helpers has been loaded yet. Load the default helpers first. helper_modules.merge!( context.helper_cache.load_helpers(settings.fetch(:default_controller_helpers)) ) end # Load new helpers __load_helpers(helpers.map(&:to_sym), helper_modules) end |
.inherited(klass) ⇒ Object
:@private
112 113 114 |
# File 'lib/racket/controller.rb', line 112 def self.inherited(klass) settings.fetch(:last_added_controller).push(klass) end |
.layout_settings ⇒ Hash
Returns the layout settings for the current controller.
119 120 121 122 |
# File 'lib/racket/controller.rb', line 119 def self.layout_settings template_settings = settings.fetch(:template_settings) template_settings[:common].merge(template_settings[:layout]) end |
.setting(key, val) ⇒ Object
Add a setting for the current controller class
128 129 130 |
# File 'lib/racket/controller.rb', line 128 def self.setting(key, val) settings.store(key, val) end |
.settings ⇒ Racket::Settings::Controller
Returns the settings for the current controller class
135 136 137 |
# File 'lib/racket/controller.rb', line 135 def self.settings @settings ||= Racket::Settings::Controller.new(self) end |
.template_setting(key, value, type = :common) ⇒ Object
Add a setting used by Tilt when rendering views/layouts.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/racket/controller.rb', line 144 def self.template_setting(key, value, type = :common) # If controller has no template settings on its own, copy the template settings # from its "closest" parent (might even be application settings) # @todo - How about template options that are unmarshallable? settings.store( :template_settings, Marshal.load(Marshal.dump(settings.fetch(:template_settings))) ) unless settings.present?(:template_settings) # Fetch current settings (guaranteed to be in controller by now) template_settings = settings.fetch(:template_settings) # Update settings template_settings[type][key] = value settings.store(:template_settings, template_settings) end |
.view_settings ⇒ Hash
Returns the view settings for the current controller.
163 164 165 166 |
# File 'lib/racket/controller.rb', line 163 def self.view_settings template_settings = settings.fetch(:template_settings) template_settings[:common].merge(template_settings[:view]) end |
Instance Method Details
#__run ⇒ String
Calls hooks, action and renderer.
241 242 243 244 245 246 |
# File 'lib/racket/controller.rb', line 241 def __run __run_hook(:before) __run_action __run_hook(:after) self.class.context.view_manager.render(self) end |
#layout_settings ⇒ Object
Returns layout settings associated with the current controller
183 184 185 |
# File 'lib/racket/controller.rb', line 183 def layout_settings self.class.layout_settings end |
#redirect(target, status = 302) ⇒ Object
Redirects the client. After hooks are run.
192 193 194 195 |
# File 'lib/racket/controller.rb', line 192 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.
202 203 204 205 |
# File 'lib/racket/controller.rb', line 202 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.
213 214 215 216 |
# File 'lib/racket/controller.rb', line 213 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.
224 225 226 |
# File 'lib/racket/controller.rb', line 224 def respond!(status = 200, headers = {}, body = '') throw :response, [status, headers, body] end |
#settings ⇒ Object
Returns settings associated with the current controller
229 230 231 |
# File 'lib/racket/controller.rb', line 229 def settings self.class.settings end |
#view_settings ⇒ Object
Returns view settings associated with the current controller
234 235 236 |
# File 'lib/racket/controller.rb', line 234 def view_settings self.class.view_settings end |