def truncate(value, max_length)
      raise ArgumentError, "max_length must be greater than 3" if max_length <= 3
      if value.is_a?(String) && value.bytesize > max_length
        max_truncated = max_length - 3
        truncated = value[0, max_truncated]
        while truncated.bytesize > max_truncated do
          truncated.chop!
        end
        truncated + "..."
      else
        value
      end
    end
def truncate(value, max_length)
      raise ArgumentError, "max_length must be greater than 3" if max_length <= 3
      if value.is_a?(String) && value.bytesize > max_length
        max_truncated = max_length - 3
        truncated = value[0, max_truncated]
        while truncated.bytesize > max_truncated do
          truncated.chop!
        end
        truncated + "..."
      else
        value
      end
    end
Truncate string if it exceeds maximum length
 Do length check with bytesize rather than size since this method
 is only intended for use with ruby 1.9 and above, otherwise
 multi-byte characters could cause this code to be too lenient

 @param [String, NilClass] value to be truncated
 @param [Integer] max_length allowed; must be greater than 3

 @return [String, NilClass] truncated string or original value if it is not a string

 @raise [ArgumentError] max_length too small