Commit 3eeb40df authored by Hordur Freyr Yngvason's avatar Hordur Freyr Yngvason

Stop using view helpers in Clusters::ClusterPresenter

See https://gitlab.com/gitlab-org/gitlab/-/issues/341389
parent a0ddad33
......@@ -2589,7 +2589,6 @@ Rails/IncludeUrlHelper:
- 'app/models/integrations/youtrack.rb'
- 'app/presenters/alert_management/alert_presenter.rb'
- 'app/presenters/ci/pipeline_presenter.rb'
- 'app/presenters/clusters/cluster_presenter.rb'
- 'app/presenters/environment_presenter.rb'
- 'app/presenters/gitlab/blame_presenter.rb'
- 'app/presenters/group_clusterable_presenter.rb'
......
......@@ -3,24 +3,11 @@
module Clusters
class ClusterPresenter < Gitlab::View::Presenter::Delegated
include ::Gitlab::Utils::StrongMemoize
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::UrlHelper
include IconsHelper
delegator_override_with ::Gitlab::Utils::StrongMemoize # TODO: Remove `::Gitlab::Utils::StrongMemoize` inclusion as it's duplicate
presents ::Clusters::Cluster, as: :cluster
# We do not want to show the group path for clusters belonging to the
# clusterable, only for the ancestor clusters.
def item_link(clusterable_presenter, *html_options)
if cluster.group_type? && clusterable != clusterable_presenter.subject
contracted_group_name(cluster.group) + ' / ' + link_to_cluster
else
link_to_cluster(*html_options)
end
end
def provider_label
if aws?
s_('ClusterIntegration|Elastic Kubernetes Service')
......@@ -41,16 +28,6 @@ module Clusters
can?(current_user, :read_cluster, cluster)
end
def cluster_type_description
if cluster.project_type?
s_("ClusterIntegration|Project cluster")
elsif cluster.group_type?
s_("ClusterIntegration|Group cluster")
elsif cluster.instance_type?
s_("ClusterIntegration|Instance cluster")
end
end
def show_path(params: {})
if cluster.project_type?
project_cluster_path(project, cluster, params)
......@@ -109,7 +86,7 @@ module Clusters
private
def image_path(path)
ActionController::Base.helpers.image_path(path)
ApplicationController.helpers.image_path(path)
end
# currently log explorer is only available in the scope of the project
......@@ -129,20 +106,6 @@ module Clusters
cluster.project
end
end
def contracted_group_name(group)
sanitize(group.full_name)
.sub(%r{\/.*\/}, "/ #{contracted_icon} /")
.html_safe
end
def contracted_icon
sprite_icon('ellipsis_h', size: 12, css_class: 'vertical-align-middle')
end
def link_to_cluster(html_options: {})
link_to_if(can_read_cluster?, cluster.name, show_path, html_options)
end
end
end
......
......@@ -30,129 +30,6 @@ RSpec.describe Clusters::ClusterPresenter do
end
end
describe '#item_link' do
let(:clusterable_presenter) { double('ClusterablePresenter', subject: clusterable) }
subject { presenter.item_link(clusterable_presenter) }
context 'for a group cluster' do
let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [group]) }
let(:group) { create(:group, name: 'Foo') }
let(:cluster_link) { "<a href=\"#{group_cluster_path(cluster.group, cluster)}\">#{cluster.name}</a>" }
before do
group.add_maintainer(user)
end
shared_examples 'ancestor clusters' do
context 'ancestor clusters' do
let(:root_group) { create(:group, name: 'Root Group') }
let(:parent) { create(:group, name: 'parent', parent: root_group) }
let(:child) { create(:group, name: 'child', parent: parent) }
let(:group) { create(:group, name: 'group', parent: child) }
before do
root_group.add_maintainer(user)
end
context 'top level group cluster' do
let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [root_group]) }
it 'returns full group names and link for cluster' do
expect(subject).to eq("Root Group / #{cluster_link}")
end
it 'is html safe' do
expect(presenter).to receive(:sanitize).with('Root Group').and_call_original
expect(subject).to be_html_safe
end
end
context 'first level group cluster' do
let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [parent]) }
it 'returns full group names and link for cluster' do
expect(subject).to eq("Root Group / parent / #{cluster_link}")
end
it 'is html safe' do
expect(presenter).to receive(:sanitize).with('Root Group / parent').and_call_original
expect(subject).to be_html_safe
end
end
context 'second level group cluster' do
let(:cluster) { create(:cluster, cluster_type: :group_type, groups: [child]) }
let(:ellipsis_h) do
/.*ellipsis_h.*/
end
it 'returns clipped group names and link for cluster' do
expect(subject).to match("Root Group / #{ellipsis_h} / child / #{cluster_link}")
end
it 'is html safe' do
expect(presenter).to receive(:sanitize).with('Root Group / parent / child').and_call_original
expect(subject).to be_html_safe
end
end
end
end
context 'for a project clusterable' do
let(:clusterable) { project }
let(:project) { create(:project, group: group) }
it 'returns the group name and the link for cluster' do
expect(subject).to eq("Foo / #{cluster_link}")
end
it 'is html safe' do
expect(presenter).to receive(:sanitize).with('Foo').and_call_original
expect(subject).to be_html_safe
end
include_examples 'ancestor clusters'
end
context 'for the group clusterable for the cluster' do
let(:clusterable) { group }
it 'returns link for cluster' do
expect(subject).to eq(cluster_link)
end
include_examples 'ancestor clusters'
it 'is html safe' do
expect(subject).to be_html_safe
end
end
end
context 'for a project cluster' do
let(:cluster) { create(:cluster, :project) }
let(:cluster_link) { "<a href=\"#{project_cluster_path(cluster.project, cluster)}\">#{cluster.name}</a>" }
before do
cluster.project.add_maintainer(user)
end
context 'for the project clusterable' do
let(:clusterable) { cluster.project }
it 'returns link for cluster' do
expect(subject).to eq(cluster_link)
end
end
end
end
describe '#provider_label' do
let(:cluster) { create(:cluster, provider_type: provider_type) }
......@@ -191,26 +68,6 @@ RSpec.describe Clusters::ClusterPresenter do
end
end
describe '#cluster_type_description' do
subject { described_class.new(cluster).cluster_type_description }
context 'project_type cluster' do
it { is_expected.to eq('Project cluster') }
end
context 'group_type cluster' do
let(:cluster) { create(:cluster, :provided_by_gcp, :group) }
it { is_expected.to eq('Group cluster') }
end
context 'instance_type cluster' do
let(:cluster) { create(:cluster, :provided_by_gcp, :instance) }
it { is_expected.to eq('Instance cluster') }
end
end
describe '#show_path' do
subject { described_class.new(cluster).show_path }
......
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