Correct place for this code?

Hi! I’m writing a piece of code that tries to truncate the amount of
text displayed in the cell to restrict it such that the formatting
doesn’t spoil if the string is too long.

What I want to do is:

  1. If the ‘met_person’ string is longer than 20 characters, I want to
    pick the first 18 and add “…” to it and display that in the cell.
  2. Also, I want to use the title attribute for the cell so that a mouse
    over shows the full text.
  3. If the name is shorter than 20 characters, it is fine - just display
    it completely

So, right now, I have something like this which works (I guess it can be
cleaned up):

<%= (meeting.met_person.length > 20)? meeting.met_person[0..17]+'...':meeting.met_person %>

My questions:

  1. I’d like to make this a generic helper that automatically truncates
    the text. So, should I put in a helper file? So, this could be
    something like truncate_text(meeting.met_person, 20) and the function
    could do the check and return the correct string.

  2. Since there is sufficient repetition to create the title and the text
    in the cell, it may be nice to create something that creates the full
    HTML code, rather than just the string. Where would that go? Would
    that be a helper too?

Thanks
Mohit.

Mohit,

There is already a truncate TextHelper.

I believe you do something like…
<%= truncate(meeting.met_person, 20) %>

Regards,
Brian

For the repetitive html snippet you may write a partial, the partial
could
then use the truncate helper.

Ouch! I thought I had seen that in the list, but couldn’t find it in
the emails I saved, so I read up on the String class and made my own. I
shall take a look at that - thanks!

Cheers
Mohit.

Hi Nasir,

That certainly answers that question! Thanks guys! I had sorta
forgotten that the partial could use a helper…

Cheers
Mohit.