The Accelerate HR Blog
Truncating with dignity in Rails (Sun Dec 02 2007)
I noticed that my RSS feed was showing you post titles but no content. Which would be fine if I wanted to drag you kicking and screaming into my site. But I'd prefer you to come along quietly, if you don't mind sir / mad ...
So I needed to find out how proper programmers did it. Well that wasn't too hard: I'd just missed a line in my xml.
Now, as you've seen, I'm not someone who writes twenty words when a thousand would do. But for the feed content, I thought a little truncating would be in order, just giving you enough detail to tempt you in. And that's when I came across a little gem (no, gem, not Gem) from Daniel Morrison, posted a few months back - awesome_truncate. What does it do? Unlike the normal Rails truncate, it doesn't hack off your flow in mid-word - like some paragraphs not too distant from here. It's just a little 5-line method with a nice little regular expression for those of you who are into that. You could use it as a helper, or tie it directly into your model, as I did. Nice touch, Daniel - thank you.
Now I wonder if the feed's still going to work?
1 hour later: Nope. It didn't. So you've still got no content in the feed. But it's not our little truncate helper causing the problem. Thats working fine - as you'll see if you go do a blog-search. But for some reason, the line I added to the xml just doesn't want to work, whether the content is truncated or not. Back to the drawing board.
Much later: Think we've got a problemette with valid xml - and that means tests. There'll be more about this.
Glad you found the code useful. Cheers!