From 9e00a237161679b0d6afdefd246e0a7bf209510c Mon Sep 17 00:00:00 2001
From: Yorick Peterse <yorickpeterse@gmail.com>
Date: Fri, 4 Mar 2016 11:39:00 +0100
Subject: [PATCH] Clean up ProjectsFinder for getting user projects

We don't need the extra layer of nesting of UNION queries here (as
User#authorized_projects already returns a UNION'd query).
---
 app/finders/projects_finder.rb | 5 ++++-
 app/models/user.rb             | 9 ++++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb
index 3b4e0362e0..2b8fba77bb 100644
--- a/app/finders/projects_finder.rb
+++ b/app/finders/projects_finder.rb
@@ -52,7 +52,10 @@ class ProjectsFinder
 
   def all_projects(current_user)
     if current_user
-      [current_user.authorized_projects, public_and_internal_projects]
+      [
+        *current_user.project_relations,
+        public_and_internal_projects
+      ]
     else
       [Project.public_only]
     end
diff --git a/app/models/user.rb b/app/models/user.rb
index fc4cf92be6..725f748faf 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -442,6 +442,11 @@ class User < ActiveRecord::Base
     Project.where("projects.id IN (#{projects_union.to_sql})")
   end
 
+  # Returns all the project relations
+  def project_relations
+    [personal_projects, groups_projects, projects]
+  end
+
   def owned_projects
     @owned_projects ||=
       Project.where('namespace_id IN (?) OR namespace_id = ?',
@@ -830,9 +835,7 @@ class User < ActiveRecord::Base
   private
 
   def projects_union
-    Gitlab::SQL::Union.new([personal_projects.select(:id),
-                            groups_projects.select(:id),
-                            projects.select(:id)])
+    Gitlab::SQL::Union.new(project_relations.map { |r| r.select(:id) })
   end
 
   def ci_projects_union
-- 
2.30.9