Commit 0369e759 authored by Mayra Cabrera's avatar Mayra Cabrera Committed by Thong Kuah

Refactor methods on ClusterHelper

- Use sprite_icon instead of hardcoded html
- Use a more descriptive method name
- Changes specs to use html_safe matcher
parent aa86223a
...@@ -10,21 +10,11 @@ module ClustersHelper ...@@ -10,21 +10,11 @@ module ClustersHelper
# clusterable, only for the ancestor clusters. # clusterable, only for the ancestor clusters.
def cluster_group_path_display(cluster, clusterable) def cluster_group_path_display(cluster, clusterable)
if cluster.group_type? && cluster.group.id != clusterable.id if cluster.group_type? && cluster.group.id != clusterable.id
group_path_shortened(cluster.group) components = cluster.group.full_path_components
end
end
def group_path_shortened(group)
components = group.full_path_components
breadcrumb = if components.size > 2
[components.first, '…'.html_safe, components.last]
else
components
end
breadcrumb.each_with_object(''.html_safe) do |component, string| group_path_shortened(components) + ' / ' + link_to_cluster(cluster)
string.concat(component + ' / ') else
link_to_cluster(cluster)
end end
end end
...@@ -37,7 +27,31 @@ module ClustersHelper ...@@ -37,7 +27,31 @@ module ClustersHelper
end end
end end
def display_clusters_callout?(clusters, clusterable) def render_cluster_help_content?(clusters, clusterable)
clusters.length > clusterable.clusters.length clusters.length > clusterable.clusters.length
end end
private
def components_split_by_horizontal_ellipsis(components)
[
components.first,
sprite_icon('ellipsis_h', size: 12, css_class: 'vertical-align-middle').html_safe,
components.last
]
end
def link_to_cluster(cluster)
link_to cluster.name, cluster.show_path
end
def group_path_shortened(components)
breadcrumb = if components.size > 2
components_split_by_horizontal_ellipsis(components)
else
components
end
breadcrumb.join(' / ').html_safe
end
end end
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
.table-mobile-header{ role: "rowheader" }= s_("ClusterIntegration|Kubernetes cluster") .table-mobile-header{ role: "rowheader" }= s_("ClusterIntegration|Kubernetes cluster")
.table-mobile-content .table-mobile-content
= cluster_group_path_display(cluster, clusterable) = cluster_group_path_display(cluster, clusterable)
= link_to cluster.name, cluster.show_path
- unless cluster.enabled? - unless cluster.enabled?
%span.badge.badge-danger Connection disabled %span.badge.badge-danger Connection disabled
.table-section.section-25 .table-section.section-25
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
= s_("ClusterIntegration|Kubernetes clusters can be used to deploy applications and to provide Review Apps for this project") = s_("ClusterIntegration|Kubernetes clusters can be used to deploy applications and to provide Review Apps for this project")
= render 'clusters/clusters/buttons' = render 'clusters/clusters/buttons'
- if display_clusters_callout?(@clusters, clusterable) - if render_cluster_help_content?(@clusters, clusterable)
.bs-callout.bs-callout-info .bs-callout.bs-callout-info
= s_("ClusterIntegration|Clusters are utilized by selecting the nearest ancestor with a matching environment scope. For example, project clusters will override group clusters.") = s_("ClusterIntegration|Clusters are utilized by selecting the nearest ancestor with a matching environment scope. For example, project clusters will override group clusters.")
%strong %strong
......
...@@ -4,70 +4,67 @@ require 'spec_helper' ...@@ -4,70 +4,67 @@ require 'spec_helper'
describe ClustersHelper do describe ClustersHelper do
describe '#cluster_group_path_display' do describe '#cluster_group_path_display' do
let(:group) { create(:group, name: 'group') } subject { helper.cluster_group_path_display(cluster.present, clusterable) }
let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [group]) }
let(:clusterable) { group }
subject { helper.cluster_group_path_display(cluster, clusterable) } context 'for a group cluster' do
let(:cluster) { create(:cluster, :group) }
let(:clusterable) { cluster.group }
let(:cluster_link) { "<a href=\"/groups/#{clusterable.name}/-/clusters/#{cluster.id}\">#{cluster.name}</a>" }
it 'returns nothing' do it 'returns link for cluster' do
is_expected.to be_nil expect(subject).to eq(cluster_link)
end end
context 'for another clusterable' do
let(:clusterable) { create(:group) }
it 'returns the group path' do it 'escapes group name' do
is_expected.to eq('group / ') expect(subject).to be_html_safe
end end
end end
context 'for a project cluster' do context 'for a project cluster' do
let(:cluster) { create(:cluster, :project) } let(:cluster) { create(:cluster, :project) }
let(:clusterable) { cluster.project } let(:clusterable) { cluster.project }
let(:cluster_link) { "<a href=\"/#{clusterable.namespace.name}/#{clusterable.name}/clusters/#{cluster.id}\">#{cluster.name}</a>" }
it 'returns nothing' do it 'returns link for cluster' do
is_expected.to be_nil expect(subject).to eq(cluster_link)
end end
end
end
describe '#group_path_shortened' do
let(:group) { create(:group, name: 'group') }
subject { helper.group_path_shortened(group) } it 'escapes group name' do
expect(subject).to be_html_safe
it 'returns the group name with trailing slash' do end
is_expected.to eq('group / ')
end end
it 'escapes group name' do context 'with subgroups' do
expect(CGI).to receive(:escapeHTML).with('group / ').and_call_original let(:root_group) { create(:group, name: 'root_group') }
let(:cluster) { create(:cluster, :group, groups: [root_group]) }
let(:clusterable) { create(:group, name: 'group', parent: root_group) }
subject subject { helper.cluster_group_path_display(cluster.present, clusterable) }
end
context 'subgroup', :nested_groups do context 'with just one group' do
let(:root_group) { create(:group, name: 'root') } let(:cluster_link) { "<a href=\"/groups/root_group/-/clusters/#{cluster.id}\">#{cluster.name}</a>" }
let(:group) { create(:group, name: 'group', parent: root_group) }
it 'returns the full path with trailing slash' do it 'returns the group path' do
is_expected.to eq('root / group / ') expect(subject).to eq("root_group / #{cluster_link}")
end
end end
it 'escapes group names' do context 'with multiple parent groups', :nested_groups do
expect(CGI).to receive(:escapeHTML).with('root / ').and_call_original let(:sub_group) { create(:group, name: 'sub_group', parent: root_group) }
expect(CGI).to receive(:escapeHTML).with('group / ').and_call_original let(:cluster) { create(:cluster, :group, groups: [sub_group]) }
subject it 'returns the full path with trailing slash' do
expect(subject).to include('root_group / sub_group /')
end
end end
context 'deeper nested' do context 'with deeper nested groups', :nested_groups do
let(:next_group) { create(:group, name: 'next', parent: root_group) } let(:sub_group) { create(:group, name: 'sub_group', parent: root_group) }
let(:group) { create(:group, name: 'group', parent: next_group) } let(:sub_sub_group) { create(:group, name: 'sub_sub_group', parent: sub_group) }
let(:cluster) { create(:cluster, :group, groups: [sub_sub_group]) }
it 'returns a shorted path with trailing slash' do it 'includes an horizontal ellipsis' do
is_expected.to eq('root / &hellip; / group / ') expect(subject).to include('ellipsis_h')
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment