Commit 8796e727 authored by Matija Čupić's avatar Matija Čupić

Refactor ClustersFinder

parent 63b08da9
class ClustersFinder class ClustersFinder
attr_reader :project, :user, :scope
def initialize(project, user, scope) def initialize(project, user, scope)
@project = project @project = project
@user = user @user = user
@scope = scope @scope = scope || :active
end end
def execute def execute
clusters = case @scope clusters = project.clusters
when :all filter_by_scope(clusters)
@project.clusters end
when :enabled
@project.clusters.enabled private
when :disabled
@project.clusters.disabled def filter_by_scope(clusters)
end case @scope.to_sym
clusters.map { |cluster| cluster.present(current_user: @user) } when :all
clusters
when :inactive
clusters.disabled
when :active
clusters.enabled
else
raise "Invalid scope #{@scope}"
end
end end
end end
...@@ -15,19 +15,19 @@ describe ClustersFinder do ...@@ -15,19 +15,19 @@ describe ClustersFinder do
context 'when scope is all' do context 'when scope is all' do
let(:scope) { :all } let(:scope) { :all }
it { is_expected.to eq(project.clusters.to_a) } it { is_expected.to eq(project.clusters) }
end end
context 'when scope is enabled' do 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 end
context 'when scope is disabled' do 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 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