Class: Cuca::Controller
- Defined in:
- lib/cuca/controller.rb,
lib/cuca/session.rb
Overview
Cuca Controller
A controller handles the get/post request for your website and has the ability to generate an output directly making use of other Widgets and Layouts.
The Controller class itself behaves just like Cuca::Widget, but instead of overwriting the output method you must implement run or post or get or any combination.
Additionally you can also define a wrapping layout (using the layout function) and you can run code before and after a certain action is called using before/after_filter’s.
Naming
When you open a url like: your.website.com/users/list cuca will load the file /users/list.rb from your application path (usually app/users/list.rb). Within that file you must define one class derrived from Cuca::Controller with name FilenameController. The /users/list.rb example must therefor implement the ListController class.
Subcalls
Sometimes you might want to implement another URL that is logically joined with your current page (“Ajax”?). To do this you can implement a (get|post|run)_subcallname method within your controller. This one will get called if you open your.website.com/-controller-subcallname .
Routing / Pretty URL’s
Depending on your directory structure within the app folder you can define variable directory names that will hint you within the controller.
Example: File: app/user/__default_username/show.rb will map to user/johnrambo/show and define instance variable The magic URL prefix (default: _default) can be change within App::Config
Defining a Layouts
In most cases a layout is defined on the controller class definition with the layout instruction:
class IndexController
layout 'standard'
end
In some cases you want to render a different layout or disable it at all. For this you can call within your action the ‘layout’ instance method that will temporarily the instruct the controller to render another layout or no layout (if false).
Interrupting the program
If you want to stop your program you can call the ‘stop’ method. Stop take some arguments that allows you to redirect, display error or set a different layout.
Examples
Hello world (index.rb):
class IndexController < Cuca::Controller
def run
content << "Hello World"
end
end
Using filters and layouts and generate page using the Markaby generator (fancy.rb):
require 'cuca/generators/markaby'
class FancyController < Cuca::Controller
include Cuca::Generator::Markaby
layout 'fancy'
before_filter 'set_title'
def set_title
@page_title = "A Fancy Page"
end
def get
mab do
h1 { "Welcome to the Fancy Page called #{@page_title}" }
p { text "Welcome to my"
b { "paragraph" }
}
end
end
end
Instance Attribute Summary collapse
-
#cancel_execution ⇒ Object
readonly
this can be set by anyone,.
Class Method Summary collapse
- .after_filter(method, priority = 50) ⇒ Object
- .before_filter(method, priority = 50) ⇒ Object
- .layout(name) ⇒ Object
- .priority_after_filter(method) ⇒ Object
- .priority_before_filter(method) ⇒ Object
-
.use_session ⇒ Object
This will create filters that initialize the session before the action and close it afterwards.
Instance Method Summary collapse
- #_do(what, subcall = nil) ⇒ Object
- #action_name ⇒ Object
-
#handle_exception(e) ⇒ Object
this piece of code will handle a thrown exception BreakControllerException.
-
#http_status(hs = nil) ⇒ Object
A ruby cgi status type ‘OK’, ‘NOT_FOUND’.…to be sent within the http header.
-
#initialize(*args) ⇒ Controller
constructor
A new instance of Controller.
-
#layout(name) ⇒ Object
define a layout for the current instance.
-
#mime_type(mt = nil) ⇒ Object
Tells the app what to send to the browser ‘text/html’.
- #output ⇒ Object
- #run_after_filters ⇒ Object
- #run_before_filters ⇒ Object
- #ses_close_session ⇒ Object
- #ses_initialize_session ⇒ Object
-
#stop(flags = {}) ⇒ Object
this method will stop execution of the controller it’s usefull to break somewhere in the middle or to set a different layout flags can be :layout - Set a new layout, or ‘false’ for no layout :redirect - redirect to a different page :error - An error message (for application errors) :no_after_filters - Do not execute any after filters defined.
Methods inherited from Widget
#app, #cgi, #clear, clear_hints, #content, #content=, #controller, define_attr_method, #escape, #escapeHTML, #get_assigns, #hints, #log, #params, #query_parameters, #request_method, #request_parameters, run_attr_method, #session, #to_s, #unescape, #unescapeHTML
Constructor Details
#initialize(*args) ⇒ Controller
Returns a new instance of Controller.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/cuca/controller.rb', line 323 def initialize(*args) # set a global variable of the controller object. # Widgets can make use of this to call or modify the controller class. $controller_object = self @cancel_execution = false super(*args) # FIXME: to think about... get_assigns.each_pair do |k,v| instance_variable_set("@#{k}", v) end @_mime_type = Cuca::App.config['default_mime_type'] @_http_status = 'OK' end |
Instance Attribute Details
#cancel_execution ⇒ Object (readonly)
this can be set by anyone,
108 109 110 |
# File 'lib/cuca/controller.rb', line 108 def cancel_execution @cancel_execution end |
Class Method Details
.after_filter(method, priority = 50) ⇒ Object
184 185 186 |
# File 'lib/cuca/controller.rb', line 184 def self.after_filter(method, priority = 50) define_filter_method(:def_after_filter, method, priority) end |
.before_filter(method, priority = 50) ⇒ Object
161 162 163 |
# File 'lib/cuca/controller.rb', line 161 def self.before_filter(method, priority = 50) define_filter_method(:def_before_filter, method, priority) end |
.layout(name) ⇒ Object
134 135 136 |
# File 'lib/cuca/controller.rb', line 134 def self.layout(name) define_attr_method(:def_layout_name, name) end |
.priority_after_filter(method) ⇒ Object
176 177 178 |
# File 'lib/cuca/controller.rb', line 176 def self.priority_after_filter(method) define_filter_method(:def_priority_after_filter, method) end |
.priority_before_filter(method) ⇒ Object
168 169 170 |
# File 'lib/cuca/controller.rb', line 168 def self.priority_before_filter(method) define_filter_method(:def_priority_before_filter, method) end |
.use_session ⇒ Object
This will create filters that initialize the session before the action and close it afterwards.
149 150 151 152 |
# File 'lib/cuca/session.rb', line 149 def self.use_session priority_before_filter('ses_initialize_session') priority_after_filter('ses_close_session') end |
Instance Method Details
#_do(what, subcall = nil) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/cuca/controller.rb', line 308 def _do(what, subcall = nil) return if @cancel_execution method_name = what method_name = "#{method_name}_#{subcall}" if subcall begin self.send(method_name) if self.methods.include?(method_name) rescue BreakControllerException => e handle_exception(e) rescue ApplicationException => e http_status "SERVER_ERROR" end end |
#action_name ⇒ Object
285 286 287 |
# File 'lib/cuca/controller.rb', line 285 def action_name $app.urlmap.action end |
#handle_exception(e) ⇒ Object
this piece of code will handle a thrown exception BreakControllerException
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/cuca/controller.rb', line 252 def handle_exception(e) if e.flags.has_key?(:layout) then @_layout = e.flags[:layout] end if e.flags.has_key?(:no_after_filters) then @_stop_no_after_filters = true end if e.flags.has_key?(:redirect) then @_layout = false to = e.flags[:redirect] clear @_content = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><title>Redirecting...</title><meta http-equiv=\"REFRESH\" content=\"0;url=#{to}\"></HEAD></HTML>" @cancel_execution = true end if e.flags.has_key?(:error) then @_layout = false http_status "SERVER_ERROR" clear @error_message = e.flags[:error] @cancel_execution = true trace = '' if Cuca::App.config['display_errors'] then e.backtrace.each do |b| trace<< "<br/>#{b}" end end mab { html { body { h2 "Error"; text @error_message; br; text trace }}} end end |
#http_status(hs = nil) ⇒ Object
A ruby cgi status type ‘OK’, ‘NOT_FOUND’.…to be sent within the http header
119 120 121 122 |
# File 'lib/cuca/controller.rb', line 119 def http_status(hs=nil) @_http_status = hs unless hs.nil? @_http_status end |
#layout(name) ⇒ Object
define a layout for the current instance
139 140 141 142 |
# File 'lib/cuca/controller.rb', line 139 def layout(name) $stderr.puts "Overwriting Layout: #{self.class.def_layout.inspect} with #{name}" @_layout = name end |
#mime_type(mt = nil) ⇒ Object
Tells the app what to send to the browser ‘text/html’
113 114 115 116 |
# File 'lib/cuca/controller.rb', line 113 def mime_type(mt=nil) @_mime_type = mt unless mt.nil? @_mime_type end |
#output ⇒ Object
356 357 358 359 360 361 362 363 |
# File 'lib/cuca/controller.rb', line 356 def output layout_class = load_layout return content unless layout_class c = content.to_s a = get_assigns a[:content_for_layout] = c @_content = layout_class.new(:assigns=>a) end |
#run_after_filters ⇒ Object
228 229 230 231 232 233 234 235 |
# File 'lib/cuca/controller.rb', line 228 def run_after_filters run_filters(:def_after_filter, 'After Filters') \ unless @_stop_no_after_filters ce = @cancel_execution @cancel_execution = false run_filters(:def_priority_after_filter, 'Priority After Filters') @cancel_execution = ce end |
#run_before_filters ⇒ Object
221 222 223 224 |
# File 'lib/cuca/controller.rb', line 221 def run_before_filters run_filters(:def_priority_before_filter, 'Priority Before Filters') run_filters(:def_before_filter, 'Before Filters') end |
#ses_close_session ⇒ Object
157 158 159 160 |
# File 'lib/cuca/session.rb', line 157 def ses_close_session $session.close # $session = nil end |
#ses_initialize_session ⇒ Object
154 155 156 |
# File 'lib/cuca/session.rb', line 154 def ses_initialize_session $session = Session.new($app.cgi) end |
#stop(flags = {}) ⇒ Object
this method will stop execution of the controller it’s usefull to break somewhere in the middle or to set a different layout flags can be :layout - Set a new layout, or ‘false’ for no layout :redirect - redirect to a different page :error - An error message (for application errors) :no_after_filters - Do not execute any after filters defined
246 247 248 |
# File 'lib/cuca/controller.rb', line 246 def stop(flags = {}) raise BreakControllerException.new(flags) end |