Skip to content

Instantly share code, notes, and snippets.

@roryokane
Forked from cararemixed/example.rb
Created June 7, 2011 13:17
Show Gist options
  • Save roryokane/1012220 to your computer and use it in GitHub Desktop.
Save roryokane/1012220 to your computer and use it in GitHub Desktop.
Better routes (just a fork, haven’t used personally) (doesn’t seem to work with Rails 3)
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
def uri(pattern, opts = {}, &b)
template = URITemplate.new(pattern, opts = {})
template.instance_eval(&b)
template.draw(self)
self
end
class URITemplate
def draw(routes)
opts = @name ? {} : {as: @name}
@methods.each do |(method, handler)|
routes.send(method, opts.merge(@template => handler))
end
end
METHOD_NAMES = %w(get post put delete)
METHOD_NAMES.each do |method_name|
define_method(method_name) do |handler|
add_method(method_name, handler)
end
end
private
def initialize(template, opts)
@template = template
@name = opts[:name]
@methods = []
end
def add_method(method_name, handler)
@methods << [method_name, handler]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment