Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Augment truncate helper to accept a separator

#1807

The truncate text helper currently cuts off the given text mid-word if that's where the length limit is reached.

This backwards-compatible patch allows you to pass the :separator option to make the truncated result end at the last occurrence of the given separator (within the length allowed).

If you don't pass the :separator option the helper behaves as it currently does. This means that all existing code will continue to work the same way.

Typically you would use this to have the truncated text finish at the end of a word.

For example:

truncate("Once upon a time in a world far far away")

=> Once upon a time in a world f...

truncate("Once upon a time in a world far far away", :separator => ' ')

=> Once upon a time in a world...

Reported by Andy Stewart · January 28th, 2009 @ 04:56 PM

State: resolved
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Olly
    Olly

    Looks like a useful patch to me. +1

    January 29th, 2009 @ 02:23 PM

  2. Andy Stewart
    Andy Stewart
    • Title changed from Modify truncate helper to accept a separator to Augment truncate helper to accept a separator

    February 2nd, 2009 @ 02:39 PM

  3. Repository
    Repository
    • State changed from new to resolved

    May 18th, 2009 @ 08:39 PM