[OT] Looking for advice - broke polymorphic_url behavior

9 views
Skip to first unread message

moritz

unread,
Mar 31, 2009, 4:39:15 PM3/31/09
to CommunityEngine
I've subclassed the CE User type using single-table inheritance and it
all seemed to work fine.

Now I discovered that polymorphic_url (used in commentable_url in
BaseHelper) doesn't work well with it. Instead of user_photo_path for
example, it looks for myusertype_photo_path, which it can't find.

I'm not that familiar with the route management and I'm looking for
options to revive the behavior of comments w/o going all the way back
to one single user class. What are good options for it? Can I make
polymorphic_url believe that myusertype is a user and to just use
that? Other alternatives?

Thanks in advance.

sprite

unread,
Apr 1, 2009, 9:39:34 AM4/1/09
to CommunityEngine
This is probably not the best way to do it, but I ended up copying and
pasting all the routes for User for each of my subclassed user types.

moritz

unread,
Apr 1, 2009, 12:52:49 PM4/1/09
to CommunityEngine
Yes, I was hoping for something dryer.

moritz

unread,
Apr 2, 2009, 7:50:44 PM4/2/09
to CommunityEngine
The following stack trace

NoMethodError (undefined method `my_user_type_photo_url' for
#<UserNotifier:0x3cbd560>):
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
polymorphic_routes.rb:112:in `__send__'
/Library/Ruby/Gems/1.8/gems/actionpack-2.2.2/lib/action_controller/
polymorphic_routes.rb:112:in `polymorphic_url'
/app/helpers/base_helper.rb:8:in `commentable_url'
/vendor/plugins/community_engine/app/models/user_notifier.rb:37:in
`comment_notice'
/Library/Ruby/Gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/
base.rb:462:in `__send__'
/Library/Ruby/Gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/
base.rb:462:in `create!'
/Library/Ruby/Gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/
base.rb:455:in `initialize'
/Library/Ruby/Gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/
base.rb:392:in `new'
/Library/Ruby/Gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/
base.rb:392:in `method_missing'
/vendor/plugins/community_engine/app/models/comment.rb:109:in
`send_notifications'
/app/controllers/comments_controller.rb:156:in `create'

made me think that I could implement a method

def my_user_type_photo_url(user, options)
user_photo_url(user, options)
end

I tried it by adding it to base_helper.rb and it works for this case.

Is there a better place to handle this?

Is there a way to automate it, maybe?

How can it be extended beyond photos?

I'm still hoping for suggestions to improve the way this is handled.

Thanks in advance for your suggestions.

Bruno Bornsztein

unread,
Apr 7, 2009, 10:25:57 AM4/7/09
to communi...@googlegroups.com
Hmmm... basically you're talking about subclassing routes, or at least aliasing them (so that myusertype_photo_url is the same as user_photo_url). Looks like there's a ticket (http://dev.rubyonrails.org/ticket/10454) with some ideas on how to fix it. In particular, you could override the polymorphic_url method to check for a superclass before generating the route. 

moritz

unread,
Apr 7, 2009, 12:08:04 PM4/7/09
to CommunityEngine
Great! This is exactly what I was looking for. Thank you.

On Apr 7, 7:25 am, Bruno Bornsztein <bruno.bornszt...@gmail.com>
wrote:

Polar

unread,
Apr 8, 2009, 10:10:24 AM4/8/09
to CommunityEngine
I added functions like

def myusertype_photo_url(a,b,args={}); user_photo_url(a,b,args); end

to my version of the BaseController, and even the BaseHelper, hoping
for an Engines
mix in.

Funny thing, it works perfectly in the development environment, but
doesn't work on the
production system.

Is there a strange reason for this?

I do have access to any functions I "mix-in" to the BaseController and
BaseHelper, but polymorphic_url
doesn't seem to be picking this up in the production environment.

Any clues?

Thanks,
-Polar
Reply all
Reply to author
Forward
0 new messages