diff --git a/app/helpers/clusters_helper.rb b/app/helpers/clusters_helper.rb deleted file mode 100644 index f8281c893fa..00000000000 --- a/app/helpers/clusters_helper.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ClustersHelper - def can_toggle_cluster?(cluster) - can?(current_user, :update_cluster, cluster) && cluster.created? - end -end diff --git a/app/presenters/clusters/cluster_presenter.rb b/app/presenters/clusters/cluster_presenter.rb index 01cb59d0d44..a424da5ab24 100644 --- a/app/presenters/clusters/cluster_presenter.rb +++ b/app/presenters/clusters/cluster_presenter.rb @@ -5,5 +5,9 @@ module Clusters def gke_cluster_url "https://console.cloud.google.com/kubernetes/clusters/details/#{provider.zone}/#{name}" if gcp? end + + def can_toggle_cluster? + can?(current_user, :update_cluster, cluster) && created? + end end end diff --git a/app/views/projects/clusters/_cluster.html.haml b/app/views/projects/clusters/_cluster.html.haml index d292d68386c..18ca01d2d49 100644 --- a/app/views/projects/clusters/_cluster.html.haml +++ b/app/views/projects/clusters/_cluster.html.haml @@ -13,9 +13,9 @@ .table-mobile-header{ role: "rowheader" } .table-mobile-content %button{ type: "button", - class: "js-toggle-cluster-list project-feature-toggle #{'is-checked' if cluster.enabled?} #{'is-disabled' if !can_toggle_cluster?(cluster)}", + class: "js-toggle-cluster-list project-feature-toggle #{'is-checked' if cluster.enabled?} #{'is-disabled' if !cluster.can_toggle_cluster?}", "aria-label": s_("ClusterIntegration|Toggle Cluster"), - disabled: !can_toggle_cluster?(cluster), + disabled: !cluster.can_toggle_cluster?, data: { "enabled-text": s_("ClusterIntegration|Active"), "disabled-text": s_("ClusterIntegration|Inactive"), endpoint: namespace_project_cluster_path(@project.namespace, @project, cluster, format: :json) } } diff --git a/app/views/projects/clusters/index.html.haml b/app/views/projects/clusters/index.html.haml index 4dd956971ae..104e39b0e06 100644 --- a/app/views/projects/clusters/index.html.haml +++ b/app/views/projects/clusters/index.html.haml @@ -14,7 +14,7 @@ = s_("ClusterIntegration|Project namespace") .table-section.section-10{ role: "rowheader" } - @clusters.each do |cluster| - = render "cluster", cluster: cluster + = render "cluster", cluster: cluster.present(current_user: current_user) = paginate @clusters, theme: "gitlab" - elsif @scope == 'all' = render "empty_state" diff --git a/spec/helpers/clusters_helper_spec.rb b/spec/helpers/clusters_helper_spec.rb deleted file mode 100644 index 459fdfdc5a4..00000000000 --- a/spec/helpers/clusters_helper_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'spec_helper' - -describe ClustersHelper do - let(:cluster) { create(:cluster) } - - describe '.can_toggle_cluster' do - let(:user) { create(:user) } - - before do - allow(helper).to receive(:current_user).and_return(user) - end - - subject { helper.can_toggle_cluster?(cluster) } - - context 'when user can update' do - before do - allow(helper).to receive(:can?).with(any_args).and_return(true) - end - - context 'when cluster is created' do - before do - allow(cluster).to receive(:created?).and_return(true) - end - - it { is_expected.to eq(true) } - end - - context 'when cluster is not created' do - before do - allow(cluster).to receive(:created?).and_return(false) - end - - it { is_expected.to eq(false) } - end - end - - context 'when user can not update' do - before do - allow(helper).to receive(:can?).with(any_args).and_return(false) - end - - it { is_expected.to eq(false) } - end - end -end diff --git a/spec/presenters/clusters/cluster_presenter_spec.rb b/spec/presenters/clusters/cluster_presenter_spec.rb index 48d4f3671c5..e96dbfb73c0 100644 --- a/spec/presenters/clusters/cluster_presenter_spec.rb +++ b/spec/presenters/clusters/cluster_presenter_spec.rb @@ -31,4 +31,44 @@ describe Clusters::ClusterPresenter do it { is_expected.to include(cluster.provider.zone) } it { is_expected.to include(cluster.name) } end + + describe '#can_toggle_cluster' do + let(:user) { create(:user) } + + before do + allow(cluster).to receive(:current_user).and_return(user) + end + + subject { described_class.new(cluster).can_toggle_cluster? } + + context 'when user can update' do + before do + allow_any_instance_of(described_class).to receive(:can?).with(user, :update_cluster, cluster).and_return(true) + end + + context 'when cluster is created' do + before do + allow(cluster).to receive(:created?).and_return(true) + end + + it { is_expected.to eq(true) } + end + + context 'when cluster is not created' do + before do + allow(cluster).to receive(:created?).and_return(false) + end + + it { is_expected.to eq(false) } + end + end + + context 'when user can not update' do + before do + allow_any_instance_of(described_class).to receive(:can?).with(user, :update_cluster, cluster).and_return(false) + end + + it { is_expected.to eq(false) } + end + end end