From bfd4e30d362e826cb22bfeedac96ea9bc32f589d Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Mon, 19 Sep 2022 12:09:34 +0200 Subject: [PATCH 1/6] Add automatic integration --- app/controllers/custom_wizard/admin/wizard.rb | 3 ++- .../components/wizard-custom-action.js.es6 | 5 +++++ .../discourse/lib/wizard-schema.js.es6 | 2 ++ .../templates/admin-wizards-wizard-show.hbs | 3 ++- .../components/wizard-custom-action.hbs | 19 +++++++++++++++++++ config/locales/client.en.yml | 2 ++ lib/custom_wizard/action.rb | 4 ++++ lib/custom_wizard/field.rb | 10 +++++----- 8 files changed, 41 insertions(+), 7 deletions(-) diff --git a/app/controllers/custom_wizard/admin/wizard.rb b/app/controllers/custom_wizard/admin/wizard.rb index 71a5623b..7a316c84 100644 --- a/app/controllers/custom_wizard/admin/wizard.rb +++ b/app/controllers/custom_wizard/admin/wizard.rb @@ -163,7 +163,8 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController mentionable_level: mapped_params, messageable_level: mapped_params, visibility_level: mapped_params, - members_visibility_level: mapped_params + members_visibility_level: mapped_params, + add_event: mapped_params ] ) end diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index a5aa06cd..c11e2b4f 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -101,4 +101,9 @@ export default Component.extend(UndoChanges, { } return apis.find((a) => a.name === api).endpoints; }, + + @discourseComputed("fieldTypes") + hasEventsField(fieldTypes) { + return fieldTypes.map((ft) => ft.id).includes("event"); + }, }); diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index f5a62c87..4953e59c 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -102,6 +102,7 @@ const action = { custom_fields: null, skip_redirect: null, suppress_notifications: null, + add_event: null, }, send_message: { title: null, @@ -198,6 +199,7 @@ const action = { "messageable_level", "visibility_level", "members_visibility_level", + "add_event", ], advanced: [ "code", diff --git a/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs b/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs index c5ed70a7..b1281a6a 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs @@ -192,7 +192,8 @@ wizard=wizard apis=apis removeAction="removeAction" - wizardFields=wizardFields}} + wizardFields=wizardFields + fieldTypes=fieldTypes}} {{/each}}
diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 4c645cf7..03aee3f4 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -158,6 +158,25 @@ )}}
+ + {{#if hasEventsField}} +
+
+ +
+ +
+ {{wizard-mapper + inputs=action.add_event + property="add_event" + onUpdate=(action "mappedFieldUpdated") + options=(hash + wizardFieldSelection=true + context="action" + )}} +
+
+ {{/if}} {{/if}} {{#if sendMessage}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index d3d938dc..ba6d4f17 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -293,6 +293,7 @@ en: date: Date time: Time date_time: Date & Time + event: Event (Events Plugin) connector: and: "and" @@ -336,6 +337,7 @@ en: category: "Category" tags: "Tags" visible: "Visible" + add_event: "Add Event (Events Plugin)" open_composer: label: "Open Composer" update_profile: diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 6de7d3f7..d532891c 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -46,6 +46,10 @@ class CustomWizard::Action def create_topic params = basic_topic_params.merge(public_topic_params) + CustomWizard::Field.action_callbacks.each do |acb| + params = acb.call(params, @wizard, @action, @submission) + end + if params[:title].present? && params[:raw].present? creator = PostCreator.new(user, params) post = creator.create diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index 12c46623..ee0fc379 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -131,18 +131,18 @@ class CustomWizard::Field } end - def self.require_assets - @require_assets ||= {} + def self.action_callbacks + @acbs ||= [] end - def self.register(type, plugin = nil, asset_paths = [], opts = {}) + def self.register(type, plugin = nil, opts = {}) if type types[type.to_sym] ||= {} types[type.to_sym] = opts[:type_opts] if opts[:type_opts].present? end - if plugin && asset_paths - require_assets[plugin] = asset_paths + if opts[:action_callback].present? && opts[:action_callback].is_a?(Proc) + action_callbacks << opts[:action_callback] end end end From 8083f4438992b208c28469d4eb046b8a970e9cd1 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Sep 2022 15:58:39 +0200 Subject: [PATCH 2/6] Bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index b6d82102..49dcb105 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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: 1.22.9 +# version: 1.22.10 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech From 26749bd055af42757cd961d0c0b50de569a76b16 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Sep 2022 16:23:41 +0200 Subject: [PATCH 3/6] Add asset requiring for backwards compatibility --- lib/custom_wizard/field.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index ee0fc379..a4358296 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -135,7 +135,20 @@ class CustomWizard::Field @acbs ||= [] end - def self.register(type, plugin = nil, opts = {}) + def self.require_assets + Rails.logger.warn("Custom Wizard field regisration no longer requires asset registration. Support will be removed in v2.1.0.") + + @require_assets ||= {} + end + + def self.register(type, plugin = nil, opts = {}, legacy_opts = {}) + if opts.is_a?(Array) + Rails.logger.warn("Custom Wizard field regisration no longer requires asset registration. Support will be removed in v2.1.0.") + + require_assets[plugin] = opts + opts = legacy_opts + end + if type types[type.to_sym] ||= {} types[type.to_sym] = opts[:type_opts] if opts[:type_opts].present? From aa288b19e2b940e4040284e3a24e90ef70ccf253 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Sep 2022 16:52:55 +0200 Subject: [PATCH 4/6] Move action callbacks to action class --- lib/custom_wizard/action.rb | 15 ++++++++++++++- lib/custom_wizard/field.rb | 8 -------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index d532891c..8eed64b2 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -43,10 +43,14 @@ class CustomWizard::Action @mapper ||= CustomWizard::Mapper.new(user: user, data: mapper_data) end + def callbacks_for(action) + self.class.callbacks[action] || [] + end + def create_topic params = basic_topic_params.merge(public_topic_params) - CustomWizard::Field.action_callbacks.each do |acb| + callbacks_for(:before_create_topic).each do |acb| params = acb.call(params, @wizard, @action, @submission) end @@ -421,6 +425,15 @@ class CustomWizard::Action end end + def self.callbacks + @callbacks ||= {} + end + + def self.register_callback(action, &block) + callbacks[action] ||= [] + callbacks[action] << block + end + private def action_category diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index a4358296..c39c2753 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -131,10 +131,6 @@ class CustomWizard::Field } end - def self.action_callbacks - @acbs ||= [] - end - def self.require_assets Rails.logger.warn("Custom Wizard field regisration no longer requires asset registration. Support will be removed in v2.1.0.") @@ -153,9 +149,5 @@ class CustomWizard::Field types[type.to_sym] ||= {} types[type.to_sym] = opts[:type_opts] if opts[:type_opts].present? end - - if opts[:action_callback].present? && opts[:action_callback].is_a?(Proc) - action_callbacks << opts[:action_callback] - end end end From 12e5bef3721faf26a8ed11c1fa5656f9e9bd0d59 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Sep 2022 17:00:41 +0200 Subject: [PATCH 5/6] Add spec for action callbacks --- spec/components/custom_wizard/action_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index a9d8b0a8..d482b8ea 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -298,4 +298,23 @@ describe CustomWizard::Action do updater.update expect(updater.result[:redirect_on_next]).to eq("https://google.com") end + + it 'registers callbacks' do + wizard = CustomWizard::Builder.new(@template[:id], user).build + + described_class.register_callback(:before_create_topic) do |params, wizard, action, submission| + params[:topic_opts][:custom_fields]["topic_custom_field"] = true + params + end + + action = CustomWizard::Action.new( + wizard: wizard, + action: create_topic.with_indifferent_access, + submission: wizard.current_submission + ) + action.perform + + expect(action.result.success?).to eq(true) + expect(Topic.find(action.result.output).custom_fields["topic_custom_field"]).to eq("t") + end end From ebd382cca40b1da171eb0361f35f435eac02d449 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 23 Sep 2022 17:06:16 +0200 Subject: [PATCH 6/6] Fix linting --- spec/components/custom_wizard/action_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index d482b8ea..3c693c22 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -300,13 +300,12 @@ describe CustomWizard::Action do end it 'registers callbacks' do - wizard = CustomWizard::Builder.new(@template[:id], user).build - described_class.register_callback(:before_create_topic) do |params, wizard, action, submission| params[:topic_opts][:custom_fields]["topic_custom_field"] = true params end + wizard = CustomWizard::Builder.new(@template[:id], user).build action = CustomWizard::Action.new( wizard: wizard, action: create_topic.with_indifferent_access,