Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10818 (closed enhancement: fixed)

Opened 4 months ago

Last modified 4 months ago

[PATCH] Add an optional :alias key to route declarations

Reported by: bscofield Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: routing
Cc:

Description

A common situation (for me at least) is something like the following:

# in routes.rb
  # from restful_authentication, for instance
  map.resources :users
  map.resource  :session 
  
  # ...
  map.login :controller => 'sessions', :action => 'new', :conditions => {:method => :get}
  map.logout :controller => 'sessions', :action => 'destroy', :conditions => {:method => :delete}
  map.register :controller => 'users', :action => 'new', :conditions => {:method => :get}
  # ...
  
  map.root :controller => 'sessions', :action => 'new'

I don't know how many other people declare duplicated named routes to keep their views more readable (which is more comprehensible - <%= link_to 'Login', new_session_path %> or <%= link_to 'Login', login_path %>?), but I think that the map.root case is probably common.

That being the case, this patch allows you to specify an :alias key when declaring a route. When the declaration is processed, the system will look for an already-declared named route corresponding to the :alias value, and will automatically reuse the options declared for that route. For example, the snippet of routes.rb above would become:

# in routes.rb
  # from restful_authentication, for instance
  map.resources :users
  map.resource  :session 
  
  # ...
  map.login :alias => :new_session
  map.logout :alias => :session, :conditions => {:method => :delete}
  map.register :alias => :new_user
  # ...
  
  map.root :new_session

The patch includes the code to make this work, an update to the documentation describing the behavior, and the appropriate test - and none of this breaks the existing tests.

Attachments

add_alias_key_to_route_options.patch (3.2 kB) - added by bscofield on 01/15/08 23:02:00.
simplified_route_aliasing.patch (2.7 kB) - added by bscofield on 01/26/08 05:39:25.
patch for map.root only, with docs, code, and test

Change History

01/15/08 23:02:00 changed by bscofield

  • attachment add_alias_key_to_route_options.patch added.

01/26/08 05:38:32 changed by bscofield

Per the discussion in IRC, I'm uploading a version of this that 1) limits the effect to map.root (the most common use case), and 2) simplifies the interface. The new version allows this:

# in routes.rb
map.resource :session
map.root :new_session

Patch includes docs, test, and code.

01/26/08 05:39:25 changed by bscofield

  • attachment simplified_route_aliasing.patch added.

patch for map.root only, with docs, code, and test

01/26/08 08:41:23 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [8738]) Make it simpler to make the root route an alias for another route. Closes #10818 [bscofield]