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.

polymorphic routes should handle collections through Model Class

#1089

The rails 2.1 PolymorphicRoutes module should handle collection's url through Model Class :

In actionpack/test/controller/polymorphic_routes_test.rb :

def test_with_a_class
  expects(:articles_url).with()
  polymorphic_url(Article)
end

def test_with_a_class_in_an_array
  expects(:articles_url).with()
  polymorphic_url([Article])
end

def test_with_a_class_and_a_namespace
  expects(:admin_articles_url).with()
  polymorphic_url([:admin, Article])
end

in module ActionController module PolymorphicRoutes

 ...
   def polymorphic_url(record_or_hash_or_array, options = {})
   ...
   inflection =
   ...
   # adding
   when record.is_a?(Class)
      args.pop
      :plural
   # end adding => this allow a Class to be mapped to the collection
   ...
   end

Reported by mamooth · September 22nd, 2008 @ 11:28 AM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • Tag changed from actionpack, polymorphic, routes to 2.1, actionpack, polymorphic, routes

    Could you please upload a patch using git-format-patch ?

    Thanks.

    December 21st, 2008 @ 04:15 PM

  2. Pratik
  3. Yehuda Katz (wycats)
    Yehuda Katz (wycats)

    @mamooth is there any chance you could provide tests that represent the desired output, not just the fact that articles_url will be called. This will help us ensure that your desired behavior does not break through any upcoming refactoring.

    December 24th, 2008 @ 08:15 AM

  4. Pratik
    Pratik
    • State changed from new to incomplete

    Will reopen when there's a patch.

    Thanks.

    February 24th, 2009 @ 04:12 PM

  5. Niklas Holmgren
    Niklas Holmgren
    • Tag changed from 2.1, actionpack, polymorphic, routes to 2.1, actionpack, patch, polymorphic, routes

    Created a patch complete with tests. Think I got all possible cases nailed.

    May 8th, 2009 @ 09:42 PM

  6. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from incomplete to open

    August 6th, 2009 @ 02:32 PM

  7. José Valim
    José Valim
    • Tag changed from 2.1, actionpack, patch, polymorphic, routes to 2.1, actionpack, bugmash, patch, polymorphic, routes

    August 8th, 2009 @ 01:52 PM

  8. Dan Pickett
    Dan Pickett

    +1 verified - I've attached an updated patch for 2-3 stable

    We should probably document this a little more.

    Also it could lead to some misleading behavior as it relates to STI models. Overall, though it could be very handy. Good idea

    August 8th, 2009 @ 03:49 PM

  9. Dan Pickett
    Dan Pickett

    trying to upload the patch for some reason it didn't take the first time

    August 8th, 2009 @ 04:03 PM

  10. Dan Pickett
  11. Dan Pickett
    Dan Pickett

    attached a patch against master per Lifo's request

    August 8th, 2009 @ 04:45 PM

  12. Yehuda Katz (wycats)
    Yehuda Katz (wycats)

    "Also it could lead to some misleading behavior as it relates to STI models." Do you know of any existing behavior this could break? I'm a bit wary of handy features that break existing assumptions. I'm posting to the list for more feedback.

    August 8th, 2009 @ 04:50 PM

  13. José Valim
    José Valim

    @Yehuda,

    He might be referring that the model SuperProject should have "/projects" as url, instead of "/super_projects" (which would be the default). Anyway, this problem already appears today and I already saw a ticket around here to solve the issue (although I don't think the solution should rely on polymorphic_url).

    Thinking out loud here, maybe we should add to ActiveModel::Naming something like route?

    August 8th, 2009 @ 04:57 PM

  14. Repository
    Repository
    • State changed from open to resolved

    August 8th, 2009 @ 05:00 PM

  15. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from resolved to open

    José: It's hard to add to naming, because the whole issue is that you can't calculate it (you have to try the subclass first and then the superclass). Maybe you're saying to cache it on naming for a given app? Again, this could get complicated in the case of multiple apps in the same process (not something you can do today, but worth keeping in mind). I think it makes more sense to cache on the router instance, which will always have one per application.

    August 8th, 2009 @ 05:01 PM

  16. José Valim
    José Valim

    @Yehuda, agreed. I also proposed a router based solution on #1231 polymorphic_url gives incorrect url for :has_one nested resource, which is also about polymorphic_url.

    August 8th, 2009 @ 05:04 PM

  17. Pratik
    Pratik
    • State changed from open to resolved

    Feel free to reopen AFTER there are any known issues. STI problem is a separate issue.

    August 8th, 2009 @ 05:19 PM

  18. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.1, actionpack, bugmash, patch, polymorphic, routes to 2.1, actionpack, patch, polymorphic, routes
    • Milestone cleared.

    August 8th, 2009 @ 11:07 PM