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 has function to do most function, like redirect or setting a different layout or raising an error message.
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 :redirect - redirect to a different page :error - An error message (for application errors).
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.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/cuca/controller.rb', line 318 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,
109 110 111 |
# File 'lib/cuca/controller.rb', line 109 def cancel_execution @cancel_execution end |
Class Method Details
.after_filter(method, priority = 50) ⇒ Object
185 186 187 |
# File 'lib/cuca/controller.rb', line 185 def self.after_filter(method, priority = 50) define_filter_method(:def_after_filter, method, priority) end |
.before_filter(method, priority = 50) ⇒ Object
162 163 164 |
# File 'lib/cuca/controller.rb', line 162 def self.before_filter(method, priority = 50) define_filter_method(:def_before_filter, method, priority) end |
.layout(name) ⇒ Object
135 136 137 |
# File 'lib/cuca/controller.rb', line 135 def self.layout(name) define_attr_method(:def_layout_name, name) end |
.priority_after_filter(method) ⇒ Object
177 178 179 |
# File 'lib/cuca/controller.rb', line 177 def self.priority_after_filter(method) define_filter_method(:def_priority_after_filter, method) end |
.priority_before_filter(method) ⇒ Object
169 170 171 |
# File 'lib/cuca/controller.rb', line 169 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.
147 148 149 150 |
# File 'lib/cuca/session.rb', line 147 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
303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/cuca/controller.rb', line 303 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
280 281 282 |
# File 'lib/cuca/controller.rb', line 280 def action_name $app.urlmap.action end |
#handle_exception(e) ⇒ Object
this piece of code will handle a thrown exception BreakControllerException
251 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 |
# File 'lib/cuca/controller.rb', line 251 def handle_exception(e) if e.flags.has_key?(:layout) then @_layout = e.flags[:layout] 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
120 121 122 123 |
# File 'lib/cuca/controller.rb', line 120 def http_status(hs=nil) @_http_status = hs unless hs.nil? @_http_status end |
#layout(name) ⇒ Object
define a layout for the current instance
140 141 142 143 |
# File 'lib/cuca/controller.rb', line 140 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’
114 115 116 117 |
# File 'lib/cuca/controller.rb', line 114 def mime_type(mt=nil) @_mime_type = mt unless mt.nil? @_mime_type end |
#output ⇒ Object
351 352 353 354 355 356 357 358 |
# File 'lib/cuca/controller.rb', line 351 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
229 230 231 232 233 234 235 |
# File 'lib/cuca/controller.rb', line 229 def run_after_filters run_filters(:def_after_filter, '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
222 223 224 225 |
# File 'lib/cuca/controller.rb', line 222 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
155 156 157 158 |
# File 'lib/cuca/session.rb', line 155 def ses_close_session $session.close # $session = nil end |
#ses_initialize_session ⇒ Object
152 153 154 |
# File 'lib/cuca/session.rb', line 152 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 :redirect - redirect to a different page :error - An error message (for application errors)
245 246 247 |
# File 'lib/cuca/controller.rb', line 245 def stop(flags = {}) raise BreakControllerException.new(flags) end |