1
0
Fork 0

DEV: Use Discourse Events instead of topic model

Dieser Commit ist enthalten in:
jumagura 2023-09-19 13:57:11 -04:00
Ursprung 03ef41f7f0
Commit bb81c5700a
4 geänderte Dateien mit 37 neuen und 45 gelöschten Zeilen

Datei anzeigen

@ -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."

Datei anzeigen

@ -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

Datei anzeigen

@ -202,7 +202,6 @@ 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)

Datei anzeigen

@ -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