grosser / restful_catch_all_route

One rule for complete restful routing, no helpers, no worries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

map.resources :user
map.resources :products, :member => {:goto => :get, :bar => :get}
map.resources :ads, :collection => {:search => :get, :preview => :get}
map.resources :lists, :member => {:something => :post}
map.resources :toys, :collection => {:foo => :put}
map.resources :sounds
...
map.restful_catch_all_route

(For Rails 2 and 3)

  • REST like normal (/users <-> index+create, /users/1 <-> show+update+destroy, /users/1/edit, /users/new )
  • collection and members map automatically (/users/search, /users/1/add)
  • _url / _path / _hash helper free global namespace
  • fallback to map.resources for e.g. nested resources
  • restful catch all route example app

Install

script/plugins install git://github.com/grosser/restful_catch_all_route.git

# config/routes.rb
map.restful_catch_all_route

namespace(:admin) do |admin|
  admin.restful_catch_all_route  
end

# if you need REST-less fallback urls, they must be placed after restful catch all
# map.connect ':controller/:action/:id'

Usage

# like normal:
url_for(@user) <-> show
url_for(User) <-> index
url_for([:admin, @user]) <-> with namespace
...
form_for @user
link_to 'hey', @user
polymorphic_url(@user)

# instead of edit_user_path(user):
"/users/edit/#{user.id}"
url_for(user)+'/edit'
polymorphic_url(user, :action => :edit)
:controller => :users, :action => :edit, :id => user.id

Id formats

By default it accepts ids that are pure numbers, or contain a '-'.
You can overwrite it by passing :id => /my_rex/

  • /my_controller/1/edit - works
  • /my_controller/1-fancy-title/edit - works
  • /my_controller/fancy-title/edit - works
  • /my_controller/fancy_title/edit - works NOT

Performance

Rails 2.3.5 with 50 resources 100_000 times

RecognitionGenerationRAMHelpers
catch all9.1s6.6200+ (1
resources68.2s9.5+9MB0
(1 `50 * (xxx_path + xxxs_path + new_xxx_path + edit_xxx_path + custom members)`

Author

Michael Grosser
grosser.michael@gmail.com
Hereby placed under public domain, do what you want, just do not hold me accountable...

About

One rule for complete restful routing, no helpers, no worries.


Languages

Language:Ruby 100.0%