Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Truncate tags in UI when they are super long #513

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/pact_broker/date_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module DateHelper
extend self

# Ripped from actionview/lib/action_view/helpers/date_helper.rb

def local_date_in_words datetime
datetime.to_time.localtime.to_datetime.strftime("%a %d %b %Y, %l:%M%P %:z").gsub(" ", " ")
end
Expand All @@ -31,9 +30,6 @@ def distance_of_time_in_words(from_time, to_time = 0, options = {})
distance_in_minutes = ((to_time - from_time)/60.0).round
distance_in_seconds = (to_time - from_time).round

# require 'pry'; pry(binding);

# locale = I18n.with_options :locale => options[:locale], :scope => options[:scope]
locale = Locale.new(:locale => options[:locale], :scope => options[:scope])
case distance_in_minutes
when 0..1
Expand Down
9 changes: 9 additions & 0 deletions lib/pact_broker/string_refinements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def camelcase(*separators)

str
end

# Adopt from https://stackoverflow.com/questions/1451384/how-can-i-center-truncate-a-string
def ellipsisize(minimum_length: 20, edge_length: 10)
return self if self.length < minimum_length || self.length <= edge_length * 2

edge = "." * edge_length
mid_length = self.length - edge_length * 2
gsub(/(#{edge}).{#{mid_length},}(#{edge})/, '\1...\2')
end
end
end
end
8 changes: 8 additions & 0 deletions lib/pact_broker/ui/controllers/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "padrino-core"
require "haml"
require "pact_broker/services"
require "pact_broker/string_refinements"

module PactBroker
module UI
module Controllers
class Base < Padrino::Application
using PactBroker::StringRefinements

set :root, File.join(File.dirname(__FILE__), "..")
set :show_exceptions, ENV["RACK_ENV"] != "production"
Expand All @@ -18,6 +20,12 @@ def base_url
# rather than request.base_url, which uses the X-Forwarded headers.
env["pactbroker.base_url"] || ""
end

helpers do
def ellipsisize(string)
string.ellipsisize
end
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/pact_broker/ui/views/dashboard/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@
- if view == "branch" || view == "all"
- index_item.consumer_version_branch_heads.each do | branch_head |
%div{"class": "tag badge badge-dark", "title": branch_head.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
= "branch: " + branch_head.branch_name
= "branch: " + ellipsisize(branch_head.branch_name)
- if view == "tag" || view == "all"
- index_item.consumer_version_latest_tag_names.each do | tag_name |
.tag.badge.badge-primary
= "tag: " + tag_name
= "tag: " + ellipsisize(tag_name)
- if view == "environment" || view == "all"
- index_item.consumer_version_environment_names.each do | environment_name |
.tag.badge.badge-success
= "env: " + environment_name
= "env: " + ellipsisize(environment_name)
- if view == "all" && index_item.display_latest_label? && index_item.latest?
.tag.badge.bg-light
latest
Expand All @@ -108,15 +108,15 @@
- if view == "branch" || view == "all"
- index_item.provider_version_branch_heads.each do | branch_head |
%div{"class": "tag badge badge-dark", "title": branch_head.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
= "branch: " + branch_head.branch_name
= "branch: " + ellipsisize(branch_head.branch_name)
- if view == "tag" || view == "all"
- index_item.provider_version_latest_tag_names.each do | tag_name |
.tag.badge.badge-primary
= "tag: " + tag_name
= "tag: " + ellipsisize(tag_name)
- if view == "environment" || view == "all"
- index_item.provider_version_environment_names.each do | environment_name |
.tag.badge.badge-success
= "env: " + environment_name
= "env: " + ellipsisize(environment_name)
%td.pact
%span.pact
%a{ href: index_item.pact_url, title: "View pact" }
Expand Down
16 changes: 8 additions & 8 deletions lib/pact_broker/ui/views/matrix/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,22 @@
.tag-parent{"title": branch.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
- branch_class = branch.latest? ? "tag badge badge-dark" : "tag badge badge-secondary"
%div{"class": branch_class}
= "branch: " + branch.name
= "branch: " + ellipsisize(branch.name)
- line.consumer_versions_in_environments.each do | version_in_environment |
.tag-parent{"title": version_in_environment.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
%a{href: version_in_environment.url}
.tag.badge.badge-success
= "env: " + version_in_environment.environment_name
= "env: " + ellipsisize(version_in_environment.environment_name)
- line.latest_consumer_version_tags.each do | tag |
.tag-parent{"title": tag.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
%a{href: tag.url}
.tag.badge.badge-primary
= "tag: " + tag.name
= "tag: " + ellipsisize(tag.name)
- line.other_consumer_version_tags.each do | tag |
.tag-parent{"title": tag.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
%a{href: tag.url}
.tag.badge.badge-secondary
= "tag: " + tag.name
= "tag: " + ellipsisize(tag.name)
%td.pact-published{'data-sort-value' => line.pact_published_order, "data-toggle": "tooltip", "title": line.pact_version_sha_message, "data-placement": "right", "data-pact-version-sha": line.pact_version_sha}
%a{href: line.pact_publication_date_url}
- if options.all_rows_checked
Expand All @@ -182,22 +182,22 @@
.tag-parent{"title": branch.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
- branch_class = branch.latest? ? "tag badge badge-dark" : "tag badge badge-secondary"
%div{"class": branch_class}
= "branch: " + branch.name
= "branch: " + ellipsisize(branch.name)
- line.provider_versions_in_environments.each do | version_in_environment |
.tag-parent{"title": version_in_environment.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
%a{href: version_in_environment.url}
.tag.badge.badge-success
= "env: " + version_in_environment.environment_name
= "env: " + ellipsisize(version_in_environment.environment_name)
- line.latest_provider_version_tags.each do | tag |
.tag-parent{"title": tag.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
%a{href: tag.url}
.tag.badge.badge-primary
= "tag:" + tag.name
= "tag:" + ellipsisize(tag.name)
- line.other_provider_version_tags.each do | tag |
.tag-parent{"title": tag.tooltip, "data-toggle": "tooltip", "data-placement": "right"}
%a{href: tag.url}
.tag.badge.badge-secondary
= "tag: " + tag.name
= "tag: " + ellipsisize(tag.name)
%td.verification-result{class: line.verification_status_class, "title": line.pre_verified_message, "data-toggle": "tooltip", "data-placement": "left"}
%a{href: line.verification_status_url}
- if options.all_rows_checked && line.number
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/pact_broker/string_refinements_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "pact_broker/string_refinements"

module PactBroker
describe StringRefinements do
using StringRefinements

describe "ellipsisize" do
let(:very_long_string) do
"This is a very long string. May be too long to be true. It should be truncated in the middle"
end

context "when using default value to truncate the string" do
it "truncates the string in the middle to the default length" do
expect(very_long_string.ellipsisize).to eq("This is a ...the middle")
end
end

context "when using customised value to truncate the string" do
it "truncates the string in the middle to the customised length" do
expect(very_long_string.ellipsisize(edge_length: 15)).to eq("This is a very ...d in the middle")
end
end
end
end
end