From 1662ff166dbf32053996f5502e0ee88b14e9aebc Mon Sep 17 00:00:00 2001 From: merefield Date: Sat, 15 Jul 2023 09:38:46 +0100 Subject: [PATCH 1/5] Add failing test --- spec/requests/custom_wizard/admin/wizard_controller_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/requests/custom_wizard/admin/wizard_controller_spec.rb b/spec/requests/custom_wizard/admin/wizard_controller_spec.rb index 9f63cb6b..3ee46f2a 100644 --- a/spec/requests/custom_wizard/admin/wizard_controller_spec.rb +++ b/spec/requests/custom_wizard/admin/wizard_controller_spec.rb @@ -5,6 +5,7 @@ describe CustomWizard::AdminWizardController do fab!(:user1) { Fabricate(:user) } fab!(:user2) { Fabricate(:user) } let(:template) { get_wizard_fixture("wizard") } + let(:category) { Fabricate(:category, custom_fields: { create_topic_wizard: "wizard" }) } before do CustomWizard::Template.save(template, skip_jobs: true) @@ -40,9 +41,11 @@ describe CustomWizard::AdminWizardController do end it "removes wizard templates" do + expect(CategoryCustomField.find_by(category_id: category.id, name: 'create_topic_wizard')).not_to eq(nil) delete "/admin/wizards/wizard/#{template['id']}.json" expect(response.status).to eq(200) expect(CustomWizard::Template.exists?(template['id'])).to eq(false) + expect(CategoryCustomField.find_by(category_id: category.id, name: 'create_topic_wizard')).to eq(nil) end it "saves wizard templates" do From 4449ab7afffcceb1fd1e60ecb35e31b9c394fea2 Mon Sep 17 00:00:00 2001 From: merefield Date: Sat, 15 Jul 2023 09:56:32 +0100 Subject: [PATCH 2/5] Add corresponding code to help pass test --- app/controllers/custom_wizard/admin/wizard.rb | 2 ++ .../custom_wizard/admin/wizard_controller_spec.rb | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/controllers/custom_wizard/admin/wizard.rb b/app/controllers/custom_wizard/admin/wizard.rb index 08e7b6d0..6737da6c 100644 --- a/app/controllers/custom_wizard/admin/wizard.rb +++ b/app/controllers/custom_wizard/admin/wizard.rb @@ -26,6 +26,8 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController def remove if CustomWizard::Template.remove(@wizard.id) + related_custom_fields = CategoryCustomField.where(name: 'create_topic_wizard', value: @wizard.name) + related_custom_fields.destroy_all render json: success_json else render json: failed_json diff --git a/spec/requests/custom_wizard/admin/wizard_controller_spec.rb b/spec/requests/custom_wizard/admin/wizard_controller_spec.rb index 3ee46f2a..cf1b7ec7 100644 --- a/spec/requests/custom_wizard/admin/wizard_controller_spec.rb +++ b/spec/requests/custom_wizard/admin/wizard_controller_spec.rb @@ -5,7 +5,7 @@ describe CustomWizard::AdminWizardController do fab!(:user1) { Fabricate(:user) } fab!(:user2) { Fabricate(:user) } let(:template) { get_wizard_fixture("wizard") } - let(:category) { Fabricate(:category, custom_fields: { create_topic_wizard: "wizard" }) } + let(:category) { Fabricate(:category, custom_fields: { create_topic_wizard: template['name'] }) } before do CustomWizard::Template.save(template, skip_jobs: true) @@ -40,12 +40,12 @@ describe CustomWizard::AdminWizardController do expect(response.parsed_body['steps'].length).to eq(3) end - it "removes wizard templates" do - expect(CategoryCustomField.find_by(category_id: category.id, name: 'create_topic_wizard')).not_to eq(nil) + it "removes wizard templates and make sure create_topic_wizard settings for that wizard are removed from Categories" do + expect(CategoryCustomField.find_by(category_id: category.id, name: 'create_topic_wizard', value: template['name'])).not_to eq(nil) delete "/admin/wizards/wizard/#{template['id']}.json" expect(response.status).to eq(200) expect(CustomWizard::Template.exists?(template['id'])).to eq(false) - expect(CategoryCustomField.find_by(category_id: category.id, name: 'create_topic_wizard')).to eq(nil) + expect(CategoryCustomField.find_by(name: 'create_topic_wizard', value: template['name'])).to eq(nil) end it "saves wizard templates" do From 1b93c0618844c391e0df9e3a398d630544e48d67 Mon Sep 17 00:00:00 2001 From: merefield Date: Sat, 15 Jul 2023 09:57:54 +0100 Subject: [PATCH 3/5] bump patch --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 287419b2..4b6440de 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.12 +# version: 2.4.13 # 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 b71f627d73460ab1fbbf7ced67049a91502eabb0 Mon Sep 17 00:00:00 2001 From: merefield Date: Sat, 15 Jul 2023 10:00:54 +0100 Subject: [PATCH 4/5] improve test explanation --- spec/requests/custom_wizard/admin/wizard_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/requests/custom_wizard/admin/wizard_controller_spec.rb b/spec/requests/custom_wizard/admin/wizard_controller_spec.rb index cf1b7ec7..04884b49 100644 --- a/spec/requests/custom_wizard/admin/wizard_controller_spec.rb +++ b/spec/requests/custom_wizard/admin/wizard_controller_spec.rb @@ -40,7 +40,7 @@ describe CustomWizard::AdminWizardController do expect(response.parsed_body['steps'].length).to eq(3) end - it "removes wizard templates and make sure create_topic_wizard settings for that wizard are removed from Categories" do + it "removes wizard templates whilst making sure create_topic_wizard settings for that wizard are removed from Categories" do expect(CategoryCustomField.find_by(category_id: category.id, name: 'create_topic_wizard', value: template['name'])).not_to eq(nil) delete "/admin/wizards/wizard/#{template['id']}.json" expect(response.status).to eq(200) From 580889a34ecb85d8403fe8cedb70cd07ba8b075a Mon Sep 17 00:00:00 2001 From: merefield Date: Mon, 17 Jul 2023 10:16:27 +0100 Subject: [PATCH 5/5] move destroy logic into library method & transaction --- app/controllers/custom_wizard/admin/wizard.rb | 2 -- lib/custom_wizard/template.rb | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/custom_wizard/admin/wizard.rb b/app/controllers/custom_wizard/admin/wizard.rb index 6737da6c..08e7b6d0 100644 --- a/app/controllers/custom_wizard/admin/wizard.rb +++ b/app/controllers/custom_wizard/admin/wizard.rb @@ -26,8 +26,6 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController def remove if CustomWizard::Template.remove(@wizard.id) - related_custom_fields = CategoryCustomField.where(name: 'create_topic_wizard', value: @wizard.name) - related_custom_fields.destroy_all render json: success_json else render json: failed_json diff --git a/lib/custom_wizard/template.rb b/lib/custom_wizard/template.rb index 12a86bf6..59a9998e 100644 --- a/lib/custom_wizard/template.rb +++ b/lib/custom_wizard/template.rb @@ -64,6 +64,8 @@ class CustomWizard::Template ensure_wizard_upload_references!(wizard_id) PluginStore.remove(CustomWizard::PLUGIN_NAME, wizard.id) clear_user_wizard_redirect(wizard_id, after_time: !!wizard.after_time) + related_custom_fields = CategoryCustomField.where(name: 'create_topic_wizard', value: wizard.name) + related_custom_fields.destroy_all end clear_cache_keys