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)