UtopiaSourceUtopiaControllerActions

module Actions

Actions let you match path patterns in your controller and execute code. In your controller.rb simply add:

prepend Actions

If you are adding multiple things, like rewriting, they should come earlier in the chain, e.g:

prepend Rewrite, Actions

A simple CRUD controller might look like:

prepend Actions

on "index" do
	@users = User.all
end

on "new" do |request|
	@user = User.new
	
	if request.post?
		@user.update_attributes(request.params["user"])
		
		redirect! "index"
	end
end

on "edit" do |request|
	@user = User.find(request.params["id"])
	
	if request.post?
		@user.update_attributes(request.params["user"])
		
		redirect! "index"
	end
end

on "delete" do |request|
	User.find(request.params["id"]).destroy
	
	redirect! "index"
end

Path Matching

Path matching works from right to left, and '**' is a greedy operator. Controllers are invoked with a path relative to the controller's URI_PATH, so all lookups are relative to the controller.

"*"
Match a single path element
"**"
Match all remaining path elements
String
Match a named path component, e.g. "edit".
Symbol
Equivalent to ["**", symbol.to_s], e.g. :logout.

Otherwise Matching

If no action was matched, it is sometimes useful to perform some specific behaviour. You can specify this by using the otherwise handler:

otherwise do |request, path|
	fail! :teapot
end

If you are doing this to perform some kind of rewriting, it may be preferable to use the Rewrite controller layer.

Example.

on '*' do |request, path|
	succeed! content: 'Hello World'
end

Signature

Nested

Definitions

def process!(request, path)

Invoke all matching actions. If no actions match, will call otherwise. If no action gives a response, the request is passed to super.

Implementation

def process!(request, path)
	catch_response do
		self.class.dispatch(self, request, path)
	end || super
end