Sven Fuchs
2009-Mar-22 23:37 UTC
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
José Valim
2009-Mar-23 07:53 UTC
Re: routing: pass the segment key to to_param if it wants so?
This sounds good, gives more flexibility into object routes. :) On Mar 23, 12:37 am, Sven Fuchs <svenfu...@artweb-design.de> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Clemens Kofler
2009-Mar-23 10:12 UTC
Re: routing: pass the segment key to to_param if it wants so?
I like that. It would make the handling of permalinks much more transparent. On Mar 23, 12:37 am, Sven Fuchs <svenfu...@artweb-design.de> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---