Skip to content

Instantly share code, notes, and snippets.

@cararemixed
Created May 13, 2011 17:54
Show Gist options
  • Save cararemixed/970992 to your computer and use it in GitHub Desktop.
Save cararemixed/970992 to your computer and use it in GitHub Desktop.
Better routes
uri 'contacts', name: 'contacts' do
get 'contacts#index'
end
uri 'contacts/:id/addresses', name: 'contact_addresses' do
put 'contact_addresses#replace'
post 'contact_addresses#create'
delete 'contact_addresses#delete'
end
module URIMap
class URITemplate
def delete(handler)
@methods << [:delete, handler]
end
def draw(routes)
opts = @name ? {} : {as: @name}
@methods.each do |(method, handler)|
routes.send(method, opts.merge(@template => handler))
end
end
def get(handler)
@methods << [:get, handler]
end
def post(handler)
@methods << [:post, handler]
end
def put(handler)
@methods << [:put, handler]
end
private
def initialize(template, opts)
@template = template
@name = opts[:name]
@methods = []
end
end
def uri(pattern, opts = {}, &b)
template = URITemplate.new(pattern, opts = {})
template.instance_eval(&b)
template.draw(self)
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment