-
-
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)
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
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 |
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
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