summaryrefslogtreecommitdiff
path: root/app/finders/cluster_ancestors_finder.rb
blob: 2f9709ee0578bf633484713f89d45d9b5d9fe550 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

class ClusterAncestorsFinder
  include Gitlab::Utils::StrongMemoize

  def initialize(clusterable, current_user)
    @clusterable = clusterable
    @current_user = current_user
  end

  def execute
    return [] unless can_read_clusters?

    clusterable.clusters + ancestor_clusters
  end

  def has_ancestor_clusters?
    ancestor_clusters.any?
  end

  private

  attr_reader :clusterable, :current_user

  def can_read_clusters?
    Ability.allowed?(current_user, :read_cluster, clusterable)
  end

  # This unfortunately returns an Array, not a Relation!
  def ancestor_clusters
    strong_memoize(:ancestor_clusters) do
      Clusters::Cluster.ancestor_clusters_for_clusterable(clusterable)
    end
  end
end