From 5e7042ad6ce8076a6c40d366f5f1116db91d8d90 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 11 Aug 2021 14:44:41 +0200 Subject: [PATCH 1/3] Show question link for answered questions with only one answer Closes #43 --- app/views/answerbox/_header.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/answerbox/_header.haml b/app/views/answerbox/_header.haml index b33387eb..e330a7a8 100644 --- a/app/views/answerbox/_header.haml +++ b/app/views/answerbox/_header.haml @@ -24,7 +24,7 @@ View in Kontrollzentrum %h6.text-muted.media-heading.answerbox__question-user = raw t('views.answerbox.asked', user: user_screen_name(a.question.user, anonymous: a.question.author_is_anonymous), time: time_tooltip(a.question)) - - if !a.question.author_is_anonymous && a.question.answer_count > 1 + - unless a.question.author_is_anonymous · %a{ href: show_user_question_path(a.question.user.screen_name, a.question.id) } = pluralize(a.question.answer_count, t('views.general.answer')) From 56786ebb3813157d108f9f95799bf30e17d3067e Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 11 Aug 2021 16:56:58 +0200 Subject: [PATCH 2/3] Add direct field to questions Co-authored-by: Georg Gadinger --- app/controllers/ajax/question_controller.rb | 3 +- app/views/answerbox/_header.haml | 2 +- .../20210811133004_add_direct_to_questions.rb | 28 +++++++++++++++++++ db/schema.rb | 3 +- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20210811133004_add_direct_to_questions.rb diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index ebe43d61..f3239ac0 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -32,7 +32,8 @@ class Ajax::QuestionController < AjaxController begin question = Question.create!(content: params[:question], author_is_anonymous: is_never_anonymous ? false : params[:anonymousQuestion], - user: current_user) + user: current_user, + direct: params[:rcpt] != 'followers') rescue ActiveRecord::RecordInvalid => e NewRelic::Agent.notice_error(e) @response[:status] = :rec_inv diff --git a/app/views/answerbox/_header.haml b/app/views/answerbox/_header.haml index e330a7a8..0e31058f 100644 --- a/app/views/answerbox/_header.haml +++ b/app/views/answerbox/_header.haml @@ -24,7 +24,7 @@ View in Kontrollzentrum %h6.text-muted.media-heading.answerbox__question-user = raw t('views.answerbox.asked', user: user_screen_name(a.question.user, anonymous: a.question.author_is_anonymous), time: time_tooltip(a.question)) - - unless a.question.author_is_anonymous + - if !a.question.author_is_anonymous && !a.question.direct · %a{ href: show_user_question_path(a.question.user.screen_name, a.question.id) } = pluralize(a.question.answer_count, t('views.general.answer')) diff --git a/db/migrate/20210811133004_add_direct_to_questions.rb b/db/migrate/20210811133004_add_direct_to_questions.rb new file mode 100644 index 00000000..092426b9 --- /dev/null +++ b/db/migrate/20210811133004_add_direct_to_questions.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class AddDirectToQuestions < ActiveRecord::Migration[5.2] + def change + add_column :questions, :direct, :boolean, null: false, default: false + + # default all legacy questions to direct + execute 'UPDATE questions SET direct = true;' + + # All questions where + # - the author is not 'justask' (generated questions), and + # - the question wasn't asked anonymously + # can be direct or not. This depends on if the question has more than one answer + execute " +UPDATE questions +SET direct = (NOT (questions.answer_count > 1)) +WHERE author_name <> 'justask' + OR NOT author_is_anonymous;" + + # All questions which exist in at least more than one inbox are not direct + execute " +UPDATE questions +SET direct = false +WHERE id IN ( + SELECT question_id FROM inboxes GROUP BY question_id HAVING count(question_id) > 1 +);" + end +end diff --git a/db/schema.rb b/db/schema.rb index 86012613..3f9aed55 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_11_01_155648) do +ActiveRecord::Schema.define(version: 2021_08_11_133004) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -125,6 +125,7 @@ ActiveRecord::Schema.define(version: 2020_11_01_155648) do t.datetime "created_at" t.datetime "updated_at" t.integer "answer_count", default: 0, null: false + t.boolean "direct", default: false, null: false t.index ["user_id", "created_at"], name: "index_questions_on_user_id_and_created_at" end From 6d490453686a90a656f5a6e9db230fe6fe37f907 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 12 Aug 2021 00:19:36 +0200 Subject: [PATCH 3/3] Test if direct flag is being assigned correctly when a question is created --- spec/controllers/ajax/question_controller_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/controllers/ajax/question_controller_spec.rb b/spec/controllers/ajax/question_controller_spec.rb index c722cdac..6055b886 100644 --- a/spec/controllers/ajax/question_controller_spec.rb +++ b/spec/controllers/ajax/question_controller_spec.rb @@ -10,6 +10,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do expect(Question.last.content).to eq(question_content) expect(Question.last.author_is_anonymous).to be(expected_question_anonymous) expect(Question.last.user).to eq(expected_question_user) + expect(Question.last.direct).to eq(expected_question_direct) end if check_for_inbox @@ -85,6 +86,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when user allows anonymous questions" do let(:user_allows_anonymous_questions) { true } + let(:expected_question_direct) { true } context "when anonymousQuestion is true" do let(:anonymous_question) { "true" } @@ -120,6 +122,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when anonymousQuestion is false" do let(:anonymous_question) { "false" } let(:expected_question_anonymous) { false } + let(:expected_question_direct) { true } include_examples "creates the question" end @@ -128,6 +131,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when rcpt is followers" do let(:rcpt) { "followers" } + let(:expected_question_direct) { false } context "when anonymousQuestion is true" do let(:anonymous_question) { "true" } @@ -182,6 +186,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when user allows anonymous questions" do let(:user_allows_anonymous_questions) { true } + let(:expected_question_direct) { true } include_examples "creates the question"