This project is archived and is in readonly mode.
polymorphic_path not dry at all
-
gucki
Here's a little optimized version. Better performance and only invoke if a single record is passed to polymorphic_path. This improve backwards compatibility even more :)
unless record_or_hash_or_array.is_a?(Array) namespaces = record.class.name.split('::') if namespaces.size>1 data = args.first namespaces.pop namespaces.reverse.each do |namespace| data = data.send(namespace.downcase) args.unshift data end end end -
Andrew White
Have you tried overriding model_name in the member and job models? e.g:
class Team::Member < ActiveRecord::Base def self.model_name ActiveSupport::ModelName.new("Member") end endAfter doing this app.polymorphic_path([t, m]) should work.
-
José Valim
- State changed from new to invalid
Good solution! But, as you said, this is specific to your application so it does not make sense in Rails itself.
-
gucki
Thanks Andrew. But this is only a solution to the error but not for the problem that the whole thing is still not DRY.
Specifying a path with app.polymorphic_path([t, m]) seems not DRY to me. One should only have to specify app.polymorphic_path(m) as the parent (here team) could be resolved from the namespace, as my code above does.
-
Andrew White
Gucki, your solution will only work where the associations are given the default names. I'd argue that trying to tie knowledge of the model into the router is likely to end up making things worse. For example I use the model_name override method to have a generic controller for some STI models - your patch would break that by using the full class name and not the STI base class. Also I have STI models inside a collection module (e.g. Page::Components::MenuComponent) which would also break with your patch.