From bccadb51519b395e2e04fb3cee2075103ce84d4a Mon Sep 17 00:00:00 2001 From: Aaryanpal Date: Thu, 12 Sep 2024 23:53:44 +0530 Subject: [PATCH] Add the Delete button on the Partner Group Page --- app/controllers/partner_groups_controller.rb | 15 ++++++++- .../partners/_partner_groups_table.html.erb | 3 +- config/routes.rb | 2 +- spec/system/partner_system_spec.rb | 33 +++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/controllers/partner_groups_controller.rb b/app/controllers/partner_groups_controller.rb index f54339d95c..df825c8fbc 100644 --- a/app/controllers/partner_groups_controller.rb +++ b/app/controllers/partner_groups_controller.rb @@ -1,4 +1,6 @@ class PartnerGroupsController < ApplicationController + before_action :set_partner_groups, only: %i[edit destroy] + def new @partner_group = current_organization.partner_groups.new @item_categories = current_organization.item_categories @@ -16,7 +18,6 @@ def create end def edit - @partner_group = current_organization.partner_groups.find(params[:id]) @item_categories = current_organization.item_categories end @@ -30,8 +31,20 @@ def update end end + def destroy + @partner_group.destroy + + respond_to do |format| + format.html { redirect_to partners_path + "#nav-partner-groups", notice: "Partner Group was successfully destroyed." } + end + end + private + def set_partner_groups + @partner_group = current_organization.partner_groups.find(params[:id]) + end + def partner_group_params params.require(:partner_group).permit(:name, :send_reminders, :deadline_day, :reminder_day, item_category_ids: []) end diff --git a/app/views/partners/_partner_groups_table.html.erb b/app/views/partners/_partner_groups_table.html.erb index abc7dad2bb..c59bbb297b 100644 --- a/app/views/partners/_partner_groups_table.html.erb +++ b/app/views/partners/_partner_groups_table.html.erb @@ -52,8 +52,9 @@ No <% end %> - + <%= edit_button_to edit_partner_group_path(pg) %> + <%= delete_button_to(partner_group_path(pg),{confirm: confirm_delete_msg(pg.name)}) unless pg.partners.any? %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 17e4398dc0..fc1ed88259 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -226,7 +226,7 @@ def set_up_flipper end end - resources :partner_groups, only: [:new, :create, :edit, :update] + resources :partner_groups, only: %i(new create edit update destroy) resources :product_drives diff --git a/spec/system/partner_system_spec.rb b/spec/system/partner_system_spec.rb index e27747f8da..66b1d1a338 100644 --- a/spec/system/partner_system_spec.rb +++ b/spec/system/partner_system_spec.rb @@ -532,6 +532,39 @@ assert page.has_content? item_category_2.name end end + + describe 'deleting an existing partner group' do + context 'when the partner group has partners' do + let!(:first_partner_group) { create(:partner_group, organization: organization) } + let!(:first_partner) { create(:partner, partner_group: first_partner_group) } + it 'should not display the delete link' do + visit partners_path + click_on 'Groups' + expect(page).to have_content(first_partner_group.name) + expect(page).to have_content(first_partner.name) + expect(page).not_to have_link('Delete') + end + end + + context 'when the partner group has no partners' do + before do + PartnerGroup.destroy_all + end + let!(:second_partner_group) { create(:partner_group, organization: organization) } + it 'should display the delete link and allow deletion' do + visit partners_path + click_on 'Groups' + expect(page).to have_content(second_partner_group.name) + expect(page).to have_link('Delete') + expect do + accept_confirm do + click_link "Delete" + end + end.to change { PartnerGroup.count }.by(-1) + expect(page).not_to have_content(second_partner_group.name) + end + end + end end end end