Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add optional :format argument to named routes

#1359

The discussion in #1215 Add :only/:except options to map.resources has investigated ways to reduce the number of memory-hungry routes generated by map.resources.

Several people have suggested that we could abandon the formatted_ named routes altogether and replace them with a :format parameter to the regular named routes. This seems like a more natural implementation and will avoid cluttering the route set for people who rarely or never use these routes.

Related links oourtesy of aaronbatalion:

Can we get a patch together?

Reported by Tom Stuart · November 12th, 2008 @ 11:12 AM

State: committed
Milestone: none
Assigned to: Michael Koziarski Michael Koziarski
Importance: Low

Activity

  1. DHH
    DHH

    I like this. Seems that http://gist.github.com/23712 has the pointers for making this so. Just needs to be wrapped up in a real patch with tests.

    November 12th, 2008 @ 11:55 AM

  2. Michael Koziarski
    Michael Koziarski

    At the same time it would be great to continue jeremy's work to cut down on the memory usage from the generated recognition code.

    November 12th, 2008 @ 12:01 PM

  3. aaronbatalion
    aaronbatalion

    I was working on a real patch with tests already. Will update soon. There are a couple known bugs in the above gist.

    November 12th, 2008 @ 12:08 PM

  4. aaronbatalion
    aaronbatalion
    • Tag changed from actionpack, options, resources to actionpack, options, patch, resources

    Attached is the patch for optional .:format in routes, which decreases the number of routes by 50%, saving up to 100M of RAM on larger rails apps.

    Notes: Found one side effect when PageCachingTest.

    In RouteSet#routes_for_controller_and_action_and_keys, routes are sorted by significant keys, subtracting the keys that are passed in. Therefore, are UrlRewriter.rewrite(:controller => "foo", :format => nil) and UrlRewriter.rewrite(:controller => "foo") are not the same.

    I've added a test to UrlRewriterTest, and modified the RouteSet#routes_for_controller_and_action_and_keys to remove pairs with nil values, and left the PageCachingTest alone.

    November 13th, 2008 @ 02:08 AM

  5. Lourens Naudé
    Lourens Naudé

    Aaron,

    Attached is an updated diff, compatible with Tom's changes from http://github.com/rails/rails/co...

    All tests passing and just piped it through the test suite of a large app with a huge number of nested routes.

    All seems well.Great track of thought with this !

    • Lourens

    November 13th, 2008 @ 05:42 PM

  6. Michael Koziarski
    Michael Koziarski

    Really nice work so far guys, if we do this we need to think about a few things:

    generation optimisations

    They assume that all segments are mandatory. This changes that and will cause them to fail to kick in even when they should.

    Deprecating the formatted_... routes nicely

    the formatted routes need to warn you, and continue to work with positional arguments

    formatted_person_url(1, :xml)

    November 14th, 2008 @ 10:35 AM

  7. DHH
    DHH
    • Milestone cleared.

    I'd really like to see this make it into 2.3. The formatted_ stuff was a hack anyway. Would be great to get rid of it. Anyone working on this have some comments for koz's concerns?

    November 23rd, 2008 @ 01:17 PM

  8. aaronbatalion
    aaronbatalion

    Attached is a new patch that gets rid of :format's, and answers Koz's concerns.

    1) Included a deprecation warn for each use of formatted_url* methods. If someone would like to suggest the proper warning text, I'm sure it can be improved.

    2) Fixed the generation optimisations implementation to still work for the previously route.optimise?-able routes.

    3) Started to rip out all direct calls to formatted_*, but that might need another pass to completely deprecate it.

    November 24th, 2008 @ 07:41 AM

  9. aaronbatalion
  10. Repository
    Repository
    • State changed from new to committed

    (from [fef6c32afe2276dffa0347e25808a86e7a101af1]) Added optimal formatted routes to rails, deprecating the formatted_* methods, and reducing routes creation by 50% [#1359 Add optional :format argument to named routes state:committed]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

    November 26th, 2008 @ 09:57 AM

  11. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 14th, 2010 @ 08:22 AM

  12. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    October 15th, 2010 @ 07:15 AM