Refactor ClustersFinder

This commit is contained in:
Matija Čupić 2017-11-28 14:22:29 +01:00
parent 63b08da9cd
commit 8796e7278e
No known key found for this signature in database
GPG Key ID: 4BAF84FFACD2E5DE
2 changed files with 25 additions and 15 deletions

View File

@ -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

View File

@ -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