From df1832dc5a603fb3f0725b7ab51e9500424e9440 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 9 Dec 2023 14:05:42 +0100 Subject: [PATCH 1/2] Fix NameError in Discover --- app/controllers/discover_controller.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/discover_controller.rb b/app/controllers/discover_controller.rb index 05779d99..dfa8eaa7 100644 --- a/app/controllers/discover_controller.rb +++ b/app/controllers/discover_controller.rb @@ -6,17 +6,18 @@ class DiscoverController < ApplicationController return redirect_to root_path end - top_x = 10 # only display the top X items + top_x = 10 # only display the top X items + week_ago = Time.now.utc.ago(1.week) - @popular_answers = Answer.for_user(current_user).where("created_at > ?", Time.now.utc.ago(1.week)).order(:smile_count).reverse_order.limit(top_x).includes(:question, :user, :comments) - @most_discussed = Answer.for_user(current_user).where("created_at > ?", Time.now.utc.ago(1.week)).order(:comment_count).reverse_order.limit(top_x).includes(:question, :user, :comments) - @popular_questions = Question.where("created_at > ?", Time.now.ago(1.week)).order(:answer_count).reverse_order.limit(top_x).includes(:user) + @popular_answers = Answer.for_user(current_user).where("created_at > ?", week_ago).order(:smile_count).reverse_order.limit(top_x).includes(:question, :user, :comments) + @most_discussed = Answer.for_user(current_user).where("created_at > ?", week_ago).order(:comment_count).reverse_order.limit(top_x).includes(:question, :user, :comments) + @popular_questions = Question.where("created_at > ?", week_ago).order(:answer_count).reverse_order.limit(top_x).includes(:user) @new_users = User.where("asked_count > 0").order(:id).reverse_order.limit(top_x).includes(:profile) # .user = the user # .question_count = how many questions did the user ask @users_with_most_questions = Question.select('user_id, COUNT(*) AS question_count'). - where("created_at > ?", Time.now.ago(1.week)). + where("created_at > ?", week_ago). where(author_is_anonymous: false). group(:user_id). order('question_count'). From 8589ebf2d713004a0a657dbafe35b145c911032b Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 9 Dec 2023 14:05:59 +0100 Subject: [PATCH 2/2] Reformat `DiscoverController` --- app/controllers/discover_controller.rb | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/controllers/discover_controller.rb b/app/controllers/discover_controller.rb index dfa8eaa7..5898875d 100644 --- a/app/controllers/discover_controller.rb +++ b/app/controllers/discover_controller.rb @@ -1,10 +1,10 @@ +# frozen_string_literal: true + class DiscoverController < ApplicationController before_action :authenticate_user! def index - unless APP_CONFIG.dig(:features, :discover, :enabled) || current_user.mod? - return redirect_to root_path - end + return redirect_to root_path unless APP_CONFIG.dig(:features, :discover, :enabled) || current_user.mod? top_x = 10 # only display the top X items week_ago = Time.now.utc.ago(1.week) @@ -16,19 +16,19 @@ class DiscoverController < ApplicationController # .user = the user # .question_count = how many questions did the user ask - @users_with_most_questions = Question.select('user_id, COUNT(*) AS question_count'). - where("created_at > ?", week_ago). - where(author_is_anonymous: false). - group(:user_id). - order('question_count'). - reverse_order.limit(top_x) + @users_with_most_questions = Question.select("user_id, COUNT(*) AS question_count") + .where("created_at > ?", week_ago) + .where(author_is_anonymous: false) + .group(:user_id) + .order("question_count") + .reverse_order.limit(top_x) # .user = the user # .answer_count = how many questions did the user answer - @users_with_most_answers = Answer.select('user_id, COUNT(*) AS answer_count'). - where("created_at > ?", week_ago). - group(:user_id). - order('answer_count'). - reverse_order.limit(top_x) + @users_with_most_answers = Answer.select("user_id, COUNT(*) AS answer_count") + .where("created_at > ?", week_ago) + .group(:user_id) + .order("answer_count") + .reverse_order.limit(top_x) end end