Re: [CommunityEngine] Page model has a magical find method

25 views
Skip to first unread message

Bruno Bornsztein

unread,
Mar 18, 2013, 3:20:23 PM3/18/13
to communi...@googlegroups.com
There's really no magic to it: since the Page id parameter is coming in the form of "123-page-title-goes-here", Rails just calls #to_i on anything that comes into ActiveRecord#find, so that parameter just gets turned into "123".

Actually, this should probably be updated to use friendly_id, the way User does (see: https://github.com/bborn/communityengine/blob/master/app/models/user.rb#L13). This would allow you to use custom slugs, as you're describing.



On Mon, Mar 18, 2013 at 10:51 AM, Dan Coutu <co...@snowy-owl.com> wrote:
I see that the page model seems to have the ability to find a page by either id or a slug that's made up of a combination of id and parameterized title. However the page model provides no method for this extension of the find method and I haven't found any gem or plugin that seems to handle it either.

The reason that this is important to me is that I want to be able to use a type of slug that doesn't rely on either the id or the title so that I can readily create links to static pages based on a slug value that won't change even if the page title or id does change. (The id would change if the original page was deleted and a new one created, or if the slug on the original was removed and assigned to a different page.) Examples of such pages are: About Us, Privacy Policy, Terms and Conditions.

What I'm really asking here is if anyone knows how to override the find method for pages so that it supports both the current lookup as well as my own slug lookup. I'm a bit reluctant to just declare an override in my local page model without first seeing what the current code looks like, so that I don't make a mess of things.

Dan

--
You received this message because you are subscribed to the Google Groups "CommunityEngine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to communityengi...@googlegroups.com.
To post to this group, send email to communi...@googlegroups.com.
Visit this group at http://groups.google.com/group/communityengine?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dan Coutu

unread,
Mar 20, 2013, 10:30:35 PM3/20/13
to communi...@googlegroups.com
Ah, that's useful to know. I would agree that friendly_id seems like a good idea but what a quick glance at the readme for it doesn't reveal is whether or not it is possible to provide explicit, versus implicit, control over the value of the slug.

For example I'd like to be able to create a link on a site's home page that points to /current_news so that the link remains constant. The corresponding page would not have to have a title of "Current News" but rather could have a title that reflects a specific news item. From what I can see of the example on the friendly_id readme it would do something like convert the title to a slug value implicitly and that's not what I would always want. (Sometimes it can very convenient though.)

Dan

Dan Coutu

unread,
Mar 20, 2013, 11:43:29 PM3/20/13
to communi...@googlegroups.com
I looked further into friendly_id and it does support the ability to specify your own rules for creating and maintaining the slugs. Given that it also provides automated url creation that uses slugs instead of numeric id values I'd say that this gem is a big win and really ought to be incorporated into CE. The flexibility of friendly_id is very impressive.

Dan

Bruno Bornsztein

unread,
Mar 21, 2013, 8:47:44 AM3/21/13
to communi...@googlegroups.com
Hi Dan,
It's already incorporated into ce. Check out the user model: https://github.com/bborn/communityengine/blob/master/app/models/user.rb#L13


--

Dan Coutu

unread,
Mar 21, 2013, 12:08:13 PM3/21/13
to communi...@googlegroups.com
That's excellent! I'm thinking that it makes sense to use it in a few other models as well. Let me review the code and come with a list.
Reply all
Reply to author
Forward
0 new messages