routing: pass the segment key to to_param if it wants so?

I wondered why routing does not pass the current route segment key to
to_param if to_param accepts a parameter.

This would allow to have a route like

   map.article "articles/:permalink"

and then pass the named url generation helper an article like so:

   article_path(article)
   # => "articles/the-permalink"

when the Article class defines to_param like so:

   def to_param(key)
     respond_to?(key) ? send(key).to_s : super()
   end

or something similar ...

This would make to_param somewhat more context sensitive in that the
user can define to some extend in the route what kind of param the
route expects to get from the model. It also makes it easier to define
higher level url helpers without baking too much knowledge about the
routes into them.

This could be done by testing method(:to_param).arity in routing so it
does not change any behaviour except when the user explicitely defines
to_param with a parameter. Something like:

   module Routing
     # to be used in 4 places in routing that call to_param
     def self.to_param(object, key)
       object.method(:to_param).arity == 0 ? object.to_param :
object.to_param(key)
     end
   end

I'd look into a patch but thought it makes sense to ask here first.

This sounds good, gives more flexibility into object routes. :slight_smile:

I like that. It would make the handling of permalinks much more
transparent.