From c894551ffcf327ffbd2a68a281eda9207fe8d466 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 21:44:46 +0200 Subject: [PATCH 1/9] Add Rails Admin link to question partials --- app/views/question/_question.haml | 4 ++++ app/views/shared/_question.haml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/app/views/question/_question.haml b/app/views/question/_question.haml index 924570d9..3452c2d7 100644 --- a/app/views/question/_question.haml +++ b/app/views/question/_question.haml @@ -25,6 +25,10 @@ %a.dropdown-item{ href: '#', tabindex: -1, data: { action: 'ab-question-report', q_id: question.id } } %i.fa.fa-exclamation-triangle = t 'views.actions.report' + - if current_user.has_role? :administrator + %a.dropdown-item{ href: rails_admin_path_for_resource(question) } + %i.fa.fa-gears + = t("voc.view_in_rails_admin") %h6.text-muted.media-heading.answerbox__question-user - identifier = question.author_is_anonymous ? question.author_identifier : nil - if hidden diff --git a/app/views/shared/_question.haml b/app/views/shared/_question.haml index 9ead4027..48ed386b 100644 --- a/app/views/shared/_question.haml +++ b/app/views/shared/_question.haml @@ -21,6 +21,10 @@ %a.dropdown-item{ href: '#', tabindex: -1, data: { action: 'ab-question-report', q_id: q.id } } %i.fa.fa-exclamation-triangle = t 'views.actions.report' + - if current_user.has_role? :administrator + %a.dropdown-item{ href: rails_admin_path_for_resource(q) } + %i.fa.fa-gears + = t("voc.view_in_rails_admin") %h6.media-heading.text-muted.answerbox__question-user = raw t('views.answerbox.asked', user: user_screen_name(q.user), time: time_tooltip(q)) - if q.answer_count > 1 From 70e95375fbc4d56e55c9fa1da783ba6a359bcbb4 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 21:55:31 +0200 Subject: [PATCH 2/9] Render questions on user profiles with markdown --- app/views/shared/_question.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/_question.haml b/app/views/shared/_question.haml index 48ed386b..61d0b356 100644 --- a/app/views/shared/_question.haml +++ b/app/views/shared/_question.haml @@ -31,5 +31,5 @@ ยท %a{ href: question_path(q.user.screen_name, q.id) } = pluralize(q.answer_count, t('views.general.answer')) - %p.answerbox__question-text - = q.content + .answerbox__question-text + = question_markdown q.content From 83dbd5d2e3782dd7397cd536b29f5a2aae5399f4 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 21:56:03 +0200 Subject: [PATCH 3/9] Don't list direct questions on user profiles --- app/controllers/user_controller.rb | 2 +- app/models/user/question_methods.rb | 4 ++-- lib/use_case/question/create.rb | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 91b557d1..89b7171a 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -54,7 +54,7 @@ class UserController < ApplicationController def questions @title = 'Questions' @user = User.where('LOWER(screen_name) = ?', params[:username].downcase).includes(:profile).first! - @questions = @user.cursored_questions(author_is_anonymous: false, last_id: params[:last_id]) + @questions = @user.cursored_questions(author_is_anonymous: false, direct: false, last_id: params[:last_id]) @questions_last_id = @questions.map(&:id).min @more_data_available = !@user.cursored_questions(author_is_anonymous: false, last_id: @questions_last_id, size: 1).count.zero? diff --git a/app/models/user/question_methods.rb b/app/models/user/question_methods.rb index 468e984a..1c52f660 100644 --- a/app/models/user/question_methods.rb +++ b/app/models/user/question_methods.rb @@ -5,9 +5,9 @@ module User::QuestionMethods define_cursor_paginator :cursored_questions, :ordered_questions - def ordered_questions(author_is_anonymous: nil) + def ordered_questions(author_is_anonymous: nil, direct: nil) questions - .where({ author_is_anonymous: author_is_anonymous }.compact) + .where({ author_is_anonymous: author_is_anonymous, direct: direct }.compact) .order(:created_at) .reverse_order end diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index afdfb03c..6d20ff20 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -20,7 +20,8 @@ module UseCase content: content, author_is_anonymous: anonymous, author_identifier: author_identifier, - user: source_user_id.nil? ? nil : source_user + user: source_user_id.nil? ? nil : source_user, + direct: true ) return if filtered?(question) From 777b25f967b5bac637ec38238c9ea1f3f066cac9 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 21:59:55 +0200 Subject: [PATCH 4/9] Add test for question create UseCase checking if direct is set --- spec/lib/use_case/question/create_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/lib/use_case/question/create_spec.rb b/spec/lib/use_case/question/create_spec.rb index ef3190b5..c3e69bf8 100644 --- a/spec/lib/use_case/question/create_spec.rb +++ b/spec/lib/use_case/question/create_spec.rb @@ -22,6 +22,7 @@ describe UseCase::Question::Create do expect(question.content).to eq(content) expect(question.author_is_anonymous).to eq(anonymous) expect(question.author_identifier).to eq(author_identifier) + expect(question.direct).to eq(true) if should_send_to_inbox expect(target_user.inboxes.first.question_id).to eq(question.id) From bea98fa9170b0379022b485af095ecdf81aa7974 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 22:24:24 +0200 Subject: [PATCH 5/9] Always set follower created questions to not be direct explicitly --- lib/use_case/question/create_followers.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/use_case/question/create_followers.rb b/lib/use_case/question/create_followers.rb index fffc3f23..23695a74 100644 --- a/lib/use_case/question/create_followers.rb +++ b/lib/use_case/question/create_followers.rb @@ -14,7 +14,8 @@ module UseCase content: content, author_is_anonymous: false, author_identifier: author_identifier, - user: source_user + user: source_user, + direct: false ) increment_asked_count From 269278351814546fbee19e2054643d673038afce Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 22:24:48 +0200 Subject: [PATCH 6/9] Turn direct setting into an option for the use case --- lib/use_case/question/create.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 6d20ff20..8adb1e7d 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -11,6 +11,7 @@ module UseCase option :content, type: Types::Coercible::String option :anonymous, type: Types::Params::Bool, default: proc { false } option :author_identifier, type: Types::Coercible::String | Types::Nil + option :direct, type: Types::Params::Bool, default: proc { true } def call check_anonymous_rules @@ -21,7 +22,7 @@ module UseCase author_is_anonymous: anonymous, author_identifier: author_identifier, user: source_user_id.nil? ? nil : source_user, - direct: true + direct: direct ) return if filtered?(question) @@ -56,7 +57,7 @@ module UseCase end def increment_asked_count - unless source_user_id && !anonymous + unless source_user_id && !anonymous && !direct # Only increment the asked count of the source user if the question # is not anonymous, and we actually have a source user return From 4f4e6c83a1b9286864714d5ff5e57349346100e9 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 22:25:13 +0200 Subject: [PATCH 7/9] Do not decrement the question count if the question is direct --- app/models/question.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/question.rb b/app/models/question.rb index 2e0fcffb..f9b8e4c3 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -17,7 +17,7 @@ class Question < ApplicationRecord end # rubocop:disable Rails/SkipsModelValidations - user&.decrement! :asked_count unless author_is_anonymous + user&.decrement! :asked_count unless author_is_anonymous || direct # rubocop:enable Rails/SkipsModelValidations end From bdb637047bfca347f6b193556a2f5f62a67ebd90 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 22:27:15 +0200 Subject: [PATCH 8/9] Omit hash value in question methods --- app/models/user/question_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user/question_methods.rb b/app/models/user/question_methods.rb index 1c52f660..0ff0db4f 100644 --- a/app/models/user/question_methods.rb +++ b/app/models/user/question_methods.rb @@ -7,7 +7,7 @@ module User::QuestionMethods def ordered_questions(author_is_anonymous: nil, direct: nil) questions - .where({ author_is_anonymous: author_is_anonymous, direct: direct }.compact) + .where({ author_is_anonymous:, direct: }.compact) .order(:created_at) .reverse_order end From ed69d84d48af1ff7e4ea1fa8bce5bb459a5dbb7b Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 24 Jul 2022 22:45:04 +0200 Subject: [PATCH 9/9] Apply review suggestion from @nilsding Co-authored-by: Georg Gadinger --- lib/use_case/question/create.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/use_case/question/create.rb b/lib/use_case/question/create.rb index 8adb1e7d..1904f6f6 100644 --- a/lib/use_case/question/create.rb +++ b/lib/use_case/question/create.rb @@ -59,7 +59,7 @@ module UseCase def increment_asked_count unless source_user_id && !anonymous && !direct # Only increment the asked count of the source user if the question - # is not anonymous, and we actually have a source user + # is not anonymous, and is not direct, and we actually have a source user return end