From f2d1437cff98e697bb24469c3867fb832e6f6f2f Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:35:38 -0400 Subject: [PATCH 01/15] DEV: Implement wizard replacement validation in Topic Creation --- config/locales/server.en.yml | 2 ++ .../extensions/topic_extension.rb | 23 +++++++++++++++++++ plugin.rb | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 lib/custom_wizard/extensions/topic_extension.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e8ceb44b..4056d885 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -55,6 +55,8 @@ en: liquid_syntax_error: "Liquid syntax error in %{attribute}: %{message}" subscription: "%{type} %{property} usage is not supported on your subscription" not_permitted_for_guests: "%{object_id} is not permitted when guests can access the wizard" + error_messages: + wizard_replacing_composer: "A wizard is set to replace the composer in this category. You cannot create a topic here." site_settings: custom_wizard_enabled: "Enable custom wizards." diff --git a/lib/custom_wizard/extensions/topic_extension.rb b/lib/custom_wizard/extensions/topic_extension.rb new file mode 100644 index 00000000..395a58cc --- /dev/null +++ b/lib/custom_wizard/extensions/topic_extension.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module CustomWizardTopicExtension + extend ActiveSupport::Concern + + included { before_validation :check_wizard_replacement, on: :create } + + def check_wizard_replacement + if wizard_replacing_composer?(self.category_id) + self.errors.add( + :base, + I18n.t('wizard.error_messages.wizard_replacing_composer') + ) + end + end + + def wizard_replacing_composer?(category_id) + return false unless category_id + + category = Category.find(category_id) + category.custom_fields['create_topic_wizard'].present? + end +end diff --git a/plugin.rb b/plugin.rb index 0634ef8c..a2df919b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -91,6 +91,7 @@ after_initialize do ../lib/custom_wizard/extensions/invites_controller.rb ../lib/custom_wizard/extensions/users_controller.rb ../lib/custom_wizard/extensions/guardian.rb + ../lib/custom_wizard/extensions/topic_extension.rb ../lib/custom_wizard/extensions/custom_field/preloader.rb ../lib/custom_wizard/extensions/custom_field/serializer.rb ../lib/custom_wizard/extensions/custom_field/extension.rb @@ -200,6 +201,7 @@ after_initialize do ::InvitesController.prepend InvitesControllerCustomWizard ::UsersController.prepend CustomWizardUsersController ::Guardian.prepend CustomWizardGuardian + ::Topic.include CustomWizardTopicExtension full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss" if Stylesheet::Importer.respond_to?(:plugin_assets) From 29d7818a4a4b5fdfd6245b7cfbd91d24d0bb028c Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:36:21 -0400 Subject: [PATCH 02/15] DEV: Add spec for validation --- spec/extensions/topic_extension_spec.rb | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 spec/extensions/topic_extension_spec.rb diff --git a/spec/extensions/topic_extension_spec.rb b/spec/extensions/topic_extension_spec.rb new file mode 100644 index 00000000..e4fabfea --- /dev/null +++ b/spec/extensions/topic_extension_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +class DummyTopic < Topic + include CustomWizardTopicExtension +end + +describe DummyTopic, type: :model do + fab!(:category_with_wizard) do + Fabricate(:category, custom_fields: { create_topic_wizard: 'true' }) + end + fab!(:category_without_wizard) { Fabricate(:category) } + fab!(:user) { Fabricate(:user) } + + context 'when the category has a create_topic_wizard custom field' do + it 'does not allow creating a topic directly' do + topic = DummyTopic.new(user: user, category: category_with_wizard) + topic.valid? + expect(topic.errors[:base]).to include( + I18n.t('wizard.error_messages.wizard_replacing_composer') + ) + end + end + + context 'when the category does not have a create_topic_wizard custom field' do + it 'allows creating a topic directly' do + topic = + DummyTopic.new( + user: user, + category: category_without_wizard, + title: 'A valid topic title' + ) + is_valid = topic.valid? + puts topic.errors.full_messages unless is_valid + expect(is_valid).to be_truthy + end + end +end From de03cbd15ae87e20280736ecdabd5c425f982c32 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:39:50 -0400 Subject: [PATCH 03/15] DEV: Implement control to hide category from composer dropdown in specified categories --- .../custom-wizard-category-settings.hbs | 11 +++++++++++ .../custom-wizard-category-settings.js.es6 | 11 +++++++++++ config/locales/client.en.yml | 1 + 3 files changed, 23 insertions(+) diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs index 2c6a6975..1d316fe2 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs +++ b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs @@ -12,4 +12,15 @@ options=(hash none="admin.wizard.select") }} + +
+ +
\ No newline at end of file diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 index 7004c317..552ee193 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 +++ b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 @@ -13,6 +13,10 @@ export default { "wizardListVal", attrs?.category?.custom_fields?.create_topic_wizard ); + component.set( + "hideFromComposer", + attrs?.category?.custom_fields?.custom_wizard_hide_from_composer + ); }, actions: { @@ -20,5 +24,12 @@ export default { this.set("wizardListVal", wizard); this.set("category.custom_fields.create_topic_wizard", wizard); }, + toggleHideFromComposer() { + this.toggleProperty("hideFromComposer"); + this.set( + "category.custom_fields.custom_wizard_hide_from_composer", + this.hideFromComposer + ); + }, }, }; diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 98519335..1fd9f158 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -133,6 +133,7 @@ en: custom_wizard: title: "Custom Wizard" create_topic_wizard: "Select a wizard to replace the new topic composer in this category." + hide_from_composer: "Exclude category from composer dropdown" message: wizard: From b365b5dd4ffb5c847e4c2fcfdb0e83e9fa6734e7 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:40:07 -0400 Subject: [PATCH 04/15] DEV: Add custom_field --- plugin.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin.rb b/plugin.rb index a2df919b..6559e469 100644 --- a/plugin.rb +++ b/plugin.rb @@ -105,6 +105,7 @@ after_initialize do # preloaded category custom fields %w[ create_topic_wizard + custom_wizard_hide_from_composer ].each do |custom_field| Site.preloaded_category_custom_fields << custom_field end From 61309fd3205aff693783d1862b082248abda5f6c Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:40:44 -0400 Subject: [PATCH 05/15] DEV: Add initializer logic to filter categories --- .../discourse/initializers/custom-wizard-edits.js.es6 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index c974fafb..fa1b753c 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -2,6 +2,7 @@ import DiscourseURL from "discourse/lib/url"; import { withPluginApi } from "discourse/lib/plugin-api"; import getUrl from "discourse-common/lib/get-url"; import { observes } from "discourse-common/utils/decorators"; +import Category from "discourse/models/category"; export default { name: "custom-wizard-edits", @@ -83,6 +84,16 @@ export default { } }, }); + + api.modifyClass("component:category-chooser", { + categoriesByScope(options = {}) { + let categories = this._super(options); + + return categories.filter((category) => { + return !category.custom_fields?.custom_wizard_hide_from_composer; + }); + }, + }); }); }, }; From bb3f0c62528b664c5792b1a041d4990fd1f35d13 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:41:24 -0400 Subject: [PATCH 06/15] DEV: Add acceptance tests for category filtering --- .../category-chooser-initializer-test.js | 21 +++++++++++++++++++ test/javascripts/fixtures/categories.js.es6 | 5 +++++ 2 files changed, 26 insertions(+) create mode 100644 test/javascripts/acceptance/category-chooser-initializer-test.js diff --git a/test/javascripts/acceptance/category-chooser-initializer-test.js b/test/javascripts/acceptance/category-chooser-initializer-test.js new file mode 100644 index 00000000..a5ca3b2a --- /dev/null +++ b/test/javascripts/acceptance/category-chooser-initializer-test.js @@ -0,0 +1,21 @@ +import { click, visit } from "@ember/test-helpers"; +import { acceptance } from "discourse/tests/helpers/qunit-helpers"; +import selectKit from "discourse/tests/helpers/select-kit-helper"; +import { test } from "qunit"; + +acceptance("CategoryChooser", function (needs) { + needs.user(); + needs.settings({ + allow_uncategorized_topics: false, + }); + + test("does not display category with custom_wizard_hide_from_composer set to 't'", async function (assert) { + const categoryChooser = selectKit(".category-chooser"); + + await visit("/"); + await click("#create-topic"); + await categoryChooser.expand(); + + assert.ok(categoryChooser.rowByIndex(4).name() !== "Custom Categories"); + }); +}); diff --git a/test/javascripts/fixtures/categories.js.es6 b/test/javascripts/fixtures/categories.js.es6 index e553f860..432a1b77 100644 --- a/test/javascripts/fixtures/categories.js.es6 +++ b/test/javascripts/fixtures/categories.js.es6 @@ -33,6 +33,7 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, + custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -77,6 +78,7 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, + custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -121,6 +123,7 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, + custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -165,6 +168,7 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, + custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -206,6 +210,7 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, + custom_wizard_hide_from_composer: "t", }, allowed_tags: [], allowed_tag_groups: [], From de9dccf23391a86e19e720f3bcc4fa2fd9cbe419 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 17:46:41 -0400 Subject: [PATCH 07/15] bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 6559e469..af82879b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.4.22 +# version: 2.4.23 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech From 2ab15aaf8649697ab19c17adea43f1dd9670007b Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 18:06:52 -0400 Subject: [PATCH 08/15] FIX: linting error --- .../category-custom-settings/custom-wizard-category-settings.hbs | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs index 1d316fe2..8af4d974 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs +++ b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs @@ -22,5 +22,4 @@ /> {{i18n "admin.wizard.category_settings.custom_wizard.hide_from_composer"}} - \ No newline at end of file From bdd290f4e680e3e41936661bc73022ca97482787 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 18:07:27 -0400 Subject: [PATCH 09/15] FIX: Linting error --- .../custom-wizard-category-settings.hbs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs index 8af4d974..f548e93f 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs +++ b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs @@ -14,12 +14,12 @@
- +
\ No newline at end of file From 03ef41f7f05e2df8283d18bd768fc181dd52fb4c Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 18 Sep 2023 18:11:20 -0400 Subject: [PATCH 10/15] FIX: Linting error --- .../discourse/initializers/custom-wizard-edits.js.es6 | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index fa1b753c..5019ef83 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -2,7 +2,6 @@ import DiscourseURL from "discourse/lib/url"; import { withPluginApi } from "discourse/lib/plugin-api"; import getUrl from "discourse-common/lib/get-url"; import { observes } from "discourse-common/utils/decorators"; -import Category from "discourse/models/category"; export default { name: "custom-wizard-edits", From bb81c5700ab08214e928826b578ae3b6d4439b35 Mon Sep 17 00:00:00 2001 From: jumagura Date: Tue, 19 Sep 2023 13:57:11 -0400 Subject: [PATCH 11/15] DEV: Use Discourse Events instead of topic model --- config/locales/server.en.yml | 2 +- .../extensions/topic_extension.rb | 27 +++------- plugin.rb | 3 +- spec/extensions/topic_extension_spec.rb | 50 +++++++++++-------- 4 files changed, 37 insertions(+), 45 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 4056d885..9fa23d53 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -56,7 +56,7 @@ en: subscription: "%{type} %{property} usage is not supported on your subscription" not_permitted_for_guests: "%{object_id} is not permitted when guests can access the wizard" error_messages: - wizard_replacing_composer: "A wizard is set to replace the composer in this category. You cannot create a topic here." + wizard_replacing_composer: "Category not allowed for topic creation." site_settings: custom_wizard_enabled: "Enable custom wizards." diff --git a/lib/custom_wizard/extensions/topic_extension.rb b/lib/custom_wizard/extensions/topic_extension.rb index 395a58cc..e890f9df 100644 --- a/lib/custom_wizard/extensions/topic_extension.rb +++ b/lib/custom_wizard/extensions/topic_extension.rb @@ -1,23 +1,8 @@ -# frozen_string_literal: true - -module CustomWizardTopicExtension - extend ActiveSupport::Concern - - included { before_validation :check_wizard_replacement, on: :create } - - def check_wizard_replacement - if wizard_replacing_composer?(self.category_id) - self.errors.add( - :base, - I18n.t('wizard.error_messages.wizard_replacing_composer') - ) - end - end - - def wizard_replacing_composer?(category_id) - return false unless category_id - - category = Category.find(category_id) - category.custom_fields['create_topic_wizard'].present? +DiscourseEvent.on(:before_create_topic) do |topic_params, user| + category = topic_params.category + if category&.custom_fields&.[]('create_topic_wizard').present? + raise Discourse::InvalidParameters.new( + I18n.t('wizard.error_messages.wizard_replacing_composer') + ) end end diff --git a/plugin.rb b/plugin.rb index af82879b..a16d47d1 100644 --- a/plugin.rb +++ b/plugin.rb @@ -202,8 +202,7 @@ after_initialize do ::InvitesController.prepend InvitesControllerCustomWizard ::UsersController.prepend CustomWizardUsersController ::Guardian.prepend CustomWizardGuardian - ::Topic.include CustomWizardTopicExtension - + full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss" if Stylesheet::Importer.respond_to?(:plugin_assets) Stylesheet::Importer.plugin_assets['wizard_custom'] = Set[full_path] diff --git a/spec/extensions/topic_extension_spec.rb b/spec/extensions/topic_extension_spec.rb index e4fabfea..da4f416e 100644 --- a/spec/extensions/topic_extension_spec.rb +++ b/spec/extensions/topic_extension_spec.rb @@ -1,37 +1,45 @@ # frozen_string_literal: true -class DummyTopic < Topic - include CustomWizardTopicExtension -end - -describe DummyTopic, type: :model do +describe Topic, type: :model do fab!(:category_with_wizard) do Fabricate(:category, custom_fields: { create_topic_wizard: 'true' }) end fab!(:category_without_wizard) { Fabricate(:category) } fab!(:user) { Fabricate(:user) } + let(:valid_attrs) { Fabricate.attributes_for(:topic) } - context 'when the category has a create_topic_wizard custom field' do - it 'does not allow creating a topic directly' do - topic = DummyTopic.new(user: user, category: category_with_wizard) - topic.valid? - expect(topic.errors[:base]).to include( - I18n.t('wizard.error_messages.wizard_replacing_composer') + context 'with a create_topic_wizard custom field in the category' do + it 'will not allow creating a topic directly' do + expect do + TopicCreator.create( + user, + Guardian.new(user), + valid_attrs.merge( + title: 'A valid and sufficiently long title for testing', + category: category_with_wizard.id, + raw: 'hello this is a test topic with category with custom fields' + ) + ) + end.to raise_error( + Discourse::InvalidParameters, + 'Category not allowed for topic creation.' ) end end - context 'when the category does not have a create_topic_wizard custom field' do - it 'allows creating a topic directly' do - topic = - DummyTopic.new( - user: user, - category: category_without_wizard, - title: 'A valid topic title' + context 'without a create_topic_wizard custom field in the category' do + it 'will allow creating a topic directly' do + expect do + TopicCreator.create( + user, + Guardian.new(user), + valid_attrs.merge( + category: category_without_wizard.id, + title: 'Another valid and sufficiently long title for testing', + raw: 'This is the body of a valid topic' + ) ) - is_valid = topic.valid? - puts topic.errors.full_messages unless is_valid - expect(is_valid).to be_truthy + end.not_to raise_error end end end From 10609f33e258513f6e2e2f56db384b53bb59aac8 Mon Sep 17 00:00:00 2001 From: jumagura Date: Tue, 19 Sep 2023 14:00:04 -0400 Subject: [PATCH 12/15] FIX: linting error --- lib/custom_wizard/extensions/topic_extension.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/custom_wizard/extensions/topic_extension.rb b/lib/custom_wizard/extensions/topic_extension.rb index e890f9df..7ee442ed 100644 --- a/lib/custom_wizard/extensions/topic_extension.rb +++ b/lib/custom_wizard/extensions/topic_extension.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + DiscourseEvent.on(:before_create_topic) do |topic_params, user| category = topic_params.category if category&.custom_fields&.[]('create_topic_wizard').present? From 0992e9601cf9dc76c226f26a4b453e6de7572797 Mon Sep 17 00:00:00 2001 From: jumagura Date: Tue, 19 Sep 2023 14:17:06 -0400 Subject: [PATCH 13/15] DEV: Make hide category default when custom wizard is selected --- .../custom-wizard-category-settings.hbs | 10 ---------- .../custom-wizard-category-settings.js.es6 | 11 ----------- .../discourse/initializers/custom-wizard-edits.js.es6 | 2 +- config/locales/client.en.yml | 1 - plugin.rb | 3 +-- test/javascripts/fixtures/categories.js.es6 | 5 ----- 6 files changed, 2 insertions(+), 30 deletions(-) diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs index f548e93f..2c6a6975 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs +++ b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.hbs @@ -12,14 +12,4 @@ options=(hash none="admin.wizard.select") }} - -
-
\ No newline at end of file diff --git a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 index 552ee193..7004c317 100644 --- a/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 +++ b/assets/javascripts/discourse/connectors/category-custom-settings/custom-wizard-category-settings.js.es6 @@ -13,10 +13,6 @@ export default { "wizardListVal", attrs?.category?.custom_fields?.create_topic_wizard ); - component.set( - "hideFromComposer", - attrs?.category?.custom_fields?.custom_wizard_hide_from_composer - ); }, actions: { @@ -24,12 +20,5 @@ export default { this.set("wizardListVal", wizard); this.set("category.custom_fields.create_topic_wizard", wizard); }, - toggleHideFromComposer() { - this.toggleProperty("hideFromComposer"); - this.set( - "category.custom_fields.custom_wizard_hide_from_composer", - this.hideFromComposer - ); - }, }, }; diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index 5019ef83..bd3c3d2d 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -89,7 +89,7 @@ export default { let categories = this._super(options); return categories.filter((category) => { - return !category.custom_fields?.custom_wizard_hide_from_composer; + return !category.custom_fields?.create_topic_wizard; }); }, }); diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 1fd9f158..98519335 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -133,7 +133,6 @@ en: custom_wizard: title: "Custom Wizard" create_topic_wizard: "Select a wizard to replace the new topic composer in this category." - hide_from_composer: "Exclude category from composer dropdown" message: wizard: diff --git a/plugin.rb b/plugin.rb index a16d47d1..d5eed12b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -105,7 +105,6 @@ after_initialize do # preloaded category custom fields %w[ create_topic_wizard - custom_wizard_hide_from_composer ].each do |custom_field| Site.preloaded_category_custom_fields << custom_field end @@ -202,7 +201,7 @@ after_initialize do ::InvitesController.prepend InvitesControllerCustomWizard ::UsersController.prepend CustomWizardUsersController ::Guardian.prepend CustomWizardGuardian - + full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss" if Stylesheet::Importer.respond_to?(:plugin_assets) Stylesheet::Importer.plugin_assets['wizard_custom'] = Set[full_path] diff --git a/test/javascripts/fixtures/categories.js.es6 b/test/javascripts/fixtures/categories.js.es6 index 432a1b77..e553f860 100644 --- a/test/javascripts/fixtures/categories.js.es6 +++ b/test/javascripts/fixtures/categories.js.es6 @@ -33,7 +33,6 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, - custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -78,7 +77,6 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, - custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -123,7 +121,6 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, - custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -168,7 +165,6 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, - custom_wizard_hide_from_composer: null, }, allowed_tags: [], allowed_tag_groups: [], @@ -210,7 +206,6 @@ export default { navigate_to_first_post_after_read: false, custom_fields: { create_topic_wizard: null, - custom_wizard_hide_from_composer: "t", }, allowed_tags: [], allowed_tag_groups: [], From 0eb6fb1ae0c7105295c7d792903ede4585c012ae Mon Sep 17 00:00:00 2001 From: jumagura Date: Tue, 19 Sep 2023 20:46:42 -0400 Subject: [PATCH 14/15] REFACTOR: Move before_create_topic event handler to plugin.rb --- lib/custom_wizard/extensions/topic_extension.rb | 10 ---------- plugin.rb | 10 +++++++++- 2 files changed, 9 insertions(+), 11 deletions(-) delete mode 100644 lib/custom_wizard/extensions/topic_extension.rb diff --git a/lib/custom_wizard/extensions/topic_extension.rb b/lib/custom_wizard/extensions/topic_extension.rb deleted file mode 100644 index 7ee442ed..00000000 --- a/lib/custom_wizard/extensions/topic_extension.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -DiscourseEvent.on(:before_create_topic) do |topic_params, user| - category = topic_params.category - if category&.custom_fields&.[]('create_topic_wizard').present? - raise Discourse::InvalidParameters.new( - I18n.t('wizard.error_messages.wizard_replacing_composer') - ) - end -end diff --git a/plugin.rb b/plugin.rb index d5eed12b..7d3f7679 100644 --- a/plugin.rb +++ b/plugin.rb @@ -91,7 +91,6 @@ after_initialize do ../lib/custom_wizard/extensions/invites_controller.rb ../lib/custom_wizard/extensions/users_controller.rb ../lib/custom_wizard/extensions/guardian.rb - ../lib/custom_wizard/extensions/topic_extension.rb ../lib/custom_wizard/extensions/custom_field/preloader.rb ../lib/custom_wizard/extensions/custom_field/serializer.rb ../lib/custom_wizard/extensions/custom_field/extension.rb @@ -237,4 +236,13 @@ after_initialize do end DiscourseEvent.trigger(:custom_wizard_ready) + + on(:before_create_topic) do |topic_params, user| + category = topic_params.category + if category&.custom_fields&.[]('create_topic_wizard').present? + raise Discourse::InvalidParameters.new( + I18n.t('wizard.error_messages.wizard_replacing_composer') + ) + end + end end From 3d104406fded4f6fc2fdb5246a30f2ae55c15d12 Mon Sep 17 00:00:00 2001 From: jumagura Date: Wed, 20 Sep 2023 03:55:32 -0400 Subject: [PATCH 15/15] REFACTOR: Improve test for category chooser when there is a create_topic_wizard customfield --- .../category-chooser-initializer-test.js | 67 ++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/test/javascripts/acceptance/category-chooser-initializer-test.js b/test/javascripts/acceptance/category-chooser-initializer-test.js index a5ca3b2a..f7e02bb8 100644 --- a/test/javascripts/acceptance/category-chooser-initializer-test.js +++ b/test/javascripts/acceptance/category-chooser-initializer-test.js @@ -3,19 +3,80 @@ import { acceptance } from "discourse/tests/helpers/qunit-helpers"; import selectKit from "discourse/tests/helpers/select-kit-helper"; import { test } from "qunit"; -acceptance("CategoryChooser", function (needs) { +acceptance("Category Chooser Initializer", function (needs) { needs.user(); needs.settings({ allow_uncategorized_topics: false, }); + needs.site({ + can_tag_topics: true, + categories: [ + { + id: 1, + name: "General", + slug: "general", + permission: 1, + topic_template: null, + }, + { + id: 2, + name: "Category with custom field", + slug: "category-custom-field", + permission: 1, + topic_template: "", + custom_fields: { + create_topic_wizard: "21", + }, + }, + { + id: 3, + name: "Category 1", + slug: "category-1", + permission: 1, + topic_template: "", + }, + { + id: 4, + name: "Category 2", + slug: "category-2", + permission: 1, + topic_template: "", + }, + ], + }); - test("does not display category with custom_wizard_hide_from_composer set to 't'", async function (assert) { + test("does not display category with create_topic_wizard custom field", async function (assert) { const categoryChooser = selectKit(".category-chooser"); await visit("/"); await click("#create-topic"); await categoryChooser.expand(); + let categories = Array.from( + document.querySelectorAll(".category-chooser .category-row") + ).filter((category) => category.getAttribute("data-name")); // Filter elements with a data-name attribute + assert.equal( + categories.length, + 3, + "Correct number of categories are displayed" + ); + const categoryNames = ["General", "Category 1", "Category 2"]; - assert.ok(categoryChooser.rowByIndex(4).name() !== "Custom Categories"); + categoryNames.forEach((categoryName) => { + assert.ok( + categories.some( + (category) => category.getAttribute("data-name") === categoryName + ), + `Category '${categoryName}' is displayed` + ); + }); + + const categoryNameWithCustomField = "Category with custom field"; + assert.notOk( + categories.some( + (category) => + category.getAttribute("data-name") === categoryNameWithCustomField + ), + `Category '${categoryNameWithCustomField}' is not displayed` + ); }); });