Last active
August 29, 2015 13:55
-
-
Save sycobuny/8776422 to your computer and use it in GitHub Desktop.
Just a simple file to build standard routes for a set of objects that will all behave in a similar way
This file contains hidden or 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 MyApp < Sinatra::Base | |
RestfulObjects = [ | |
SampleObject | |
] | |
require 'restful_sinatra.rb' | |
end |
This file contains hidden or 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
helpers do | |
def klass(klass) | |
Sequel::Model.send(:constantize, Sequel::Model.send(:camelize, klass)) | |
end | |
def wipe!(params) | |
%w(_method splat captures).each { |key| params.delete(key) } | |
end | |
end | |
MyApp::RestfulObjects.each do |obj| | |
get "/#{obj}s" do | |
klass = klass(obj) | |
@obj = obj | |
@list = klass.index.paginate(1, 15) | |
@listnav = true | |
@title = "#{klass}s" | |
haml :"index/#{obj}" | |
end | |
get "/#{obj}s/page/:pg" do | |
not_found unless params[:pg] =~ /^[0-9]+$/ | |
klass = klass(obj) | |
@obj = obj | |
@list = klass.index.paginate(params[:pg].to_i, 15) | |
@listnav = true | |
@title = "#{klass}s - Page #{params[:pg]}" | |
haml :"index/#{obj}" | |
end | |
get "/#{obj}/new" do | |
klass = klass(obj) | |
@object = klass.new | |
@title = "New #{klass}" | |
@root_link = "#{obj}s" | |
@rest_meth = :post | |
haml :"form/#{obj}" | |
end | |
post "/#{obj}/new" do | |
wipe!(params) | |
klass = klass(obj) | |
object = klass.new(params) | |
object.save | |
redirect "/#{obj}/#{object.id}" | |
end | |
get "/#{obj}/:id/edit" do | |
klass = klass(obj) | |
@object = klass[params[:id].to_i] | |
not_found unless @object | |
@title = "Edit #{klass}" | |
@subtitle = @object.to_s | |
@root_link = "#{obj}s" | |
@rest_meth = :put | |
haml :"form/#{obj}" | |
end | |
put "/#{obj}/:id/edit" do | |
wipe!(params) | |
klass = klass(obj) | |
object = klass[params.delete('id').to_i] | |
not_found unless object | |
object.update(params) | |
redirect "/#{obj}/#{object.id}" | |
end | |
get "/#{obj}/:id" do | |
klass = klass(obj) | |
@object = klass[params[:id].to_i] | |
not_found unless @object | |
@title = klass.to_s | |
@subtitle = @object.to_s | |
@root_link = "#{obj}s" | |
haml :"show/#{obj}" | |
end | |
end |
This file contains hidden or 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
%form{:method => :post, :action => ENV['REQUEST_PATH']} | |
%input{:type => :hidden, :name => :_method, :value => @rest_meth} | |
%label{:for => :name} Name | |
%input{:name => :name, :value => @object.name} | |
%br | |
%label{:for => :some_param} Some Param | |
%input{:name => :some_param, :value => @object.some_param} | |
%br | |
%label.required{:for => :some_other_param} Some Other Param | |
%input{:name => :some_other_param, :value => @object.some_other_param} | |
%br | |
%label.required{:for => :some_date} Some Date | |
%input{:type => :date, :name => :some_date, :value => @object.some_date} | |
%hr | |
%input{:type => :submit, :value => 'Save'} |
This file contains hidden or 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 SampleObject < Sequel::Model | |
Sequel.extension :pagination | |
# default view orders by :some_param | |
def self.index | |
dataset.order :some_param | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment