fix display conditions of profile questions

This commit is contained in:
Georg Gadinger 2022-12-27 22:06:24 +00:00
parent 712328bd86
commit 3524809e21
2 changed files with 33 additions and 11 deletions

View File

@ -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

View File

@ -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