Skip to content

A platform for community discussion. Free, open, simple.

FIX: Remove topic voting custom field from category logs when there is no change (#36832)

Natalie Tay105aa3213a04
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings.gjs
index f7ff3234d5b..13cf7963f71 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings.gjs
@@ -1,23 +1,17 @@
-/* eslint-disable ember/no-classic-components */
-import Component, { Input } from "@ember/component";
-import { classNames, tagName } from "@ember-decorators/component";
+import { Input } from "@ember/component";
import { i18n } from "discourse-i18n";
-@tagName("")
-@classNames("category-custom-settings-outlet", "feature-voting-settings")
-export default class FeatureVotingSettings extends Component {
- <template>
+<template>
+ <section class="field">
<h3>{{i18n "topic_voting.title"}}</h3>
- <section class="field">
- <div class="enable-topic-voting">
- <label class="checkbox-label">
- <Input
- @type="checkbox"
- @checked={{this.category.custom_fields.enable_topic_voting}}
- />
- {{i18n "topic_voting.allow_topic_voting"}}
- </label>
- </div>
- </section>
- </template>
-}
+ <div class="enable-topic-voting">
+ <label class="checkbox-label">
+ <Input
+ @type="checkbox"
+ @checked={{@outletArgs.category.custom_fields.enable_topic_voting}}
+ />
+ {{i18n "topic_voting.allow_topic_voting"}}
+ </label>
+ </div>
+ </section>
+</template>
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js b/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
index 4dcfb238d9d..959e8eb38ac 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
@@ -1,8 +1,17 @@
import { tracked } from "@glimmer/tracking";
+import { computed, get } from "@ember/object";
import { withPluginApi } from "discourse/lib/plugin-api";
+import Category from "discourse/models/category";
import { i18n } from "discourse-i18n";
function initialize(api) {
+ Category.reopen({
+ enable_topic_voting: computed("custom_fields.enable_topic_voting", {
+ get() {
+ return get(this.custom_fields, "enable_topic_voting") === true;
+ },
+ }),
+ });
api.addPostClassesCallback((post) => {
if (post.post_number === 1 && post.can_vote) {
return ["voting-post"];
diff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rb
index da3036f2047..f9ad943847d 100755
--- a/plugins/discourse-topic-voting/plugin.rb
+++ b/plugins/discourse-topic-voting/plugin.rb
@@ -22,6 +22,7 @@ Discourse.anonymous_filters.push(:votes)
module ::DiscourseTopicVoting
PLUGIN_NAME = "discourse-topic-voting"
+ ENABLE_TOPIC_VOTING_SETTING = "enable_topic_voting"
end
require_relative "lib/discourse_topic_voting/engine"
@@ -80,15 +81,8 @@ after_initialize do
result
end
- register_category_custom_field_type("enable_topic_voting", :boolean)
- add_to_serializer(:category, :custom_fields, respect_plugin_enabled: false) do
- return object.custom_fields if !SiteSetting.topic_voting_enabled
-
- object.custom_fields.merge(
- enable_topic_voting:
- DiscourseTopicVoting::CategorySetting.find_by(category_id: object.id).present?,
- )
- end
+ register_category_custom_field_type(DiscourseTopicVoting::ENABLE_TOPIC_VOTING_SETTING, :boolean)
+ register_preloaded_category_custom_fields DiscourseTopicVoting::ENABLE_TOPIC_VOTING_SETTING
add_to_serializer(:topic_list_item, :vote_count, include_condition: -> { object.can_vote? }) do
object.vote_count
diff --git a/plugins/discourse-topic-voting/spec/serializers/category_serializer_spec.rb b/plugins/discourse-topic-voting/spec/serializers/category_serializer_spec.rb
index 002442ae581..903399cb6fb 100644
--- a/plugins/discourse-topic-voting/spec/serializers/category_serializer_spec.rb
+++ b/plugins/discourse-topic-voting/spec/serializers/category_serializer_spec.rb
@@ -3,7 +3,7 @@
describe CategorySerializer do
fab!(:category)
- it "does not return enable_topic_voting voting disabled" do
+ it "does not return enable_topic_voting when voting disabled" do
SiteSetting.topic_voting_enabled = false
json = CategorySerializer.new(category, root: false).as_json
@@ -13,9 +13,11 @@ describe CategorySerializer do
it "returns enable_topic_voting when voting enabled" do
SiteSetting.topic_voting_enabled = true
+ category.custom_fields["enable_topic_voting"] = "true"
+ category.save_custom_fields
json = CategorySerializer.new(category, root: false).as_json
- expect(json[:custom_fields]).to eq({ "enable_topic_voting" => false })
+ expect(json[:custom_fields]).to eq({ "enable_topic_voting" => true })
end
end
diff --git a/plugins/discourse-topic-voting/test/javascripts/acceptance/category-edit-test.js b/plugins/discourse-topic-voting/test/javascripts/acceptance/category-edit-test.js
new file mode 100644
index 00000000000..1c2154ba5b2
--- /dev/null
+++ b/plugins/discourse-topic-voting/test/javascripts/acceptance/category-edit-test.js
@@ -0,0 +1,58 @@
+import { click, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import pretender from "discourse/tests/helpers/create-pretender";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("Category Edit | Topic Voting", function (needs) {
+ needs.user();
+ needs.settings({ topic_voting_enabled: true });
+
+ test("Enabling topic voting sends boolean true", async function (assert) {
+ await visit("/c/bug/edit/settings");
+ await click(".enable-topic-voting input[type='checkbox']");
+ await click("#save-category");
+
+ const payload = JSON.parse(
+ pretender.handledRequests[pretender.handledRequests.length - 1]
+ .requestBody
+ );
+ assert.true(
+ payload.custom_fields.enable_topic_voting,
+ "enable_topic_voting should be boolean true"
+ );
+ });
+
+ test("Disabling topic voting sends boolean false", async function (assert) {
+ await visit("/c/bug/edit/settings");
+
+ // twice to toggle false
+ await click(".enable-topic-voting input[type='checkbox']");
+ await click(".enable-topic-voting input[type='checkbox']");
+
+ await click("#save-category");
+
+ const payload = JSON.parse(
+ pretender.handledRequests[pretender.handledRequests.length - 1]
+ .requestBody
+ );
+ assert.false(
+ payload.custom_fields.enable_topic_voting,
+ "enable_topic_voting should be boolean false"
+ );
+ });
+
+ test("Modifying other settings does not include enable_topic_voting", async function (assert) {
+ await visit("/c/bug/edit/settings");
+ await click("#save-category");
+
+ const payload = JSON.parse(
+ pretender.handledRequests[pretender.handledRequests.length - 1]
+ .requestBody
+ );
+ assert.strictEqual(
+ payload.custom_fields?.enable_topic_voting,
+ undefined,
+ "enable_topic_voting should not be in payload when not modified"
+ );
+ });
+});