recommendable icon indicating copy to clipboard operation
recommendable copied to clipboard

Namespace conflict with model methods

Open gbuesing opened this issue 9 years ago • 4 comments

My User model recommends :places, but it already has an Active Record association for recommended_places, so I am forced to access Recommendable recommendations via:

user.send :recommended_for, Place

... which is a bit clunky. To avoid this, and other potential namespace collisions for existing model methods, I suggest supporting an optional recommendable_ prefix on Recommendable finder methods, e.g.:

user.recommendable_recommended_places

Would be easy to implement -- we'd just need to add an optional check in the method regexes for the namespace. Existing finder methods would still work -- you'd only need to prepend recommendable_ if you had a namespace issue.

gbuesing avatar Dec 14 '15 23:12 gbuesing

I have a similar but different name collision: some of my models already implement a recommendable? function that of course has a different meaning. You might want to look into how elasticsearch-rails handles potential name collisions and do something similar.

Nuru avatar Dec 17 '15 00:12 Nuru

@Nuru Thanks for that link; I like that idea a lot. I'll take a look at implementing something similar soon. In the meantime, I'm glad you found a workaround @gbuesing!

davidcelis avatar Dec 17 '15 01:12 davidcelis

@davidcelis There's also the issue that if a ratable class is within a module, e.g. Product::Song, then the generated methods are not callable directly because they contain a /. One ends up having to use user.send('recommended_product/song'). Since these all end up calling recommended_for anyway, perhaps make that public instead of private. (While you are at it, you could make liked_ids_for(klass) public, too.)

Nuru avatar Dec 17 '15 02:12 Nuru

I completely forgot about that issue! 😳

davidcelis avatar Dec 17 '15 02:12 davidcelis