Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-09 20:02:54 +01:00
DEV: Use Discourse Events instead of topic model
Dieser Commit ist enthalten in:
Ursprung
03ef41f7f0
Commit
bb81c5700a
4 geänderte Dateien mit 37 neuen und 45 gelöschten Zeilen
|
@ -56,7 +56,7 @@ en:
|
||||||
subscription: "%{type} %{property} usage is not supported on your subscription"
|
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"
|
not_permitted_for_guests: "%{object_id} is not permitted when guests can access the wizard"
|
||||||
error_messages:
|
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:
|
site_settings:
|
||||||
custom_wizard_enabled: "Enable custom wizards."
|
custom_wizard_enabled: "Enable custom wizards."
|
||||||
|
|
|
@ -1,23 +1,8 @@
|
||||||
# frozen_string_literal: true
|
DiscourseEvent.on(:before_create_topic) do |topic_params, user|
|
||||||
|
category = topic_params.category
|
||||||
module CustomWizardTopicExtension
|
if category&.custom_fields&.[]('create_topic_wizard').present?
|
||||||
extend ActiveSupport::Concern
|
raise Discourse::InvalidParameters.new(
|
||||||
|
I18n.t('wizard.error_messages.wizard_replacing_composer')
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -202,7 +202,6 @@ after_initialize do
|
||||||
::InvitesController.prepend InvitesControllerCustomWizard
|
::InvitesController.prepend InvitesControllerCustomWizard
|
||||||
::UsersController.prepend CustomWizardUsersController
|
::UsersController.prepend CustomWizardUsersController
|
||||||
::Guardian.prepend CustomWizardGuardian
|
::Guardian.prepend CustomWizardGuardian
|
||||||
::Topic.include CustomWizardTopicExtension
|
|
||||||
|
|
||||||
full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss"
|
full_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets/stylesheets/wizard/wizard_custom.scss"
|
||||||
if Stylesheet::Importer.respond_to?(:plugin_assets)
|
if Stylesheet::Importer.respond_to?(:plugin_assets)
|
||||||
|
|
|
@ -1,37 +1,45 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class DummyTopic < Topic
|
describe Topic, type: :model do
|
||||||
include CustomWizardTopicExtension
|
|
||||||
end
|
|
||||||
|
|
||||||
describe DummyTopic, type: :model do
|
|
||||||
fab!(:category_with_wizard) do
|
fab!(:category_with_wizard) do
|
||||||
Fabricate(:category, custom_fields: { create_topic_wizard: 'true' })
|
Fabricate(:category, custom_fields: { create_topic_wizard: 'true' })
|
||||||
end
|
end
|
||||||
fab!(:category_without_wizard) { Fabricate(:category) }
|
fab!(:category_without_wizard) { Fabricate(:category) }
|
||||||
fab!(:user) { Fabricate(:user) }
|
fab!(:user) { Fabricate(:user) }
|
||||||
|
let(:valid_attrs) { Fabricate.attributes_for(:topic) }
|
||||||
|
|
||||||
context 'when the category has a create_topic_wizard custom field' do
|
context 'with a create_topic_wizard custom field in the category' do
|
||||||
it 'does not allow creating a topic directly' do
|
it 'will not allow creating a topic directly' do
|
||||||
topic = DummyTopic.new(user: user, category: category_with_wizard)
|
expect do
|
||||||
topic.valid?
|
TopicCreator.create(
|
||||||
expect(topic.errors[:base]).to include(
|
user,
|
||||||
I18n.t('wizard.error_messages.wizard_replacing_composer')
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the category does not have a create_topic_wizard custom field' do
|
context 'without a create_topic_wizard custom field in the category' do
|
||||||
it 'allows creating a topic directly' do
|
it 'will allow creating a topic directly' do
|
||||||
topic =
|
expect do
|
||||||
DummyTopic.new(
|
TopicCreator.create(
|
||||||
user: user,
|
user,
|
||||||
category: category_without_wizard,
|
Guardian.new(user),
|
||||||
title: 'A valid topic title'
|
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?
|
end.not_to raise_error
|
||||||
puts topic.errors.full_messages unless is_valid
|
|
||||||
expect(is_valid).to be_truthy
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Laden …
In neuem Issue referenzieren