diff --git a/app/finders/clusters_finder.rb b/app/finders/clusters_finder.rb index 2b29c7bdf12690ab44ea9a70f66e2a62b4f15714..26bae3193da642bbd9ccf4e35d03476a86c100b4 100644 --- a/app/finders/clusters_finder.rb +++ b/app/finders/clusters_finder.rb @@ -1,19 +1,29 @@ class ClustersFinder + attr_reader :project, :user, :scope + def initialize(project, user, scope) @project = project @user = user - @scope = scope + @scope = scope || :active end def execute - clusters = case @scope - when :all - @project.clusters - when :enabled - @project.clusters.enabled - when :disabled - @project.clusters.disabled - end - clusters.map { |cluster| cluster.present(current_user: @user) } + clusters = project.clusters + filter_by_scope(clusters) + end + + private + + def filter_by_scope(clusters) + case @scope.to_sym + when :all + clusters + when :inactive + clusters.disabled + when :active + clusters.enabled + else + raise "Invalid scope #{@scope}" + end end end diff --git a/spec/finders/clusters_finder_spec.rb b/spec/finders/clusters_finder_spec.rb index 29fb3846d03e0aba6c27d976e701bc7de69c64c6..3e7bbbe39c4990f8db92fc3271b416d9271812c9 100644 --- a/spec/finders/clusters_finder_spec.rb +++ b/spec/finders/clusters_finder_spec.rb @@ -15,19 +15,19 @@ describe ClustersFinder do context 'when scope is all' do let(:scope) { :all } - it { is_expected.to eq(project.clusters.to_a) } + it { is_expected.to eq(project.clusters) } end context 'when scope is enabled' do - let(:scope) { :enabled } + let(:scope) { :active } - it { is_expected.to eq(project.clusters.enabled.to_a) } + it { is_expected.to eq(project.clusters.enabled) } end context 'when scope is disabled' do - let(:scope) { :disabled } + let(:scope) { :inactive } - it { is_expected.to eq(project.clusters.disabled.to_a) } + it { is_expected.to eq(project.clusters.disabled) } end end end