summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-08-26 22:43:04 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2015-08-26 22:43:04 +0200
commitc27c81331183dd6c878b4a74df7e62e37b006a04 (patch)
tree647d525e0723b07156d5cfb8af920f07c76c8283
parent6c23fd389f5bd024e892876d593162e1a8995ad4 (diff)
downloadgitlab-ce-c27c81331183dd6c878b4a74df7e62e37b006a04.tar.gz
Improve trending projects finder
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/finders/trending_projects_finder.rb18
1 files changed, 13 insertions, 5 deletions
diff --git a/app/finders/trending_projects_finder.rb b/app/finders/trending_projects_finder.rb
index a79bd47d986..f3f4d461efa 100644
--- a/app/finders/trending_projects_finder.rb
+++ b/app/finders/trending_projects_finder.rb
@@ -2,13 +2,21 @@ class TrendingProjectsFinder
def execute(current_user, start_date = nil)
start_date ||= Date.today - 1.month
- projects = projects_for(current_user)
-
# Determine trending projects based on comments count
# for period of time - ex. month
- projects.joins(:notes).where('notes.created_at > ?', start_date).
- select("projects.*, count(notes.id) as ncount").
- group("projects.id").reorder("ncount DESC")
+ trending_project_ids = Note.
+ select("notes.project_id, count(notes.project_id) as pcount").
+ where('notes.created_at > ?', start_date).
+ group("project_id").
+ reorder("pcount DESC").
+ map(&:project_id)
+
+ sql_order_ids = trending_project_ids.reverse.
+ map { |project_id| "id = #{project_id}" }.join(", ")
+
+ # Get list of projects that user allowed to see
+ projects = projects_for(current_user)
+ projects.where(id: trending_project_ids).reorder(sql_order_ids)
end
private