# Correct place for this code?

**URL:** <https://www.ruby-forum.com/t/correct-place-for-this-code/70260>\
**Category:** Rails\
**Created:** [September 27, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/correct-place-for-this-code/70260 "2006-09-27T20:27:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [September 27, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/correct-place-for-this-code/70260/1 "2006-09-27T20:27:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [September 27, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/correct-place-for-this-code/70260/2 "2006-09-27T20:41:48Z")

</div>

Mohit,

There is already a truncate TextHelper.

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

Regards,  
Brian

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [September 27, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/correct-place-for-this-code/70260/3 "2006-09-27T21:03:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [September 27, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/correct-place-for-this-code/70260/4 "2006-09-27T21:07:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [September 27, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/correct-place-for-this-code/70260/5 "2006-09-27T21:09:52Z")

</div>

Hi Nasir,

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

Cheers  
Mohit.
