From fe27da42e42585ea061ec709755d07f9f95f2da4 Mon Sep 17 00:00:00 2001 From: Aaryan <53212802+Aaryanpal@users.noreply.github.com> Date: Sat, 21 Sep 2024 01:22:12 +0530 Subject: [PATCH] 4634 - Add the Delete button on the Partner Group Page (#4649) * Add the Delete button on the Partner Group Page * Replace Destroyed with Deleted * Resolve Comments * Added Request Spec for PartnerGroup and remove corresponding system-spec * Fix lint * fix the request-spec --- app/controllers/partner_groups_controller.rb | 18 +++++++- .../partners/_partner_groups_table.html.erb | 3 +- config/routes.rb | 2 +- spec/requests/partner_group_spec.rb | 46 +++++++++++++++++++ 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 spec/requests/partner_group_spec.rb diff --git a/app/controllers/partner_groups_controller.rb b/app/controllers/partner_groups_controller.rb index f54339d95c..b6ae82dc00 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_group, 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,23 @@ def update end end + def destroy + if @partner_group.partners.any? + redirect_to partners_path + "#nav-partner-groups", alert: "Partner Group cannot be deleted." + else + @partner_group.destroy + respond_to do |format| + format.html { redirect_to partners_path + "#nav-partner-groups", notice: "Partner Group was successfully deleted." } + end + end + end + private + def set_partner_group + @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..94778851c3 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)}) if pg.partners.none? %> <% 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/requests/partner_group_spec.rb b/spec/requests/partner_group_spec.rb new file mode 100644 index 0000000000..f91dbc1dcf --- /dev/null +++ b/spec/requests/partner_group_spec.rb @@ -0,0 +1,46 @@ +RSpec.describe "PartnerGroups", type: :request do + let(:user) { create(:user) } + let(:partner_group) { create(:partner_group) } + + before do + sign_in(user) + end + + describe "DELETE #destroy" do + context "when partner group has no partners" do + let!(:partner_group) { create(:partner_group) } + before { get partners_path + "#nav-partner-groups" } + it "destroys the partner group" do + within "#nav-partner-groups" do + expect(response.body).to have_link("Delete") + end + expect { + delete partner_group_path(partner_group) + }.to change(PartnerGroup, :count).by(-1) + + expect(flash[:notice]).to eq("Partner Group was successfully deleted.") + expect(response).to redirect_to(partners_path + "#nav-partner-groups") + end + end + + context "when partner group has partners" do + let!(:partner_group) { create(:partner_group) } + + before do + create(:partner, partner_group: partner_group) + get partners_path + "#nav-partner-groups" + end + it "does not destroy the partner group" do + within "#nav-partner-groups" do + expect(reponse.body).not_to have_link("Delete") + end + expect { + delete partner_group_path(partner_group) + }.not_to change(PartnerGroup, :count) + + expect(flash[:alert]).to eq("Partner Group cannot be deleted.") + expect(response).to redirect_to(partners_path + "#nav-partner-groups") + end + end + end +end