Skip to content

Commit

Permalink
Add the Delete button on the Partner Group Page
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaryanpal committed Sep 13, 2024
1 parent badeff0 commit bccadb5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
15 changes: 14 additions & 1 deletion app/controllers/partner_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/views/partners/_partner_groups_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
<span class='text-gray-600 text-bold font-italic'>No</span>
<% end %>
</td>
<td class="text-right">
<td class="text-left">
<%= 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? %>
</td>
</tr>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions spec/system/partner_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bccadb5

Please sign in to comment.