Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1733] Add option to remove Court Mandates #1802

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/controllers/case_court_mandates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CaseCourtMandatesController < ApplicationController
before_action :set_case_court_mandate, only: %i[destroy]
before_action :require_organization!
after_action :verify_authorized

def destroy
authorize @case_court_mandate
@case_court_mandate.destroy
end

private

def set_case_court_mandate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be private

@case_court_mandate = CaseCourtMandate.find(params[:id])
rescue ActiveRecord::RecordNotFound
head :not_found
end
end
51 changes: 51 additions & 0 deletions app/javascript/src/casa_case.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,57 @@ id="casa_case_case_court_mandates_attributes_1_mandate_text">\
$(list).children(':last').trigger('focus')
}

function remove_mandate_with_confirmation () {
Swal.fire({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swal!

icon: 'warning',
title: 'Delete court mandate?',
text: 'Are you sure you want to remove this court mandate? Doing so will \
delete all records of it unless it was included in a previous court report.',

showCloseButton: true,
showCancelButton: true,
focusConfirm: false,

confirmButtonColor: '#d33',
cancelButtonColor: '#39c',

confirmButtonText: 'Delete',
cancelButtonText: 'Go back'
}).then((result) => {
if (result.isConfirmed) {
remove_mandate_action($(this))
}
})
}

function remove_mandate_action (ctx) {
id_element = ctx.parent().next('input[type="hidden"]')
id = id_element.val()

$.ajax({
url: `/case_court_mandates/${id}`,
method: 'delete',
success: () => {
ctx.parent().remove()
id_element.remove() // Remove form element since this mandate has been deleted

Swal.fire({
icon: 'success',
text: 'Court mandate has been removed.',
showCloseButton: true
})
},
error: () => {
Swal.fire({
icon: 'error',
text: 'Something went wrong when attempting to delete this court mandate.',
showCloseButton: true
})
}
})
}

$('document').ready(() => {
$('button#add-mandate-button').on('click', add_court_mandate_input)
$('button.remove-mandate-button').on('click', remove_mandate_with_confirmation)
})
30 changes: 24 additions & 6 deletions app/javascript/src/stylesheets/pages/casa_cases.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,38 @@ body.casa_cases {
gap: 10px;
}

#add-mandate-button {
background-color: #{$primary};
#add-mandate-button, .remove-mandate-button {
color: white;
border: none;

font-size: 1.25em;

padding: 0.25em 1.5em;
border-radius: 10px;
border: none;

:hover {
cursor: pointer;
}
}

.court-mandate-entry {
display: flex;
gap: 8px;
align-items: center;

.remove-mandate-button {
background-color: #{$red};

border-radius: 100%;

max-width: 30px;
max-height: 30px;
}
}

#add-mandate-button {
background-color: #{$primary};

padding: 0.25em 1.5em;
border-radius: 10px;
}
}

@media only screen and (max-width: 640px) {
Expand Down
3 changes: 3 additions & 0 deletions app/policies/case_court_mandate_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CaseCourtMandatePolicy < ApplicationPolicy
alias_method :destroy?, :admin_or_supervisor?
end
7 changes: 6 additions & 1 deletion app/views/casa_cases/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@
<% if policy(casa_case).update_court_mandates? %>
<div id="mandates-list-container">
<%= form.fields_for :case_court_mandates do |ff| %>
<%= ff.text_area :mandate_text %>
<div class="court-mandate-entry">
<%= ff.text_area :mandate_text %>
<button type="button" class="remove-mandate-button">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
</div>
<% end %>
</div>
<div class="add-court-mandate-container">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
patch :unassign
end
end
resources :case_court_mandates, only: %i[destroy]

namespace :all_casa_admins do
resources :casa_orgs, only: [:new, :create, :show] do
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/casa_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
end
end

trait :with_one_court_mandate do
after(:create) do |casa_case|
casa_case.case_court_mandates << build(:case_court_mandate)
casa_case.save
end
end

trait :active do
active { true }
end
Expand Down
58 changes: 58 additions & 0 deletions spec/requests/case_court_mandates_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "rails_helper"

RSpec.describe "/case_court_mandates", type: :request do
subject(:delete_request) { delete case_court_mandate_url(case_court_mandate) }
let(:case_court_mandate) { build(:case_court_mandate) }

before do
sign_in user
casa_case = create(:casa_case)
casa_case.case_court_mandates << case_court_mandate
end

describe "as an admin" do
let(:user) { create(:casa_admin) }

describe "DELETE /destroy" do
it "renders a successful response" do
delete_request
expect(response).to be_successful
end

it "deletes the court mandate" do
expect { delete_request }.to change(CaseCourtMandate, :count).from(1).to(0)
end
end
end

describe "as a supervisor" do
let(:user) { create(:supervisor) }

describe "DELETE /destroy" do
it "renders a successful response" do
delete_request
expect(response).to be_successful
end

it "deletes the court mandate" do
expect { delete_request }.to change(CaseCourtMandate, :count).from(1).to(0)
end
end
end

describe "as a volunteer" do
let(:user) { create(:volunteer) }

describe "DELETE /destroy" do
it "renders a successful response" do
delete_request
# CASA will attempt to redirect to another page
expect(response.status).to be(302)
end

it "deletes the court mandate" do
expect { delete_request }.to_not change(CaseCourtMandate, :count)
end
end
end
end
23 changes: 23 additions & 0 deletions spec/system/casa_cases/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,29 @@ def sign_in_and_assign_volunteer
expect(page).to have_text(volunteer_2.display_name)
end
end

context "deleting court mandates", js: true do
let(:casa_case) { create(:casa_case, :with_one_court_mandate) }
let(:mandate_text) { casa_case.case_court_mandates.first.mandate_text }

it "can delete a court mandate" do
visit edit_casa_case_path(casa_case.id)

expect(page).to have_text(mandate_text)

find("i.fa-minus").click
expect(page).to have_text("Are you sure you want to remove this court mandate? Doing so will delete all records \
of it unless it was included in a previous court report.")

click_on "Delete"
expect(page).to have_text("Court mandate has been removed.")
click_on "OK"
expect(page).to_not have_text(mandate_text)

click_on "Update CASA Case"
expect(page).to_not have_text(mandate_text)
end
end
end

context "when volunteer" do
Expand Down