From 24d7ca646a425af83cfb8054ef10af18e26ae843 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 18:18:00 -0400 Subject: [PATCH 01/10] FIX: Refine Topic Creation Hook to Allow Wizard Multi-Step Functionality --- lib/custom_wizard/action.rb | 1 + plugin.rb | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index ac0799f3..ad261e9a 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -63,6 +63,7 @@ class CustomWizard::Action def create_topic params = basic_topic_params.merge(public_topic_params) + params[:from_wizard] = true callbacks_for(:before_create_topic).each do |acb| params = acb.call(params, @wizard, @action, @submission) diff --git a/plugin.rb b/plugin.rb index 7d3f7679..ed236919 100644 --- a/plugin.rb +++ b/plugin.rb @@ -238,11 +238,13 @@ after_initialize do DiscourseEvent.trigger(:custom_wizard_ready) on(:before_create_topic) do |topic_params, user| - category = topic_params.category - if category&.custom_fields&.[]('create_topic_wizard').present? + next if topic_params[:archetype] == 'message' + + if topic_params[:category]&.custom_fields&.[]('create_topic_wizard').present? && !topic_params[:from_wizard] raise Discourse::InvalidParameters.new( - I18n.t('wizard.error_messages.wizard_replacing_composer') - ) + I18n.t('wizard.error_messages.wizard_replacing_composer') + ) end end + end From dae803bf20e10687f5ce247972616e609e031f75 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 18:25:36 -0400 Subject: [PATCH 02/10] bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index ed236919..e930dfa6 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: 2.4.23 +# version: 2.4.24 # 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: development@pavilion.tech From 3433bd4141574458e058a89c5774b0788d1b1cb3 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 18:29:12 -0400 Subject: [PATCH 03/10] fix linting --- plugin.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugin.rb b/plugin.rb index e930dfa6..4e674b1d 100644 --- a/plugin.rb +++ b/plugin.rb @@ -239,12 +239,10 @@ after_initialize do on(:before_create_topic) do |topic_params, user| next if topic_params[:archetype] == 'message' - if topic_params[:category]&.custom_fields&.[]('create_topic_wizard').present? && !topic_params[:from_wizard] raise Discourse::InvalidParameters.new( I18n.t('wizard.error_messages.wizard_replacing_composer') ) end end - end From 1d2f75951f88baa66d9534b063ee7856865f0d00 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 22:05:28 -0400 Subject: [PATCH 04/10] FIX: Use wizard_submission_id instead of new value --- lib/custom_wizard/action.rb | 1 - plugin.rb | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index ad261e9a..ac0799f3 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -63,7 +63,6 @@ class CustomWizard::Action def create_topic params = basic_topic_params.merge(public_topic_params) - params[:from_wizard] = true callbacks_for(:before_create_topic).each do |acb| params = acb.call(params, @wizard, @action, @submission) diff --git a/plugin.rb b/plugin.rb index 4e674b1d..62e4d225 100644 --- a/plugin.rb +++ b/plugin.rb @@ -239,10 +239,14 @@ after_initialize do on(:before_create_topic) do |topic_params, user| next if topic_params[:archetype] == 'message' - if topic_params[:category]&.custom_fields&.[]('create_topic_wizard').present? && !topic_params[:from_wizard] + category = topic_params.category + wizard_submission_id = topic_params.custom_fields['wizard_submission_id'] + + if category&.custom_fields&.[]('create_topic_wizard').present? && wizard_submission_id.blank? raise Discourse::InvalidParameters.new( I18n.t('wizard.error_messages.wizard_replacing_composer') ) end end + end From 779912332a446b76c7bcea8c828080c77a073af9 Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 22:25:20 -0400 Subject: [PATCH 05/10] DEV: remove next condition --- plugin.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 62e4d225..20822c7f 100644 --- a/plugin.rb +++ b/plugin.rb @@ -238,7 +238,6 @@ after_initialize do DiscourseEvent.trigger(:custom_wizard_ready) on(:before_create_topic) do |topic_params, user| - next if topic_params[:archetype] == 'message' category = topic_params.category wizard_submission_id = topic_params.custom_fields['wizard_submission_id'] From 98391b565e728b2a53658661a9a49c370418ae3b Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 22:26:00 -0400 Subject: [PATCH 06/10] DEV: Modify test for errors --- spec/extensions/topic_extension_spec.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/spec/extensions/topic_extension_spec.rb b/spec/extensions/topic_extension_spec.rb index da4f416e..9b2d9ee5 100644 --- a/spec/extensions/topic_extension_spec.rb +++ b/spec/extensions/topic_extension_spec.rb @@ -15,15 +15,12 @@ describe Topic, type: :model do 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' + title: 'A valid title', + raw: 'hello this is a test topic' ) ) - end.to raise_error( - Discourse::InvalidParameters, - 'Category not allowed for topic creation.' - ) + end.to raise_error end end @@ -35,8 +32,8 @@ describe Topic, type: :model do 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' + title: 'Another valid title', + raw: 'This is a valid topic' ) ) end.not_to raise_error From 6294f6f9c0744e8f1fe31c2edb1ce8a0240c4dae Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 22:44:54 -0400 Subject: [PATCH 07/10] DEV: Add correct parameters for testing --- spec/extensions/topic_extension_spec.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spec/extensions/topic_extension_spec.rb b/spec/extensions/topic_extension_spec.rb index 9b2d9ee5..da4f416e 100644 --- a/spec/extensions/topic_extension_spec.rb +++ b/spec/extensions/topic_extension_spec.rb @@ -15,12 +15,15 @@ describe Topic, type: :model do user, Guardian.new(user), valid_attrs.merge( + title: 'A valid and sufficiently long title for testing', category: category_with_wizard.id, - title: 'A valid title', - raw: 'hello this is a test topic' + raw: 'hello this is a test topic with category with custom fields' ) ) - end.to raise_error + end.to raise_error( + Discourse::InvalidParameters, + 'Category not allowed for topic creation.' + ) end end @@ -32,8 +35,8 @@ describe Topic, type: :model do Guardian.new(user), valid_attrs.merge( category: category_without_wizard.id, - title: 'Another valid title', - raw: 'This is a valid topic' + title: 'Another valid and sufficiently long title for testing', + raw: 'This is the body of a valid topic' ) ) end.not_to raise_error From 4ce82b0d17387919fedb176f3ee29a9776c9d9ff Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 22:46:31 -0400 Subject: [PATCH 08/10] fix linting --- plugin.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugin.rb b/plugin.rb index 20822c7f..a312209c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -240,12 +240,10 @@ after_initialize do on(:before_create_topic) do |topic_params, user| category = topic_params.category wizard_submission_id = topic_params.custom_fields['wizard_submission_id'] - if category&.custom_fields&.[]('create_topic_wizard').present? && wizard_submission_id.blank? raise Discourse::InvalidParameters.new( I18n.t('wizard.error_messages.wizard_replacing_composer') ) end end - end From a5af0b109db49a6b5ad8e0a22f833f1bc8d1e3fc Mon Sep 17 00:00:00 2001 From: jumagura Date: Mon, 2 Oct 2023 23:38:01 -0400 Subject: [PATCH 09/10] DEV: Manage edge case not custom fields --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index a312209c..3a84371b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -239,7 +239,7 @@ after_initialize do on(:before_create_topic) do |topic_params, user| category = topic_params.category - wizard_submission_id = topic_params.custom_fields['wizard_submission_id'] + wizard_submission_id = topic_params.custom_fields&.[]('wizard_submission_id') if category&.custom_fields&.[]('create_topic_wizard').present? && wizard_submission_id.blank? raise Discourse::InvalidParameters.new( I18n.t('wizard.error_messages.wizard_replacing_composer') From 7171e04e3e06d97aefe20254f0cbf09483217d2b Mon Sep 17 00:00:00 2001 From: jumagura Date: Tue, 3 Oct 2023 13:56:45 -0400 Subject: [PATCH 10/10] DEV: Add test of creating post when there are multiple actions --- spec/components/custom_wizard/action_spec.rb | 25 ++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index c450582d..57fe7f56 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -450,4 +450,29 @@ describe CustomWizard::Action do expect(action.result.success?).to eq(true) expect(Topic.find(action.result.output).custom_fields["topic_custom_field"]).to eq("t") end + + context 'creating a topic when there are multiple actions' do + it 'works' do + wizard_template['actions'] << create_topic + wizard_template['actions'] << send_message + update_template(wizard_template) + wizard = CustomWizard::Builder.new(@template[:id], user).build + wizard.create_updater( + wizard.steps.first.id, + step_1_field_1: 'Topic Title', + step_1_field_2: 'topic body' + ).update + wizard.create_updater(wizard.steps.second.id, {}).update + wizard.create_updater(wizard.steps.last.id, step_3_field_3: category.id) + .update + User.create(username: 'angus1', email: 'angus1@email.com') + wizard.create_updater(wizard.steps[0].id, {}).update + wizard.create_updater(wizard.steps[1].id, {}).update + topic = Topic.where(title: 'Topic Title', category_id: category.id) + expect(topic.exists?).to eq(true) + expect( + Post.where(topic_id: topic.pluck(:id), raw: 'topic body').exists? + ).to eq(true) + end + end end