Advanced RESTful Rails
Advanced RESTful Rails
Ben Scofield
Constraints
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate.
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date.
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair some time declines,
By chance, or nature's changing course, untrimm'd;
But thy eternal summer shall not fade
...
app
controllers
helpers
models
views
config
environments
initializers
db
doc
lib
tasks
log
public
images
javascripts
stylesheets
script
performance
process
test
fixtures
functional
integration
unit
...
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/users
exists test/functional/
exists test/unit/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/user.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create db/migrate
create db/migrate/20080531002035_create_users.rb
create app/controllers/users_controller.rb
create test/functional/users_controller_test.rb
create app/helpers/users_helper.rb
route map.resources :users
REST
Audience Participation!
who is building restful applications?
212,000 Results
How do I handle ...
Difficult
even for the pros
class UsersController < ApplicationController
# ...
def activate
self.current_user = params[:activation_code].blank? ? false : # ...
if logged_in? && !current_user.active?
current_user.activate!
flash[:notice] = "Signup complete!"
end
redirect_back_or_default('/')
end
def suspend
@user.suspend!
redirect_to users_path
end
def unsuspend
@user.unsuspend!
redirect_to users_path
end
def destroy
@user.delete!
redirect_to users_path
end
def purge
@user.destroy
redirect_to users_path
Restful Authentication
end
end
What is REST?
Resources
hey-helen - flickr
Addressability
memestate - flickr
Representations
stevedave - flickr
Stateless*
http://www1.ncdc.noaa.gov/pub/data/images/usa-avhrr.gif
Audience Participation!
why care?
Process
tiptoe - flickr
Domain
ejpphoto - flickr
Modeled
kerim - flickr
?
Identify
resources
Select
methods to expose
Respect
the middleman
Simple
My Pull List
Releases
ActionController::Routing::Routes.draw do |map|
map.releases 'releases/:year/:month/:day',
:controller => 'items', :action => 'index'
end
# edit account
def edit; end
# update account
def update; end
end
Lists
ActionController::Routing::Routes.draw do |map|
map.resources :users
end
# login action
def create; end
# logout action
def destroy; end
end
Homepage seandreilinger - flickr
ActionController::Routing::Routes.draw do |map|
map.resource :homepage
map.root :homepage
end
# dashboard
def show; end
end
ActionController::Routing::Routes.draw do |map|
map.resources :instruments
end
# dashboard
def index
@instruments = current_user.instruments
end
end
Preview ashoe - flickr
ActionController::Routing::Routes.draw do |map|
map.resources :posts, :has_one => [:preview]
end
Inventory
Search Application Text
RESTful API
HR
etc.
RESTful API Staff Directory
Administration
ActionController::Routing::Routes.draw do |map|
map.namespace :admin do |admin|
admin.resources :invitations
admin.resources :emails
admin.resources :users
admin.resources :features
end
end
Audience Participation!
what gives you fits?
Rails, Specifically
<%= link_to 'Delete', record, :method => 'delete',
:confirm => 'Are you sure?' %>
Accessibility
ActionController::Routing::Routes.draw do |map|
map.users 'users', :controller => ‘users’, :action => ‘index’
map.users 'users', :controller => ‘users’, :action => ‘create’
end
Hand-Written Routes
ActionController::Routing::Routes.draw do |map|
map.resources :users
Default Routing
Collections wooandy - flickr
ActionController::Routing::Routes.draw do |map|
map.resources :records
end
# ...
end
Mixed
ActionController::Routing::Routes.draw do |map|
map.resource :record_list
map.resources :records
end
# ...
end
# ...
end
Separated
Audience Participation!
where’s rails bitten you?
Thanks!
ben scofield
[email protected]
http://www.viget.com/extend
http://www.culann.com