Skip to content

Commit

Permalink
feat: Truncate tags in UI when they are super long
Browse files Browse the repository at this point in the history
  • Loading branch information
bangn committed Oct 7, 2021
1 parent c0008e9 commit 66a08bb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
2 changes: 0 additions & 2 deletions lib/pact_broker/date_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,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
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: 5 additions & 3 deletions lib/pact_broker/ui/view_models/index_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
module PactBroker
module UI
module ViewDomain
using PactBroker::StringRefinements

class IndexItem
extend Forwardable

Expand Down Expand Up @@ -75,11 +77,11 @@ def consumer_version_branch_heads
end

def consumer_version_latest_tag_names
@relationship.tag_names
@relationship.tag_names.map(&:ellipsisize)
end

def provider_version_latest_tag_names
@relationship.latest_verification_latest_tags.collect(&:name)
@relationship.latest_verification_latest_tags.collect(&:name).map(&:ellipsisize)
end

def provider_version_branch_heads
Expand Down Expand Up @@ -254,7 +256,7 @@ def base_url
def pact_tags
@relationship.tag_names.map do |tag|
{
name: tag,
name: tag.ellipsisize,
deletionUrl: PactBroker::Api::PactBrokerUrls.tagged_pact_versions_url(consumer_name, provider_name, tag, base_url)
}
end.to_json
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
46 changes: 37 additions & 9 deletions spec/lib/pact_broker/ui/view_models/index_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module ViewDomain
let(:latest_pact) { instance_double("PactBroker::Domain::Pact", consumer_version_number: "1.2.3") }
let(:latest_verification) { instance_double("PactBroker::Domain::Verification") }
let(:domain_relationship) { PactBroker::Domain::IndexItem.new(consumer, provider, consumer_version, latest_pact, latest, latest_verification, [], [], tags, latest_verification_latest_tags)}
let(:very_long_tag) { "This_is_a_rediculously_long_tag_names_It_will_be_truncated_in_the_middle" }
let(:expected_truncate_tag) { "This_is_a_...the_middle" }
let(:tags) { [] }
let(:verification_tag_1) { instance_double("PactBroker::Tags::TagWithLatestFlag", name: "dev") }
let(:verification_tag_2) { instance_double("PactBroker::Tags::TagWithLatestFlag", name: "prod") }
Expand Down Expand Up @@ -132,21 +134,47 @@ module ViewDomain
end

describe "consumer_version_latest_tag_names" do
let(:tags) { ["master", "prod"] }
its(:consumer_version_latest_tag_names) { is_expected.to eq ["master", "prod"] }
context "when tags is short" do
let(:tags) { ["master", "prod"] }
its(:consumer_version_latest_tag_names) { is_expected.to eq ["master", "prod"] }
end

context "when tags is too long" do
let(:tags) { [very_long_tag] }
its(:consumer_version_latest_tag_names) { is_expected.to eq [expected_truncate_tag] }
end
end

describe "provider_version_latest_tag_names" do
its(:provider_version_latest_tag_names) { is_expected.to eq ["dev", "prod"] }
context "when tags is short" do
its(:provider_version_latest_tag_names) { is_expected.to eq ["dev", "prod"] }
end

context "when tags is too long" do
let(:verification_tag_1) { instance_double( "PactBroker::Tags::TagWithLatestFlag", name: very_long_tag) }
its(:provider_version_latest_tag_names) { is_expected.to eq [expected_truncate_tag, "prod"] }
end
end

describe "pact_tags" do
let(:tags) { ["master", "prod"] }
its(:pact_tags) do
is_expected.to eq([
{ name: "master", deletionUrl: "/pacts/provider/Provider%20Name/consumer/Consumer%20Name/tag/master" },
{ name: "prod", deletionUrl: "/pacts/provider/Provider%20Name/consumer/Consumer%20Name/tag/prod" }
].to_json)
context "when tags is short" do
let(:tags) { ["master", "prod"] }
its(:pact_tags) do
is_expected.to eq([
{ name: "master", deletionUrl: "/pacts/provider/Provider%20Name/consumer/Consumer%20Name/tag/master" },
{ name: "prod", deletionUrl: "/pacts/provider/Provider%20Name/consumer/Consumer%20Name/tag/prod" }
].to_json)
end
end

context "when tags is too long" do
let(:tags) { [very_long_tag, "prod"] }
its(:pact_tags) do
is_expected.to eq([
{ name: expected_truncate_tag, deletionUrl: "/pacts/provider/Provider%20Name/consumer/Consumer%20Name/tag/#{very_long_tag}" },
{ name: "prod", deletionUrl: "/pacts/provider/Provider%20Name/consumer/Consumer%20Name/tag/prod" }
].to_json)
end
end
end

Expand Down

0 comments on commit 66a08bb

Please sign in to comment.