From bb81c5700ab08214e928826b578ae3b6d4439b35 Mon Sep 17 00:00:00 2001 From: jumagura Date: Tue, 19 Sep 2023 13:57:11 -0400 Subject: [PATCH] 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