Skip to content

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

FEATURE: Add "Ideas" category type for Topic Voting plugin (#38794)

Régis HANOL535d343ee2cb
diff --git a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
index 9ac8ea0d9a1..2785b69007d 100644
--- a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
+++ b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
@@ -130,6 +130,28 @@
.edit-category-type-schema-fields {
display: contents;
+
+ &.--site-settings-only > .form-kit__section.emphasis {
+ border: none;
+ padding: 0;
+ }
+
+ .--dependent {
+ display: flex;
+ gap: 0.75em;
+ }
+
+ .--dependent-border {
+ flex: 0 0 1lh;
+ border-right: 3px solid var(--primary-low);
+ }
+
+ .--dependent-fields {
+ flex: 1;
+ display: flex;
+ flex-direction: column;
+ gap: 1em;
+ }
}
}
diff --git a/app/services/categories/types/base.rb b/app/services/categories/types/base.rb
index 4c7f173ce86..3c7e690ff19 100644
--- a/app/services/categories/types/base.rb
+++ b/app/services/categories/types/base.rb
@@ -235,7 +235,8 @@ module Categories
# This SHOULD NOT be overridden by category types.
def configure_site_settings(category, guardian:, configuration_values: {})
category_type_settings =
- configuration_schema[:site_settings]&.map do |setting_name, default_value|
+ configuration_schema[:site_settings]&.map do |setting_name, config|
+ default_value = config.is_a?(Hash) ? config[:default] : config
{
setting_name: setting_name.to_s,
value: configuration_values.fetch(setting_name.to_s, default_value),
@@ -299,18 +300,31 @@ module Categories
end
schema[:site_settings]&.each do |setting_name, target_value|
+ if target_value.is_a?(Hash)
+ default = target_value[:default]
+ custom_label = target_value[:label]
+ else
+ default = target_value
+ custom_label = nil
+ end
+
meta = SiteSetting.setting_metadata_hash(setting_name)
- entries[:site_settings] << {
+ depends_on = SiteSetting.type_supervisor.dependencies[setting_name.to_sym]&.first&.to_s
+ entry = {
key: setting_name.to_s,
- default: target_value,
+ default:,
current: SiteSetting.public_send(setting_name),
type: meta[:type],
- label: meta[:humanized_name],
+ label: custom_label || meta[:humanized_name],
description: meta[:description],
required: false,
show_on_create: true,
show_on_edit: true,
}
+ entry[:depends_on] = depends_on if depends_on
+ entry[:min] = meta[:min] if meta[:min]
+ entry[:max] = meta[:max] if meta[:max]
+ entries[:site_settings] << entry
end
schema[:category_settings]&.each do |field_name, config|
diff --git a/frontend/discourse/app/components/edit-category-type-schema-fields.gjs b/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
index 65949d6ec00..506bd291833 100644
--- a/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
+++ b/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
@@ -1,4 +1,5 @@
import Component from "@glimmer/component";
+import { get } from "@ember/helper";
import RelativeTimePicker from "discourse/components/relative-time-picker";
import { bind } from "discourse/lib/decorators";
import { eq } from "discourse/truth-helpers";
@@ -51,7 +52,7 @@ const SchemaFormField = <template>
@type="input-number"
as |field|
>
- <field.Control />
+ <field.Control min={{@entry.min}} max={{@entry.max}} />
</@formObject.Field>
{{else}}
<@formObject.Field
@@ -78,6 +79,12 @@ export default class EditCategoryTypeSchemaFields extends Component {
);
}
+ get hasCustomFields() {
+ return this.schema.category_custom_fields?.some((entry) =>
+ this.shouldDisplayField(entry)
+ );
+ }
+
get className() {
let classes = [
"edit-category-type-schema-fields",
@@ -86,9 +93,31 @@ export default class EditCategoryTypeSchemaFields extends Component {
if (this.args.active) {
classes.push("active");
}
+ if (!this.hasCustomFields) {
+ classes.push("--site-settings-only");
+ }
return classes.join(" ");
}
+ @bind
+ groupEntries(entries) {
+ const groups = [];
+ const byDependsOn = new Map();
+
+ entries?.forEach((entry) => {
+ const dependsOn = entry.depends_on || null;
+ let group = byDependsOn.get(dependsOn);
+ if (!group) {
+ group = { dependsOn, entries: [] };
+ byDependsOn.set(dependsOn, group);
+ groups.push(group);
+ }
+ group.entries.push(entry);
+ });
+
+ return groups;
+ }
+
@bind
shouldDisplayField(entry) {
if (this.args.category.isCreated) {
@@ -100,19 +129,21 @@ export default class EditCategoryTypeSchemaFields extends Component {
<template>
<div class={{this.className}}>
- <@form.Section>
- <@form.Object @name="custom_fields" as |customFields|>
- {{#each this.schema.category_custom_fields as |entry|}}
- {{#if (this.shouldDisplayField entry)}}
- <SchemaFormField
- @category={{@category}}
- @entry={{entry}}
- @formObject={{customFields}}
- />
- {{/if}}
- {{/each}}
- </@form.Object>
- </@form.Section>
+ {{#if this.hasCustomFields}}
+ <@form.Section>
+ <@form.Object @name="custom_fields" as |customFields|>
+ {{#each this.schema.category_custom_fields as |entry|}}
+ {{#if (this.shouldDisplayField entry)}}
+ <SchemaFormField
+ @category={{@category}}
+ @entry={{entry}}
+ @formObject={{customFields}}
+ />
+ {{/if}}
+ {{/each}}
+ </@form.Object>
+ </@form.Section>
+ {{/if}}
{{yield to="beforeSiteSettings"}}
@@ -120,13 +151,35 @@ export default class EditCategoryTypeSchemaFields extends Component {
@title={{i18n "category.type_settings_schema.site_settings"}}
@subtitle={{i18n "category.settings_apply_to_all_of_type_warning"}}
>
- <@form.Object @name="category_type_site_settings" as |siteSettings|>
- {{#each this.schema.site_settings as |entry|}}
- <SchemaFormField
- @category={{@category}}
- @entry={{entry}}
- @formObject={{siteSettings}}
- />
+ <@form.Object
+ @name="category_type_site_settings"
+ as |siteSettings data|
+ >
+ {{#each (this.groupEntries this.schema.site_settings) as |group|}}
+ {{#if group.dependsOn}}
+ {{#if (get data group.dependsOn)}}
+ <div class="--dependent">
+ <div class="--dependent-border"></div>
+ <div class="--dependent-fields">
+ {{#each group.entries as |entry|}}
+ <SchemaFormField
+ @category={{@category}}
+ @entry={{entry}}
+ @formObject={{siteSettings}}
+ />
+ {{/each}}
+ </div>
+ </div>
+ {{/if}}
+ {{else}}
+ {{#each group.entries as |entry|}}
+ <SchemaFormField
+ @category={{@category}}
+ @entry={{entry}}
+ @formObject={{siteSettings}}
+ />
+ {{/each}}
+ {{/if}}
{{/each}}
</@form.Object>
</@form.Emphasis>
diff --git a/plugins/discourse-topic-voting/app/services/discourse_topic_voting/categories/types/ideas.rb b/plugins/discourse-topic-voting/app/services/discourse_topic_voting/categories/types/ideas.rb
new file mode 100644
index 00000000000..3543f12a095
--- /dev/null
+++ b/plugins/discourse-topic-voting/app/services/discourse_topic_voting/categories/types/ideas.rb
@@ -0,0 +1,96 @@
+# frozen_string_literal: true
+
+module DiscourseTopicVoting
+ module Categories
+ module Types
+ class Ideas < ::Categories::Types::Base
+ type_id :ideas
+
+ class << self
+ def visible?
+ SiteSetting.enable_ideas_category_type_setup
+ end
+
+ def enable_plugin
+ SiteSetting.topic_voting_enabled = true
+ end
+
+ def category_matches?(category)
+ Category.can_vote?(category.id)
+ end
+
+ def find_matches
+ Category.where(id: DiscourseTopicVoting::CategorySetting.select(:category_id))
+ end
+
+ def configure_category(category, guardian:, configuration_values: {})
+ category.discourse_topic_voting_category_setting ||=
+ DiscourseTopicVoting::CategorySetting.new(category: category)
+ category.discourse_topic_voting_category_setting.save!
+ end
+
+ def configuration_schema
+ {
+ general_category_settings: {
+ name: {
+ default: I18n.t("category_types.ideas.name"),
+ type: :string,
+ },
+ style_type: {
+ default: "emoji",
+ type: :string,
+ },
+ emoji: {
+ default: "bulb",
+ type: :string,
+ },
+ },
+ site_settings: {
+ topic_voting_show_who_voted: {
+ default: true,
+ label: I18n.t("topic_voting.category_type.show_who_voted.label"),
+ },
+ topic_voting_show_votes_on_profile: {
+ default: true,
+ label: I18n.t("topic_voting.category_type.show_votes_on_profile.label"),
+ },
+ topic_voting_enable_vote_limits: {
+ default: true,
+ label: I18n.t("topic_voting.category_type.enable_vote_limits.label"),
+ },
+ topic_voting_tl0_vote_limit: {
+ default: 2,
+ label: I18n.t("topic_voting.category_type.tl0_vote_limit.label"),
+ },
+ topic_voting_tl1_vote_limit: {
+ default: 4,
+ label: I18n.t("topic_voting.category_type.tl1_vote_limit.label"),
+ },
+ topic_voting_tl2_vote_limit: {
+ default: 6,
+ label: I18n.t("topic_voting.category_type.tl2_vote_limit.label"),
+ },
+ topic_voting_tl3_vote_limit: {
+ default: 8,
+ label: I18n.t("topic_voting.category_type.tl3_vote_limit.label"),
+ },
+ topic_voting_tl4_vote_limit: {
+ default: 10,
+ label: I18n.t("topic_voting.category_type.tl4_vote_limit.label"),
+ },
+ topic_voting_alert_votes_left: {
+ default: 1,
+ label: I18n.t("topic_voting.category_type.alert_votes_left.label"),
+ },
+ },
+ }
+ end
+
+ def icon
+ "bulb"
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/upsert-category-ideas.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/upsert-category-ideas.gjs
new file mode 100644
index 00000000000..9c89deeec17
--- /dev/null
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/upsert-category-ideas.gjs
@@ -0,0 +1,74 @@
+import Component from "@glimmer/component";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import DButton from "discourse/components/d-button";
+import EditCategoryTypeSchemaFields from "discourse/components/edit-category-type-schema-fields";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+import Category from "discourse/models/category";
+import { i18n } from "discourse-i18n";
+
+export default class UpsertCategoryIdeas extends Component {
+ @service dialog;
+ @service toasts;
+ @service router;
+
+ get isIdeasCategory() {
+ return this.args.category.isType("ideas");
+ }
+
+ @action
+ removeIdeasCategory() {
+ this.dialog.yesNoConfirm({
+ message: i18n(
+ "topic_voting.category_type_ideas.confirm_remove_ideas_type"
+ ),
+ didConfirm: async () => {
+ this.args.category.set("custom_fields.enable_topic_voting", "false");
+ this.args.form.set("custom_fields.enable_topic_voting", "false");
+ try {
+ await this.args.category.save();
+ this.args.category.removeType("ideas");
+ this.toasts.success({
+ duration: "short",
+ data: {
+ message: i18n("saved"),
+ },
+ });
+ this.router.transitionTo(
+ "editCategory.tabs",
+ Category.slugFor(this.args.category),
+ "general"
+ );
+ } catch (err) {
+ this.args.category.set("custom_fields.enable_topic_voting", "true");
+ popupAjaxError(err);
+ }
+ },
+ });
+ }
+
+ <template>
+ {{#if this.isIdeasCategory}}
+ <EditCategoryTypeSchemaFields
+ @category={{@category}}
+ @categoryType="ideas"
+ @form={{@form}}
+ >
+ <:beforeSiteSettings>
+ {{#if @category.id}}
+ <div class="ideas-category--danger-zone">
+ <DButton
+ class="btn-small ideas-category__remove-type"
+ @action={{this.removeIdeasCategory}}
+ @icon="link-slash"
+ @label="topic_voting.category_type_ideas.remove_ideas_type"
+ />
+ </div>
+ {{/if}}
+ </:beforeSiteSettings>
+ </EditCategoryTypeSchemaFields>
+ {{else}}
+ {{i18n "topic_voting.category_type_ideas.not_ideas_type"}}
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings-upsert.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings-upsert.gjs
index 0ad19d0ef49..c99e7b6d0e2 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings-upsert.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/connectors/category-custom-settings/feature-voting-settings-upsert.gjs
@@ -4,7 +4,15 @@ import { i18n } from "discourse-i18n";
export default class FeatureVotingSettingsUpsert extends Component {
static shouldRender(args, context) {
- return context.siteSettings.enable_simplified_category_creation;
+ if (!context.siteSettings.enable_simplified_category_creation) {
+ return false;
+ }
+
+ const handledByIdeasTab =
+ args.category?.isType("ideas") &&
+ context.siteSettings.enable_ideas_category_type_setup;
+
+ return !handledByIdeasTab;
}
get enableTopicVoting() {
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/ideas-category-type-tab.js b/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/ideas-category-type-tab.js
new file mode 100644
index 00000000000..34145a98f70
--- /dev/null
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/ideas-category-type-tab.js
@@ -0,0 +1,25 @@
+import { withPluginApi } from "discourse/lib/plugin-api";
+import { i18n } from "discourse-i18n";
+import UpsertCategoryIdeas from "../components/upsert-category-ideas";
+
+export default {
+ name: "ideas-category-type-tab",
+
+ initialize(container) {
+ const siteSettings = container.lookup("service:site-settings");
+ withPluginApi((api) => {
+ api.registerEditCategoryTab({
+ id: "ideas",
+ name: i18n("topic_voting.category_type_ideas.title"),
+ primary: true,
+ component: UpsertCategoryIdeas,
+ condition: ({ category }) => {
+ return (
+ category.isType("ideas") &&
+ siteSettings.enable_ideas_category_type_setup
+ );
+ },
+ });
+ });
+ },
+};
diff --git a/plugins/discourse-topic-voting/config/locales/client.en.yml b/plugins/discourse-topic-voting/config/locales/client.en.yml
index 4aa2e0fbed4..f2a59ce07f0 100755
--- a/plugins/discourse-topic-voting/config/locales/client.en.yml
+++ b/plugins/discourse-topic-voting/config/locales/client.en.yml
@@ -4,6 +4,11 @@ en:
sort_options:
votes: "Votes"
topic_voting:
+ category_type_ideas:
+ title: "Ideas"
+ not_ideas_type: "Heads up! You've disabled voting in this category. To turn that back on, you'll find the relevant settings in the Settings tab."
+ remove_ideas_type: "Remove Ideas type"
+ confirm_remove_ideas_type: "Are you sure you want to remove voting from this category? Existing votes will be preserved but members won't be able to cast new votes."
notification_label:
vote_released: "Vote returned"
title: "Topic voting"
diff --git a/plugins/discourse-topic-voting/config/locales/server.en.yml b/plugins/discourse-topic-voting/config/locales/server.en.yml
index 3aed76e0a8f..98ab8686def 100755
--- a/plugins/discourse-topic-voting/config/locales/server.en.yml
+++ b/plugins/discourse-topic-voting/config/locales/server.en.yml
@@ -1,5 +1,12 @@
en:
+ category_types:
+ ideas:
+ name: "Ideas"
+ title: "ideas"
+ description: "Where members can submit ideas and vote on the ones they want most."
+
site_settings:
+ enable_ideas_category_type_setup: "Enables the Ideas category type option during category creation setup. Note that 'enable simplified category creation' must be enabled for this to take effect."
topic_voting_enabled: 'Allow users to vote on topics?'
topic_voting_enable_vote_limits: 'Limit the number of votes per user based on trust level'
topic_voting_tl0_vote_limit: 'How many active votes are TL0 users allowed?'
@@ -11,6 +18,25 @@ en:
topic_voting_show_votes_on_profile: 'Allow users to see their votes in their activity feed?'
topic_voting_alert_votes_left: 'Alert user when this many votes are left'
topic_voting:
+ category_type:
+ show_who_voted:
+ label: "Show who voted"
+ show_votes_on_profile:
+ label: "Show votes on profile"
+ enable_vote_limits:
+ label: "Limit member votes"
+ tl0_vote_limit:
+ label: "TL0 vote limit"
+ tl1_vote_limit:
+ label: "TL1 vote limit"
+ tl2_vote_limit:
+ label: "TL2 vote limit"
+ tl3_vote_limit:
+ label: "TL3 vote limit"
+ tl4_vote_limit:
+ label: "TL4 vote limit"
+ alert_votes_left:
+ label: "Alert when votes remaining"
votes_moved:
one: "A vote has been moved."
other: "%{count} votes have been moved."
diff --git a/plugins/discourse-topic-voting/config/settings.yml b/plugins/discourse-topic-voting/config/settings.yml
index 36b2b31a9bc..064179eab80 100755
--- a/plugins/discourse-topic-voting/config/settings.yml
+++ b/plugins/discourse-topic-voting/config/settings.yml
@@ -12,32 +12,46 @@ plugins:
default: true
client: true
topic_voting_alert_votes_left:
- default: 10
+ default: 1
+ min: 0
depends_on:
- "topic_voting_enable_vote_limits"
depends_behavior: "hidden"
topic_voting_tl0_vote_limit:
default: 2
+ min: 0
depends_on:
- "topic_voting_enable_vote_limits"
depends_behavior: "hidden"
topic_voting_tl1_vote_limit:
default: 4
+ min: 0
depends_on:
- "topic_voting_enable_vote_limits"
depends_behavior: "hidden"
topic_voting_tl2_vote_limit:
default: 6
+ min: 0
depends_on:
- "topic_voting_enable_vote_limits"
depends_behavior: "hidden"
topic_voting_tl3_vote_limit:
default: 8
+ min: 0
depends_on:
- "topic_voting_enable_vote_limits"
depends_behavior: "hidden"
topic_voting_tl4_vote_limit:
default: 10
+ min: 0
depends_on:
- "topic_voting_enable_vote_limits"
depends_behavior: "hidden"
+ enable_ideas_category_type_setup:
+ default: false
+ hidden: true
+ client: true
+ upcoming_change:
+ status: "experimental"
+ impact: "feature,staff"
+ learn_more_url: "https://meta.discourse.org/t/-/399365"
diff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rb
index 29aa47ba0ee..aa0cd00e6bc 100755
--- a/plugins/discourse-topic-voting/plugin.rb
+++ b/plugins/discourse-topic-voting/plugin.rb
@@ -30,6 +30,7 @@ require_relative "lib/discourse_topic_voting/topic_votes_filter"
after_initialize do
reloadable_patch do
+ register_category_type(DiscourseTopicVoting::Categories::Types::Ideas)
CategoriesController.prepend(DiscourseTopicVoting::CategoriesControllerExtension)
Category.prepend(DiscourseTopicVoting::CategoryExtension)
ListController.prepend(DiscourseTopicVoting::ListControllerExtension)
diff --git a/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/categories/types/ideas_spec.rb b/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/categories/types/ideas_spec.rb
new file mode 100644
index 00000000000..41fd786beab
--- /dev/null
+++ b/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/categories/types/ideas_spec.rb
@@ -0,0 +1,129 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseTopicVoting::Categories::Types::Ideas do
+ fab!(:category)
+
+ before { SiteSetting.topic_voting_enabled = true }
+
+ describe ".visible?" do
+ it "returns true when enable_ideas_category_type_setup is true" do
+ SiteSetting.enable_ideas_category_type_setup = true
+ expect(described_class.visible?).to eq(true)
+ end
+
+ it "returns false when enable_ideas_category_type_setup is false" do
+ SiteSetting.enable_ideas_category_type_setup = false
+ expect(described_class.visible?).to eq(false)
+ end
+ end
+
+ describe ".enable_plugin" do
+ it "enables the topic_voting_enabled site setting" do
+ SiteSetting.topic_voting_enabled = false
+ described_class.enable_plugin
+ expect(SiteSetting.topic_voting_enabled).to eq(true)
+ end
+ end
+
+ describe ".category_matches?" do
+ it "returns true when category has voting enabled" do
+ DiscourseTopicVoting::CategorySetting.create!(category: category)
+ Category.reset_voting_cache
+ expect(described_class.category_matches?(category)).to eq(true)
+ end
+
+ it "returns false when category does not have voting enabled" do
+ Category.reset_voting_cache
+ expect(described_class.category_matches?(category)).to eq(false)
+ end
+ end
+
+ describe ".find_matches" do
+ it "returns categories with voting enabled" do
+ DiscourseTopicVoting::CategorySetting.create!(category: category)
+ expect(described_class.find_matches).to include(category)
+ end
+
+ it "does not return categories without voting" do
+ expect(described_class.find_matches).not_to include(category)
+ end
+ end
+
+ describe ".configure_category" do
+ fab!(:admin)
+
+ it "creates a topic_voting_category_setting record" do
+ expect { described_class.configure_category(category, guardian: admin.guardian) }.to change {
+ DiscourseTopicVoting::CategorySetting.count
+ }.by(1)
+
+ expect(Category.can_vote?(category.id)).to eq(true)
+ end
+
+ it "does not create duplicate records" do
+ described_class.configure_category(category, guardian: admin.guardian)
+
+ expect {
+ described_class.configure_category(category, guardian: admin.guardian)
+ }.not_to change { DiscourseTopicVoting::CategorySetting.count }
+ end
+ end
+
+ describe ".configuration_schema" do
+ it "returns expected keys" do
+ schema = described_class.configuration_schema
+ expect(schema).to have_key(:general_category_settings)
+ expect(schema).to have_key(:site_settings)
+ end
+
+ it "includes general category settings with correct defaults" do
+ schema = described_class.configuration_schema
+ expect(schema[:general_category_settings][:name][:default]).to eq("Ideas")
+ expect(schema[:general_category_settings][:emoji][:default]).to eq("bulb")
+ end
+
+ it "includes site settings with defaults and labels" do
+ schema = described_class.configuration_schema
+ expect(schema[:site_settings]).to eq(
+ {
+ topic_voting_show_who_voted: {
+ default: true,
+ label: "Show who voted",
+ },
+ topic_voting_show_votes_on_profile: {
+ default: true,
+ label: "Show votes on profile",
+ },
+ topic_voting_enable_vote_limits: {
+ default: true,
+ label: "Limit member votes",
+ },
+ topic_voting_tl0_vote_limit: {
+ default: 2,
+ label: "TL0 vote limit",
+ },
+ topic_voting_tl1_vote_limit: {
+ default: 4,
+ label: "TL1 vote limit",
+ },
+ topic_voting_tl2_vote_limit: {
+ default: 6,
+ label: "TL2 vote limit",
+ },
+ topic_voting_tl3_vote_limit: {
+ default: 8,
+ label: "TL3 vote limit",
+ },
+ topic_voting_tl4_vote_limit: {
+ default: 10,
+ label: "TL4 vote limit",
+ },
+ topic_voting_alert_votes_left: {
+ default: 1,
+ label: "Alert when votes remaining",
+ },
+ },
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-topic-voting/spec/system/ideas_category_type_setup_spec.rb b/plugins/discourse-topic-voting/spec/system/ideas_category_type_setup_spec.rb
new file mode 100644
index 00000000000..2ff3e849348
--- /dev/null
+++ b/plugins/discourse-topic-voting/spec/system/ideas_category_type_setup_spec.rb
@@ -0,0 +1,128 @@
+# frozen_string_literal: true
+
+RSpec.describe "Ideas Category Type Setup" do
+ fab!(:admin)
+
+ let(:category_page) { PageObjects::Pages::Category.new }
+ let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
+ let(:category_type_card) { PageObjects::Components::CategoryTypeCard.new }
+ let(:banner) { PageObjects::Components::AdminChangesBanner.new }
+ let(:dialog) { PageObjects::Components::Dialog.new }
+ let(:toast) { PageObjects::Components::Toasts.new }
+
+ before do
+ SiteSetting.enable_simplified_category_creation = true
+ SiteSetting.enable_ideas_category_type_setup = true
+ sign_in(admin)
+ end
+
+ it "works with correct defaults and configures site settings and category setting automatically" do
+ visit("/new-category/setup")
+ category_type_card.find_type_card("ideas").click
+ expect(page).to have_content(I18n.t("js.category.create_with_type", typeName: "ideas"))
+
+ expect(form.field("name").value).to eq("Ideas")
+ expect(
+ form.field("style_type").find(".form-kit__control-radio[type='radio'][value='emoji']")[
+ "checked"
+ ],
+ ).to eq(true)
+ expect(form.field("style_type").find("#control-emoji").find("img.emoji")["title"]).to eq("bulb")
+
+ expect(banner).to be_visible
+ banner.click_save
+
+ expect(page).to have_content(I18n.t("js.category.edit_dialog_title", categoryName: "Ideas"))
+ expect(page).to have_css(".d-nav-submenu__tabs .edit-category-ideas")
+ expect(SiteSetting.topic_voting_enabled).to eq(true)
+ category = Category.find_by(name: "Ideas")
+ expect(Category.can_vote?(category.id)).to eq(true)
+ end
+
+ context "when the ideas category type setup is disabled" do
+ before { SiteSetting.enable_ideas_category_type_setup = false }
+
+ it "does not show the ideas category type" do
+ visit("/new-category/setup")
+ expect(page).to have_no_css(".category-type-cards__card.--category-type-ideas")
+ end
+
+ it "does not show the tab for the ideas category type when editing an existing category" do
+ ideas_category = Fabricate(:category, name: "Ideas")
+ DiscourseTopicVoting::Categories::Types::Ideas.configure_category(
+ ideas_category,
+ guardian: admin.guardian,
+ )
+ visit("/c/#{ideas_category.slug}/edit/ideas")
+ expect(page).to have_no_css(".d-nav-submenu__tabs .edit-category-ideas")
+ end
+ end
+
+ context "when there is an ideas category already configured" do
+ fab!(:category)
+
+ before do
+ DiscourseTopicVoting::Categories::Types::Ideas.configure_category(
+ category,
+ guardian: admin.guardian,
+ )
+ end
+
+ it "does not preload basic data for the ideas category type" do
+ visit("/new-category/setup")
+ category_type_card.find_type_card("ideas").click
+
+ expect(page).to have_content(I18n.t("js.category.create_with_type", typeName: "ideas"))
+ expect(form.field("name").value).to eq("")
+ expect(
+ form.field("style_type").find(".form-kit__control-radio[type='radio'][value='emoji']")[
+ "checked"
+ ],
+ ).to eq(nil)
+ end
+
+ it "can edit the settings of the ideas category in a tab" do
+ visit("/c/#{category.slug}/edit/ideas")
+
+ form.field("category_type_site_settings.topic_voting_show_who_voted").toggle
+
+ banner.click_save
+ expect(toast).to have_success(I18n.t("js.saved"))
+
+ expect(SiteSetting.topic_voting_show_who_voted).to eq(false)
+ end
+
+ it "hides vote limit settings when limit member votes is unchecked" do
+ visit("/c/#{category.slug}/edit/ideas")
+
+ expect(page).to have_field("category_type_site_settings.topic_voting_tl0_vote_limit")
+
+ form.field("category_type_site_settings.topic_voting_enable_vote_limits").toggle
+
+ expect(page).to have_no_field("category_type_site_settings.topic_voting_tl0_vote_limit")
+ expect(page).to have_no_field("category_type_site_settings.topic_voting_alert_votes_left")
+ end
+
+ it "can remove the ideas type from the category" do
+ visit("/c/#{category.slug}/edit/ideas")
+ page.find(".ideas-category--danger-zone .ideas-category__remove-type").click
+ expect(dialog).to have_content(
+ I18n.t("js.topic_voting.category_type_ideas.confirm_remove_ideas_type"),
+ )
+ dialog.click_yes
+ expect(toast).to have_success(I18n.t("js.saved"))
+ expect(page).to have_css(".edit-category-general.active")
+ expect(page).to have_current_path("/c/#{category.slug}/edit/general")
+ expect(Category.can_vote?(category.reload.id)).to eq(false)
+ end
+ end
+
+ context "when visiting the Ideas tab for a non-ideas category" do
+ fab!(:category)
+
+ it "shows the not ideas type message" do
+ visit("/c/#{category.slug}/edit/ideas")
+ expect(page).to have_content(I18n.t("js.topic_voting.category_type_ideas.not_ideas_type"))
+ end
+ end
+end
diff --git a/spec/services/categories/types/base_spec.rb b/spec/services/categories/types/base_spec.rb
index db2e1dfb4dc..919ba037521 100644
--- a/spec/services/categories/types/base_spec.rb
+++ b/spec/services/categories/types/base_spec.rb
@@ -240,6 +240,105 @@ RSpec.describe Categories::Types::Base do
schema = described_class.send(:resolved_configuration_schema)
expect(schema).to be_a(Hash)
end
+
+ it "uses custom label from the hash config when present" do
+ test_type =
+ Class.new(described_class) do
+ type_id :test_labels
+
+ def self.configuration_schema
+ { site_settings: { title: { default: "My Forum", label: "Custom title label" } } }
+ end
+ end
+
+ schema = test_type.send(:resolved_configuration_schema)
+ entry = schema[:site_settings].first
+ expect(entry[:label]).to eq("Custom title label")
+ end
+
+ it "falls back to humanized_name when no label override exists" do
+ test_type =
+ Class.new(described_class) do
+ type_id :test_no_labels
+
+ def self.configuration_schema
+ { site_settings: { title: "My Forum" } }
+ end
+ end
+
+ schema = test_type.send(:resolved_configuration_schema)
+ entry = schema[:site_settings].first
+ expect(entry[:label]).to eq(SiteSetting.setting_metadata_hash(:title)[:humanized_name])
+ end
+
+ it "includes min and max when present in site setting metadata" do
+ test_type =
+ Class.new(described_class) do
+ type_id :test_min_max
+
+ def self.configuration_schema
+ { site_settings: { suggested_topics_unread_max_days_old: 7 } }
+ end
+ end
+
+ schema = test_type.send(:resolved_configuration_schema)
+ entry = schema[:site_settings].find { |e| e[:key] == "suggested_topics_unread_max_days_old" }
+
+ expect(entry[:min]).to eq(0)
+ expect(entry[:max]).to eq(36_500)
+ end
+
+ it "does not include min or max when absent from site setting metadata" do
+ test_type =
+ Class.new(described_class) do
+ type_id :test_no_min_max
+
+ def self.configuration_schema
+ { site_settings: { title: "My Forum" } }
+ end
+ end
+
+ schema = test_type.send(:resolved_configuration_schema)
+ entry = schema[:site_settings].find { |e| e[:key] == "title" }
+
+ expect(entry).not_to have_key(:min)
+ expect(entry).not_to have_key(:max)
+ end
+
+ it "infers depends_on from site setting metadata" do
+ test_type =
+ Class.new(described_class) do
+ type_id :test_depends_on
+
+ def self.configuration_schema
+ { site_settings: { title: "My Forum", set_locale_from_accept_language_header: true } }
+ end
+ end
+
+ schema = test_type.send(:resolved_configuration_schema)
+ title_entry = schema[:site_settings].find { |e| e[:key] == "title" }
+ dependent_entry =
+ schema[:site_settings].find { |e| e[:key] == "set_locale_from_accept_language_header" }
+
+ expect(title_entry).not_to have_key(:depends_on)
+ expect(dependent_entry[:depends_on]).to eq("allow_user_locale")
+ end
+ end
+
+ describe ".configure_site_settings" do
+ it "extracts default from hash config values" do
+ test_type =
+ Class.new(described_class) do
+ type_id :test_hash_config
+
+ def self.configuration_schema
+ { site_settings: { title: { default: "Hash Default" } } }
+ end
+ end
+
+ test_type.configure_site_settings(category, guardian: admin.guardian)
+ expect(SiteSetting.title).to eq("Hash Default")
+ end
end
describe "category_type_site_setting_names with empty schema" do