summaryrefslogtreecommitdiff
path: root/app/helpers
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2017-08-04 15:40:33 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2017-08-07 12:38:27 +0200
commitf77fda6437bfeb946510abdf5c56872af392f624 (patch)
tree247834fbb53527e37e8948c1bf001f3c69d0307a /app/helpers
parent81933cfdd3e23d24ae18c0d2f5452ecd9690ab63 (diff)
downloadgitlab-ce-f77fda6437bfeb946510abdf5c56872af392f624.tar.gz
Improve checking if projects would be returned
In various places we check if the same relation would return projects. This is done using "any?" which will run a COUNT query with any LIMIT/OFFSET values still applied. To work around all this we introduce 2 helper methods that take care of doing the right thing. This leads to the produced queries being simpler and fewer queries being executed.
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/projects_helper.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index 34ff6107eab..a268413e84f 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -225,6 +225,26 @@ module ProjectsHelper
end
end
+ # Returns true if any projects are present.
+ #
+ # If the relation has a LIMIT applied we'll cast the relation to an Array
+ # since repeated any? checks would otherwise result in multiple COUNT queries
+ # being executed.
+ #
+ # If no limit is applied we'll just issue a COUNT since the result set could
+ # be too large to load into memory.
+ def any_projects?(projects)
+ if projects.limit_value
+ projects.to_a.any?
+ else
+ projects.except(:offset).any?
+ end
+ end
+
+ def has_projects_or_name?(projects, params)
+ !!(params[:name] || any_projects?(projects))
+ end
+
private
def repo_children_classes(field)