Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composer control feature #269

Merged
merged 15 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@
options=(hash none="admin.wizard.select")
}}
</div>
</section>
<section class="field hide-from-composer">
<label>
<Input
@type="checkbox"
@checked={{hideFromComposer}}
{{on "change" (action "toggleHideFromComposer")}}
/>
{{i18n "admin.wizard.category_settings.custom_wizard.hide_from_composer"}}
</label>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ export default {
"wizardListVal",
attrs?.category?.custom_fields?.create_topic_wizard
);
component.set(
"hideFromComposer",
attrs?.category?.custom_fields?.custom_wizard_hide_from_composer
);
},

actions: {
changeWizard(wizard) {
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
);
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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;
});
},
});
});
},
};
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
23 changes: 23 additions & 0 deletions lib/custom_wizard/extensions/topic_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module CustomWizardTopicExtension
jumagura marked this conversation as resolved.
Show resolved Hide resolved
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)
jumagura marked this conversation as resolved.
Show resolved Hide resolved
category.custom_fields['create_topic_wizard'].present?
end
end
5 changes: 4 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -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: [email protected]
Expand Down Expand Up @@ -91,6 +91,7 @@
../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
Expand All @@ -104,6 +105,7 @@
# 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
Expand Down Expand Up @@ -200,6 +202,7 @@
::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)
Expand Down
37 changes: 37 additions & 0 deletions spec/extensions/topic_extension_spec.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/javascripts/acceptance/category-chooser-initializer-test.js
Original file line number Diff line number Diff line change
@@ -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) {
jumagura marked this conversation as resolved.
Show resolved Hide resolved
const categoryChooser = selectKit(".category-chooser");

await visit("/");
await click("#create-topic");
await categoryChooser.expand();

assert.ok(categoryChooser.rowByIndex(4).name() !== "Custom Categories");
});
});
5 changes: 5 additions & 0 deletions test/javascripts/fixtures/categories.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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: [],
Expand Down Expand Up @@ -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: [],
Expand Down
Loading