Skip to content

Instantly share code, notes, and snippets.

@blissdev
Created June 21, 2009 11:12
Show Gist options
  • Save blissdev/133470 to your computer and use it in GitHub Desktop.
Save blissdev/133470 to your computer and use it in GitHub Desktop.
# At the first word-break past length, span the rest of the string with a
# class 'full-text' and append a link with a class of 'more-link'.
#
# This is tightly coupled to the page_scripts added to
# views/shop/results.rhtml and the knowledge that the category sidebar text
# is going to be rendered within a p tag.
def hide_past text, length=200, interstitial="..." # ought to take options for link_text, link_class, and span_class
return text if text.length <= length
text.sub(/\A(.{0,#{length}}\b)(.*?)(<\/p>)?\z/m) {|match|
$1 + content_tag(:span, $2, :class => 'full-text') +
link_to(interstitial, '#', :class => 'more-link', :title => 'toggle full text') #+ $3
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment