From cca8795b01715b2dcf12139ce4805615674ceffb Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 15 Jun 2022 17:30:48 +0200 Subject: [PATCH] Allow anonymous questions to be returned when the user has disabled anonymous questions Fixes #267 --- app/controllers/ajax/answer_controller.rb | 4 ++-- app/models/inbox.rb | 7 ++++++- spec/controllers/ajax/answer_controller_spec.rb | 6 ++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb index cc393dad..7a283e7b 100644 --- a/app/controllers/ajax/answer_controller.rb +++ b/app/controllers/ajax/answer_controller.rb @@ -66,8 +66,8 @@ class Ajax::AnswerController < AjaxController end if answer.user == current_user - Inbox.create!(user: answer.user, question: answer.question, new: true) - end # TODO: decide what happens with the question + Inbox.create!(user: answer.user, question: answer.question, new: true, returning: true) + end answer.destroy @response[:status] = :okay diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 6d14ecb7..4d863686 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -2,8 +2,13 @@ class Inbox < ApplicationRecord belongs_to :user belongs_to :question + attr_accessor :returning + before_create do - raise "User does not want to receive anonymous questions" if self.question.author_is_anonymous and self.question.author_name != 'justask' and !self.user.privacy_allow_anonymous_questions? + raise "User does not want to receive anonymous questions" if !returning && + question.author_is_anonymous && + (question.author_name != "justask") && + !user.privacy_allow_anonymous_questions? end def answer(answer_content, user) diff --git a/spec/controllers/ajax/answer_controller_spec.rb b/spec/controllers/ajax/answer_controller_spec.rb index 2fdce45a..fc5abe9a 100644 --- a/spec/controllers/ajax/answer_controller_spec.rb +++ b/spec/controllers/ajax/answer_controller_spec.rb @@ -268,6 +268,12 @@ describe Ajax::AnswerController, :ajax_controller, type: :controller do it "returns the question back to the user's inbox" do expect { subject }.to(change { Inbox.where(question_id: answer.question.id, user_id: user.id).count }.by(1)) end + + it "returns the question back to the user's inbox when the user has anonymous questions disabled" do + user.privacy_allow_anonymous_questions = false + user.save + expect { subject }.to(change { Inbox.where(question_id: answer.question.id, user_id: user.id).count }.by(1)) + end end context "when the answer exists and was not made by the current user" do