Retrospring/app/controllers/discover_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
1.8 KiB
Ruby
Raw Normal View History

2023-12-09 05:05:59 -08:00
# frozen_string_literal: true
2015-05-13 11:39:04 -07:00
class DiscoverController < ApplicationController
2020-04-18 16:45:50 -07:00
before_action :authenticate_user!
def index
2023-12-09 05:05:59 -08:00
return redirect_to root_path unless APP_CONFIG.dig(:features, :discover, :enabled) || current_user.mod?
2020-04-19 04:55:13 -07:00
2023-12-09 05:05:42 -08:00
top_x = 10 # only display the top X items
week_ago = Time.now.utc.ago(1.week)
2023-12-09 05:05:42 -08:00
@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)
2015-05-13 12:58:00 -07:00
# .user = the user
# .question_count = how many questions did the user ask
2023-12-09 05:05:59 -08:00
@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
2023-12-09 05:05:59 -08:00
@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
2015-05-13 11:39:04 -07:00
end