Iamvery

The musings of a nerd


Use Rails' truncate helper!

— Dec 03, 2012

TL;DR: Ever wanted to cut a long string down to something like “This is the song that n…”? Use Rails’ truncate helper!

Warning: This is my first blog! Exciting, yes! But you’ll have to bear with my rambling as I get used to the process. :)

The other day I ran into the mundane issue of having a record’s title attribute exceed the width that a Twitter Bootstrap tooltip could display. I quickly threw together some helper methods that would trim excessive titles down to size:

class Article
  MAX_SHORT_TITLE_LENGTH = 10
  SHORT_TITLE_SUFFIX = '...'

  def title
    'The longest title imaginable.'
  end

  def short_title
    if long_title?
      "#{title[0...short_title_length]}#{SHORT_TITLE_SUFFIX}"
    else
      title
    end
  end

  def long_title?
    title.length > MAX_SHORT_TITLE_LENGTH
  end

  def short_title_length
    MAX_SHORT_TITLE_LENGTH - SHORT_TITLE_SUFFIX.length
  end
end

Along with it’s specs, I was quite pleased with my solution. So much so I thought to myself “Ah hah! I should write a small gem to easily add this functionality to other projects!” Then it hit me. Such a common problem is likely already solved…

And there it was ActiveView::Helpers::TextHelper#truncate. The exact functionality I was (re)implementing. Don’t miss out on this little helper! Its simple:

# In your view or helper
truncate(article.title, :length => 10) # => 'The lon...'

© Jay Hayes