Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h3>{{i18n "category.qa_settings_heading"}}</h3>
<section class="field">
<label>
<Input @type="checkbox" @checked={{this.category.custom_fields.create_as_qa_default}} />
{{i18n "category.create_as_qa_default"}}
</label>
</section>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import I18n from "I18n";
import { withPluginApi } from "discourse/lib/plugin-api";
import { CREATE_TOPIC } from "discourse/models/composer";
import { observes } from "discourse-common/utils/decorators";

export default {
name: "extend-composer-actions",
Expand Down Expand Up @@ -75,6 +76,21 @@ export default {
return [];
}
});

api.modifyClass("model:composer", {
pluginId: "discourse-question-answer",

@observes("categoryId")
categoryCreateAsQADefault() {
const createAsQA = this.category?.create_as_qa_default;

if (this.creatingTopic && createAsQA !== this.createAsQA) {
this.set("createAsQA", createAsQA);
this.notifyPropertyChange("replyOptions");
this.notifyPropertyChange("action");
}
},
});
});
},
};
5 changes: 5 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ en:
question_answer_user_commented: "new comment"
popup:
question_answer_user_commented: '%{username} commented in "%{topic}" - %{site_title}'

category:
qa_settings_heading: Question Answer
create_as_qa_default: New topics default to Q&A in this category.

composer:
create_qa:
label: "Create Q&A topic"
Expand Down
2 changes: 2 additions & 0 deletions lib/question_answer/engine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

module ::QuestionAnswer
CREATE_AS_QA_DEFAULT = "create_as_qa_default"

class Engine < Rails::Engine
engine_name 'question_answer'
isolate_namespace QuestionAnswer
Expand Down
9 changes: 9 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,13 @@

false
end

register_category_custom_field_type(QuestionAnswer::CREATE_AS_QA_DEFAULT, :boolean)
if Site.respond_to? :preloaded_category_custom_fields
Site.preloaded_category_custom_fields << QuestionAnswer::CREATE_AS_QA_DEFAULT
end
add_to_class(:category, :create_as_qa_default) do
ActiveModel::Type::Boolean.new.cast(self.custom_fields[QuestionAnswer::CREATE_AS_QA_DEFAULT])
end
add_to_serializer(:basic_category, :create_as_qa_default) { object.create_as_qa_default }
end
37 changes: 37 additions & 0 deletions spec/serializers/question_answer/basic_category_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'rails_helper'

describe BasicCategorySerializer do
fab!(:category) { Fabricate(:category) }

let(:serialized) do
serializer = BasicCategorySerializer.new(category, root: false)
serializer.as_json
end

before do
category.custom_fields[QuestionAnswer::CREATE_AS_QA_DEFAULT] = true
category.save_custom_fields(true)
end

context 'qa enabled' do
before do
SiteSetting.qa_enabled = true
end

it 'should return qa category attributes' do
expect(serialized[:create_as_qa_default]).to eq(true)
end
end

context 'qa disabled' do
before do
SiteSetting.qa_enabled = false
end

it 'should not return qa category attributes' do
expect(serialized.key?(:create_as_qa_default)).to eq(false)
end
end
end
22 changes: 22 additions & 0 deletions test/javascripts/acceptance/composer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import selectKit from "discourse/tests/helpers/select-kit-helper";
import { test } from "qunit";
import I18n from "I18n";
import { parsePostData } from "discourse/tests/helpers/create-pretender";
import Category from "discourse/models/category";

let createAsQASetInRequest = false;

Expand Down Expand Up @@ -76,4 +77,25 @@ acceptance("Discourse Question Answer - composer", function (needs) {
"submits the right request to create topic as Q&A formatted"
);
});

test("Creating new topic in category with Q&A create default", async function (assert) {
Category.findById(2).set("create_as_qa_default", true);

await visit("/");
await click("#create-topic");

assert.strictEqual(
query(".action-title").innerText.trim(),
I18n.t("topic.create_long")
);

const categoryChooser = selectKit(".category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(2);

assert.strictEqual(
query(".action-title").innerText.trim(),
I18n.t("composer.create_qa.label")
);
});
});