From 3524809e21de73e1b1413d5745a4f5359c8a0839 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Tue, 27 Dec 2022 22:06:24 +0000 Subject: [PATCH] fix display conditions of profile questions --- app/controllers/user_controller.rb | 15 ++++++++++-- spec/controllers/user_controller_spec.rb | 29 ++++++++++++++++-------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index f71e41dc..79be3630 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -53,9 +53,10 @@ class UserController < ApplicationController def questions @title = "Questions" - @questions = @user.cursored_questions(author_is_anonymous: false, direct: belongs_to_current_user? || moderation_view?, last_id: params[:last_id]) + + @questions = @user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: params[:last_id]) @questions_last_id = @questions.map(&:id).min - @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? + @more_data_available = !@user.cursored_questions(author_is_anonymous: false, direct: direct_param, last_id: @questions_last_id, size: 1).count.zero? respond_to do |format| format.html @@ -75,5 +76,15 @@ class UserController < ApplicationController redirect_to user_path(@user) end + def direct_param + # return `nil` instead of `false` so we retrieve all questions for the user, direct or not. + # `cursored_questions` will then remove the `direct` field from the WHERE query. otherwise the query filters + # for `WHERE direct = false` ... + return if belongs_to_current_user? || moderation_view? + + # page is not being viewed by the current user, and we're not in the moderation view -> only show public questions + false + end + def belongs_to_current_user? = @user == current_user end diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index aa898073..b1d35574 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -98,7 +98,10 @@ describe UserController, type: :controller do end describe "#questions" do - let!(:question) { FactoryBot.create(:question, user:, direct: true, author_is_anonymous: false) } + let!(:question_anyone) { FactoryBot.create(:question, user:, direct: false, author_is_anonymous: false) } + let!(:question_direct) { FactoryBot.create(:question, user:, direct: true, author_is_anonymous: false) } + let!(:question_direct_anon) { FactoryBot.create(:question, user:, direct: true, author_is_anonymous: true) } + subject { get :questions, params: { username: user.screen_name } } it "renders the user/questions template" do @@ -112,9 +115,11 @@ describe UserController, type: :controller do sign_in user end - it "renders all questions" do + it "contains all non-anon questions" do subject - expect(assigns(:questions).size).to eq(1) + expect(assigns(:questions)).to include(question_anyone, question_direct) + expect(assigns(:questions)).not_to include(question_direct_anon) + expect(assigns(:questions).size).to eq(2) end end @@ -125,9 +130,11 @@ describe UserController, type: :controller do sign_in another_user end - it "renders no questions" do + it "contains all non-direct non-anon questions" do subject - expect(assigns(:questions).size).to eq(0) + expect(assigns(:questions)).to include(question_anyone) + expect(assigns(:questions)).not_to include(question_direct, question_direct_anon) + expect(assigns(:questions).size).to eq(1) end end @@ -139,16 +146,20 @@ describe UserController, type: :controller do allow_any_instance_of(UserHelper).to receive(:moderation_view?) { true } end - it "contains all questions" do + it "contains all non-anon questions" do subject - expect(assigns(:questions).size).to eq(1) + expect(assigns(:questions)).to include(question_anyone, question_direct) + expect(assigns(:questions)).not_to include(question_direct_anon) + expect(assigns(:questions).size).to eq(2) end end context "when user is not signed in" do - it "contains no questions" do + it "contains all non-direct non-anon questions" do subject - expect(assigns(:questions).size).to eq(0) + expect(assigns(:questions)).to include(question_anyone) + expect(assigns(:questions)).not_to include(question_direct, question_direct_anon) + expect(assigns(:questions).size).to eq(1) end end end