Created
January 30, 2013 11:33
-
-
Save jamiehodge/4672674 to your computer and use it in GitHub Desktop.
Header links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Catalogs < Controller | |
set :model, Catalog | |
set :fields, [:title, :description] | |
namespace '/' do | |
get do | |
catalogs = model.paginate(page, page_size) | |
etag catalogs.etag | |
last_modified catalogs.last_modified | |
link url('/'), rel: 'self', title: 'Catalog Index' | |
pagination_links(catalogs) | |
yajl :'catalogs/index', locals: {catalogs: catalogs} | |
end | |
post do | |
catalog.set_fields params, settings.fields | |
catalog.save | |
headers 'Location' => url(catalog.id) | |
204 | |
end | |
namespace ':id' do | |
before do | |
catalog or not_found | |
etag catalog.etag | |
last_modified catalog.last_modified | |
end | |
get do | |
link url(catalog.id), rel: 'self', title: catalog.title | |
link url('/'), rel: 'collection', title: 'Return to Index' | |
yajl :'catalogs/show' | |
end | |
put do | |
catalog.update_fields params, settings.fields, missing: :skip | |
headers 'Location' => url(catalog.id) | |
204 | |
end | |
delete do | |
catalog.destroy | |
204 | |
end | |
def catalog | |
@catalog ||= model[id] | |
end | |
end | |
def catalog | |
@catalog ||= model.new | |
end | |
error Sequel::ValidationFailed do | |
[ | |
400, | |
yajl(:error, locals: {title: 'validation error', message: catalog.errors.full_messages}) | |
] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment