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: __) 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.
Filters
A filter method can be run before or after the controller code is executed. It can be used to prepare data, restrict access or format the generated content for example. See: before_filter and after_filter
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_header(field = nil, value = nil) ⇒ Object
Add additional http header for the response No validation made.
-
#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.
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/cuca/controller.rb', line 337 def initialize(*args) @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,
114 115 116 |
# File 'lib/cuca/controller.rb', line 114 def cancel_execution @cancel_execution end |
Class Method Details
.after_filter(method, priority = 50) ⇒ Object
196 197 198 |
# File 'lib/cuca/controller.rb', line 196 def self.after_filter(method, priority = 50) define_filter_method(:def_after_filter, method, priority) end |
.before_filter(method, priority = 50) ⇒ Object
173 174 175 |
# File 'lib/cuca/controller.rb', line 173 def self.before_filter(method, priority = 50) define_filter_method(:def_before_filter, method, priority) end |
.layout(name) ⇒ Object
146 147 148 |
# File 'lib/cuca/controller.rb', line 146 def self.layout(name) define_attr_method(:def_layout_name, name) end |
.priority_after_filter(method) ⇒ Object
188 189 190 |
# File 'lib/cuca/controller.rb', line 188 def self.priority_after_filter(method) define_filter_method(:def_priority_after_filter, method) end |
.priority_before_filter(method) ⇒ Object
180 181 182 |
# File 'lib/cuca/controller.rb', line 180 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
324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/cuca/controller.rb', line 324 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.respond_to?(method_name.intern) rescue BreakControllerException => e handle_exception(e) end end |
#action_name ⇒ Object
301 302 303 |
# File 'lib/cuca/controller.rb', line 301 def action_name $app.urlmap.action end |
#handle_exception(e) ⇒ Object
this piece of code will handle a thrown exception BreakControllerException
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/cuca/controller.rb', line 264 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 if e.flags.has_key?(:cancel_execution) then @cancel_execution = true end end |
#http_header(field = nil, value = nil) ⇒ Object
Add additional http header for the response No validation made
132 133 134 135 136 |
# File 'lib/cuca/controller.rb', line 132 def http_header(field=nil, value=nil) @_http_header ||= {} @_http_header[field] = value if field @_http_header end |
#http_status(hs = nil) ⇒ Object
A ruby cgi status type ‘OK’, ‘NOT_FOUND’.…to be sent within the http header
125 126 127 128 |
# File 'lib/cuca/controller.rb', line 125 def http_status(hs=nil) @_http_status = hs unless hs.nil? @_http_status end |
#layout(name) ⇒ Object
define a layout for the current instance
151 152 153 154 |
# File 'lib/cuca/controller.rb', line 151 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’
119 120 121 122 |
# File 'lib/cuca/controller.rb', line 119 def mime_type(mt=nil) @_mime_type = mt unless mt.nil? @_mime_type end |
#output ⇒ Object
365 366 367 368 369 370 371 372 |
# File 'lib/cuca/controller.rb', line 365 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
240 241 242 243 244 245 246 247 |
# File 'lib/cuca/controller.rb', line 240 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
233 234 235 236 |
# File 'lib/cuca/controller.rb', line 233 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
258 259 260 |
# File 'lib/cuca/controller.rb', line 258 def stop(flags = {}) raise BreakControllerException.new(flags) end |