From 4d75901c027ecd5e7f08b63c1b5d7f96be74981e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= <matteeyah@gmail.com>
Date: Tue, 5 Dec 2017 16:12:11 +0100
Subject: [PATCH] Use ClustersFinder for setting counts

---
 app/controllers/projects/clusters_controller.rb       | 7 +++----
 spec/controllers/projects/clusters_controller_spec.rb | 9 ++++-----
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/app/controllers/projects/clusters_controller.rb b/app/controllers/projects/clusters_controller.rb
index f3939c74591..0907daacbc3 100644
--- a/app/controllers/projects/clusters_controller.rb
+++ b/app/controllers/projects/clusters_controller.rb
@@ -9,10 +9,9 @@ class Projects::ClustersController < Projects::ApplicationController
 
   def index
     @scope = params[:scope] || 'all'
-    clusters = ClustersFinder.new(project, current_user, @scope).execute
-    @clusters = clusters.page(params[:page])
-    @active_count = project.clusters.enabled.count
-    @inactive_count = project.clusters.disabled.count
+    @clusters = ClustersFinder.new(project, current_user, @scope).execute.page(params[:page])
+    @active_count = ClustersFinder.new(project, current_user, :active).execute.count
+    @inactive_count = ClustersFinder.new(project, current_user, :inactive).execute.count
     @all_count = @active_count + @inactive_count
   end
 
diff --git a/spec/controllers/projects/clusters_controller_spec.rb b/spec/controllers/projects/clusters_controller_spec.rb
index dcdb75b6ff6..15d94575f80 100644
--- a/spec/controllers/projects/clusters_controller_spec.rb
+++ b/spec/controllers/projects/clusters_controller_spec.rb
@@ -17,9 +17,8 @@ describe Projects::ClustersController do
 
       context 'when project has one or more clusters' do
         let(:project) { create(:project) }
-        let(:enabled_cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
-        let(:disabled_cluster) { create(:cluster, :disabled, :provided_by_gcp, projects: [project]) }
-
+        let!(:enabled_cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
+        let!(:disabled_cluster) { create(:cluster, :disabled, :provided_by_gcp, projects: [project]) }
         it 'lists available clusters' do
           go
 
@@ -31,8 +30,8 @@ describe Projects::ClustersController do
         it 'assigns counters to correct values' do
           go
 
-          expect(assigns(:active_count)).to eq(project.clusters.enabled.count)
-          expect(assigns(:inactive_count)).to eq(project.clusters.disabled.count)
+          expect(assigns(:active_count)).to eq(1)
+          expect(assigns(:inactive_count)).to eq(1)
         end
 
         context 'when page is specified' do
-- 
2.30.9