From 87c2ed3ab706d4a6efb5b47c49b0dd4d111b2e17 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 17 Aug 2022 23:19:47 +0200 Subject: [PATCH] List direct questions for current users or mods --- app/controllers/user_controller.rb | 4 ++-- app/helpers/user_helper.rb | 4 ++++ spec/helpers/user_helper_spec.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 89b7171a..8287c90c 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -54,9 +54,9 @@ class UserController < ApplicationController def questions @title = 'Questions' @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first! - @questions = @user.cursored_questions(author_is_anonymous: false, direct: false, last_id: params[:last_id]) + @questions = @user.cursored_questions(author_is_anonymous: false, direct: belongs_to_current_user? || moderation_view?, last_id: params[:last_id]) @questions_last_id = @questions.map(&:id).min - @more_data_available = !@user.cursored_questions(author_is_anonymous: false, last_id: @questions_last_id, size: 1).count.zero? + @more_data_available = !@user.cursored_questions(author_is_anonymous: false, direct: belongs_to_current_user? || moderation_view?, last_id: @questions_last_id, size: 1).count.zero? respond_to do |format| format.html diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb index b0532faf..903017a5 100644 --- a/app/helpers/user_helper.rb +++ b/app/helpers/user_helper.rb @@ -21,6 +21,10 @@ module UserHelper current_user&.mod? && session[:moderation_view] == true end + def belongs_to_current_user? + @user == current_user + end + private def profile_link(user) diff --git a/spec/helpers/user_helper_spec.rb b/spec/helpers/user_helper_spec.rb index 1187070a..397d641b 100644 --- a/spec/helpers/user_helper_spec.rb +++ b/spec/helpers/user_helper_spec.rb @@ -149,4 +149,31 @@ describe UserHelper, type: :helper do end end end + + describe "#belongs_to_current_user?" do + context "user is logged in" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + @user = user + end + + it "returns the correct value" do + expect(helper.belongs_to_current_user?).to eq(true) + end + end + + context "user is not logged in" do + let(:user) { FactoryBot.create(:user) } + + before do + @user = user + end + + it "returns the correct value" do + expect(helper.belongs_to_current_user?).to eq(false) + end + end + end end