Merge pull request #886 from Retrospring/bugfix/questionable-user-questions

fix display conditions of profile questions
This commit is contained in:
Georg Gadinger 2022-12-27 23:15:58 +00:00 committed by GitHub
commit 447d1f7e6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 11 deletions

View File

@ -53,9 +53,10 @@ class UserController < ApplicationController
def questions def questions
@title = "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 @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| respond_to do |format|
format.html format.html
@ -75,5 +76,15 @@ class UserController < ApplicationController
redirect_to user_path(@user) redirect_to user_path(@user)
end 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 def belongs_to_current_user? = @user == current_user
end end

View File

@ -98,7 +98,10 @@ describe UserController, type: :controller do
end end
describe "#questions" do 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 } } subject { get :questions, params: { username: user.screen_name } }
it "renders the user/questions template" do it "renders the user/questions template" do
@ -112,9 +115,11 @@ describe UserController, type: :controller do
sign_in user sign_in user
end end
it "renders all questions" do it "contains all non-anon questions" do
subject 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
end end
@ -125,9 +130,11 @@ describe UserController, type: :controller do
sign_in another_user sign_in another_user
end end
it "renders no questions" do it "contains all non-direct non-anon questions" do
subject 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 end
@ -139,16 +146,20 @@ describe UserController, type: :controller do
allow_any_instance_of(UserHelper).to receive(:moderation_view?) { true } allow_any_instance_of(UserHelper).to receive(:moderation_view?) { true }
end end
it "contains all questions" do it "contains all non-anon questions" do
subject 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
end end
context "when user is not signed in" do context "when user is not signed in" do
it "contains no questions" do it "contains all non-direct non-anon questions" do
subject 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 end
end end