adds new string truncation method [fixes SCI-2344]

This commit is contained in:
zmagod 2018-05-03 14:24:23 +02:00
parent b277501e6d
commit 0b721d86b1
3 changed files with 20 additions and 1 deletions

View file

@ -430,6 +430,7 @@ class ReportsController < ApplicationController
private
include StringUtility
VisibleProject = Struct.new(:path, :name)
def load_vars
@ -454,7 +455,8 @@ class ReportsController < ApplicationController
.limit(Constants::SEARCH_LIMIT)
.select(:id, :name)
@visible_projects = projects.collect do |project|
VisibleProject.new(new_project_reports_path(project), project.name)
VisibleProject.new(new_project_reports_path(project),
ellipsisize(project.name))
end
end

View file

@ -0,0 +1,13 @@
module StringUtility
def ellipsisize(
string,
minimum_length = Constants::MAX_NAME_TRUNCATION,
edge_length = Constants::MAX_EDGE_LENGTH
)
length = string.length
return string if length < minimum_length || length <= edge_length * 2
edge = '.' * edge_length
mid_length = length - edge_length * 2
string.gsub(/(#{edge}).{#{mid_length},}(#{edge})/, '\1...\2')
end
end

View file

@ -9,6 +9,10 @@ class Constants
NAME_MAX_LENGTH = 255
# Max characters for short text fields, after which they get truncated
NAME_TRUNCATION_LENGTH = 25
# Max edge length
MAX_EDGE_LENGTH = 75
# Max character for listing projects in dropdown
MAX_NAME_TRUNCATION = 150
# Max characters for short text fields, in dropdownList
NAME_TRUNCATION_LENGTH_DROPDOWN = 20
# Max characters for long text fields