Skip to content

Instantly share code, notes, and snippets.

@maxime
Created December 14, 2008 16:31
Show Gist options
  • Save maxime/35720 to your computer and use it in GitHub Desktop.
Save maxime/35720 to your computer and use it in GitHub Desktop.
#
# In my router, I have:
#
resources :companies do
collection :do_something, :method => :post
end
#
# The match(request) method after compilation is:
#
def match(request)
cached_path = request.path.to_s
cached_method = request.method.to_s
if (/^\/companies(?:\/index)?(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1 = $1) || true)) && (cached_method == "get")
[0, {:controller => "companies", :format => (path1), :action => "index"}]
elsif (/^\/companies(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1 = $1) || true)) && (cached_method == "post")
[1, {:controller => "companies", :format => (path1), :action => "create"}]
elsif (/^\/companies\/new(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1 = $1) || true)) && (cached_method == "get")
[2, {:controller => "companies", :format => (path1), :action => "new"}]
elsif (/^\/companies\/do_something(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1 = $1) || true)) && (cached_method == "post")
[3, {:controller => "companies", :format => (path1), :action => "do_something"}]
elsif (/^\/companies\/([^\/.,;?]+)(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1, path2 = $1, $2) || true)) && (cached_method == "get")
[3, {:controller => "companies", :format => (path2), :action => "show", :id => (path1)}]
elsif (/^\/companies\/([^\/.,;?]+)\/edit(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1, path2 = $1, $2) || true)) && (cached_method == "get")
[4, {:controller => "companies", :format => (path2), :action => "edit", :id => (path1)}]
elsif (/^\/companies\/([^\/.,;?]+)\/delete(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1, path2 = $1, $2) || true)) && (cached_method == "get")
[5, {:controller => "companies", :format => (path2), :action => "delete", :id => (path1)}]
elsif (/^\/companies\/([^\/.,;?]+)(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1, path2 = $1, $2) || true)) && (cached_method == "put")
[6, {:controller => "companies", :format => (path2), :action => "update", :id => (path1)}]
elsif (/^\/companies\/([^\/.,;?]+)(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1, path2 = $1, $2) || true)) && (cached_method == "delete")
[7, {:controller => "companies", :format => (path2), :action => "destroy", :id => (path1)}]
elsif (/^\/login$/ =~ cached_path ) && (cached_method == "get")
[9, {:controller => "exceptions", :action => "unauthenticated"}]
elsif (/^\/login$/ =~ cached_path ) && (cached_method == "put")
[10, {:controller => "merb_auth_slice_password/sessions", :action => "update"}]
elsif (/^\/logout$/ =~ cached_path )
[11, {:controller => "merb_auth_slice_password/sessions", :action => "destroy"}]
elsif (/^\/([^\/.,;?]+)(?:\/([^\/.,;?]+)(?:\/([^\/.,;?]+))?)?(?:\.([^\/.,;?]+))?$/ =~ cached_path && ((path1, path2, path3, path4 = $1, $2, $3, $4) || true))
[12, {:controller => (path1), :format => (path4), :action => (path2 || "index"), :id => (path3)}]
else
[nil, {}]
end
end
#
# You might notice that 2 routes have the same index (3)
#
# in router.rb, method route_for:
# You have this line:
route = routes[index] if index
#
# So, if you go to /companies/1
# You will find that:
request.route
=> /companies/do_something(.:format)
# Isn't that weird?
#
# Shouldn't it be: /companies/:id(.:format) ?
#
# Maybe I shouldn't use request.route (private) but I thought I should still mention it
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment