Permalink
Browse files

update plugin, add example for namespaced routes

  • Loading branch information...
grosser committed Mar 21, 2010
1 parent eeedf8a commit 77997d1d193e9d91ede72ce27d063698480615aa
@@ -0,0 +1,9 @@
+class Admin::UsersController < ApplicationController
+ def show
+ @user = User.find(params[:id])
+ end
+
+ def edit
+ @user = User.find(params[:id])
+ end
+end
@@ -0,0 +1 @@
+Edit meee!!!
@@ -0,0 +1,9 @@
+Admin index is here!!!
+
+We got some models TOOO
+<br />
+<% User.all.each do |user| %>
+ <%= link_to user.name, [:admin, user] %>
+ <%= link_to 'edit', polymorphic_url([:admin, user], :action => :edit) %>
+ <br />
+<% end %>
@@ -0,0 +1 @@
+Hello <%= @user.name %>, im your admin!
@@ -10,4 +10,7 @@ This is <%= link_to 'index', User %>
<br />
If you like it you may also like:<br />
<%= link_to 'new', :controller => :users, :action => :new %> or
-<%= link_to 'search', :action => :search %>
+<%= link_to 'search', :action => :search %>
+
+<br /><br /><br />
+<small>Psssst heres the admin url <%=link_to 'admin', [:admin, User] %>
View
@@ -1,3 +1,13 @@
RestfulCatchAllRouteExample::Application.routes.draw do |map|
+ root :to => 'users#index'
+
+ map.namespace(:admin) do |admin|
+ admin.restful_catch_all_route
+# deep is nil, Rails 3 bug !?
+# namespace(:deep) do |deep|
+# deep.restful_catch_all_route
+# end
+ end
+
map.restful_catch_all_route
end
@@ -1,14 +1,20 @@
-One rule, no worries (Rails 2 + 3)
-
- - resources like normal (get:show, put:update, delete:destroy, post:create, edit, new, index, etc.)
- - no resources need to be added
- - no actions (aka collection/member) need to be added
- - no _url / _path / _hash helpers in global namespace
- - `form_for @user` / `link_to xxx, @user` / `polymorphic_url @user` like normal
- - you can always add normal resources for edge-cases (e.g. nesting)
+<del>map.resources :user</del><br />
+<del>map.resources :products, :member => {:goto => :get, :bar => :get}</del><br />
+<del>map.resources :ads, :collection => {:search => :get, :preview => :get}</del><br />
+<del>map.resources :lists, :member => {:something => :post}</del><br />
+<del>map.resources :toys, :collection => {:foo => :put}</del><br />
+<del>map.resources :sounds</del><br />
+...<br/>
+<b>map.restful_catch_all_route</b>
+
+(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](http://github.com/grosser/restful_catch_all_route_example)
-
Install
=======
@@ -17,6 +23,10 @@ Install
# config/routes.rb
map.restful_catch_all_route
+ namespace(:admin) do |admin|
+ map.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'
@@ -27,13 +37,20 @@ Usage
form_for @user
link_to 'hey', @user
polymorphic_url(@user)
+ polymorphic_url([:admin, @user])
# changed:
link_to 'foo', new_users_path
+ link_to 'edit foo', edit_user_path(user)
- # is now...
+ # is now one of these ...
link_to 'foo', '/users/new'
link_to 'foo', :controller => 'users', :action => 'new'
+ link_to 'foo', polymorphic_url(User, :action => :new)
+
+ link_to 'edit foo', "/users/edit/#{user.id}"
+ link_to 'edit foo', :controller => :users, :action => :edit, :id => user.id
+ link_to 'edit foo', polymorphic_url(user, :action => :edit)
### Id formats
@@ -24,24 +24,35 @@ def polymorphic_url_with_restful_fallback(*args, &block)
begin
polymorphic_url_without_restful_fallback(*args, &block)
rescue NoMethodError => error
- model = [*args.first] # called with [<User>] or <User>
- raise error if model.compact.size != 1 # something like [:admin, <User>], [nil,<User>], ..
- model = model.first
+ # prepare and cleanup models
+ models = [*args.first] # called with [<User>] or <User>
+ namespaces, models = models.partition{|x| x.is_a?(String) or x.is_a?(Symbol) }
+ raise error if models.size != 1
+ model = models.first
+ # prepare and cleanup options
options = (args.last.is_a?(Hash) ? args.last.dup : {})
options.delete(:routing_type)
+ # match action+controller+path/url from missing method
model_name = (model.is_a?(Class) ? model : model.class).class_name.underscore
+ raise error unless error.to_s =~ /\W((\w+?)_)?(#{model_name}|#{model_name.pluralize})_(path|url)\W/
+ options.reverse_merge!(:only_path => ($4 == 'path'))
- raise error unless error.to_s =~ /\W((.*?)_)?(#{model_name}|#{model_name.pluralize})_(path|url)\W/
+ # add namespace to controller
+ action = $2
+ namespaces.each{|n| action.sub!(/^#{n}(_|$)/,'')} # remove namespaces from action
+ action = nil if action.blank? # can be empty after namespace removal <-> '?action='
+ controller = ((namespaces+[nil]) * '/') + model_name.pluralize # add name/space/to/controller
+ options.reverse_merge!(:controller => controller, :action => action)
- options.reverse_merge!(:controller => model_name.pluralize, :action => $2)
- options.reverse_merge!(:only_path => ($4 == 'path'))
+ # add :id when we have a model
if not model.is_a?(Class) and model.respond_to?(:new_record?) and not model.new_record?
options.reverse_merge!(:id => model.to_param)
options[:action] ||= :show # or it would be users?id=1 instead of users/1
end
+ # generate the url or path
url_for(options)
end
end

0 comments on commit 77997d1

Please sign in to comment.