summaryrefslogtreecommitdiff
path: root/app/finders/starred_projects_finder.rb
blob: e5eeb19a8f3b5c1a2aa32f608b88e58e2d2ad27a (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
# frozen_string_literal: true

class StarredProjectsFinder < UnionFinder
  def initialize(user)
    @user = user
  end

  # Finds the projects "@user" starred, limited to either public projects or
  # projects visible to the given user.
  #
  # current_user - When given the list of the projects is limited to those only
  #                visible by this user.
  #
  # Returns an ActiveRecord::Relation.
  # rubocop: disable CodeReuse/ActiveRecord
  def execute(current_user = nil)
    segments = all_projects(current_user)

    find_union(segments, Project).includes(:namespace).order_id_desc
  end
  # rubocop: enable CodeReuse/ActiveRecord

  private

  def all_projects(current_user)
    projects = []

    projects << @user.starred_projects.visible_to_user(current_user) if current_user
    projects << @user.starred_projects.public_to_user(current_user)

    projects
  end
end