Skip to content

Instantly share code, notes, and snippets.

@ypetya
Created February 23, 2010 14:56
Show Gist options
  • Save ypetya/312253 to your computer and use it in GitHub Desktop.
Save ypetya/312253 to your computer and use it in GitHub Desktop.
# config.yaml : (Load this into an OpenStruct at the initializers, as usual...)
#
# test_legnths:
# default: 20
# title: 150
# description_in_list: 450
module ApplicationHelper
# {{{ this will cut the labels length from a template name
def cut_string_length string,len,suffix
max_count = "{#{len}}"
string.gsub(/^(.#{max_count}).*$/u) {"#{$1}#{suffix}"}
end
# call this function to use it
def fix(string, max_length = nil)
len = CONFIG.text_lengths[max_length.to_s] || CONFIG.text_lengths['default'] if max_length
h( max_length ? cut_string_length( string, len, '...' ) : string )
end
# }}}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment