Skip to content

Commit

Permalink
Merge pull request rubyforgood#1192 from rubyforgood/rachelwyatt/1132…
Browse files Browse the repository at this point in the history
…-inactivate-case-from-edit-case-page

inactivate and reactivate case from edit case page
  • Loading branch information
compwron authored Oct 26, 2020
2 parents de50f47 + 1a06768 commit 51d10bf
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 11 deletions.
22 changes: 22 additions & 0 deletions app/controllers/casa_cases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ def update
end
end

def deactivate
casa_case = CasaCase.find(params[:id])
# TODO: authorize action with pundit
flash_message = "Case #{casa_case.case_number} has been deactivated."
if casa_case.deactivate
redirect_to edit_casa_case_path(casa_case), notice: flash_message
else
render :edit
end
end

def reactivate
casa_case = CasaCase.find(params[:id])
# TODO: authorize action w/ Pundit
flash_message = "Case #{casa_case.case_number} has been reactivated."
if casa_case.reactivate
redirect_to edit_casa_case_path(casa_case), notice: flash_message
else
render :edit
end
end

# DELETE /casa_cases/1
# DELETE /casa_cases/1.json
def destroy
Expand Down
10 changes: 10 additions & 0 deletions app/models/casa_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ def clear_court_dates
court_report_submitted: false)
end

def deactivate
update(active: false)
case_assignments.map { |ca| ca.update(is_active: false) }
end

def reactivate
update(active: true)
end

private

def validate_date(day, month, year)
Expand Down Expand Up @@ -108,6 +117,7 @@ def parse_date(date_field_name, args)
# Table name: casa_cases
#
# id :bigint not null, primary key
# active :boolean default(TRUE), not null
# birth_month_year_youth :datetime
# case_number :string not null
# court_date :datetime
Expand Down
4 changes: 4 additions & 0 deletions app/policies/casa_case_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def update_contact_types?
user.is_a?(Supervisor)
end

def update_case_status?
user.is_a?(CasaAdmin)
end

def update_court_date?
user.casa_admin? || user.supervisor?
end
Expand Down
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 @@ -127,7 +127,12 @@
<br>

<div class="actions">
<%= form.submit casa_case.persisted? ? 'Update CASA Case' : 'Create CASA Case', class: "btn btn-primary" %>
<% if casa_case.active %>
<%= form.submit casa_case.persisted? ? 'Update CASA Case' : 'Create CASA Case', class: "btn btn-primary" %>
<%= link_to 'Deactivate CASA Case', deactivate_casa_case_path(casa_case), method: :patch, class: "btn btn-outline-danger pull-right", data: { confirm: "Deactivating a CASA Case will unassign any volunteers currently assigned to this case. Are you sure you'd like to continue?"} if Pundit.policy(current_user, casa_case).update_case_status? && casa_case.persisted? %>
<% else %>
<%= link_to 'Reactivate CASA Case', reactivate_casa_case_path(casa_case), method: :patch, class: "btn btn-primary" if Pundit.policy(current_user, casa_case).update_case_status? %>
<% end %>
</div>
<% end %>
</div>
Expand Down
4 changes: 4 additions & 0 deletions app/views/casa_cases/_inactive_case.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="alert alert-danger">
<p>Case was deactivated on: <%= @casa_case.updated_at.strftime("%m-%d-%Y") %>
<%= link_to 'Reactivate CASA Case', reactivate_casa_case_path(@casa_case), method: :patch, class: "btn btn-primary pull-right" if Pundit.policy(current_user, @casa_case).update_case_status? %></p>
</div>
6 changes: 5 additions & 1 deletion app/views/casa_cases/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<%= link_to "Back", casa_cases_path %>
<h1>Editing CASA Case</h1>

<%= render 'form', casa_case: @casa_case %>
<% if !@casa_case.active%>
<%= render 'inactive_case', casa_case: @casa_case%>
<% else %>
<%= render 'form', casa_case: @casa_case %>
<% end %>

<% if Pundit.policy(current_user, @casa_case).assign_volunteers? %>
<%= render "volunteer_assignment" %>
Expand Down
8 changes: 6 additions & 2 deletions app/views/casa_cases/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
</thead>
<tbody>
<% @casa_cases.each do |casa_case| %>
<tr>
<tr class=<%= !casa_case.active ? "table-secondary" : "" %> >
<td><%= link_to(casa_case.case_number, casa_case) %></td>
<td><%= casa_case.hearing_type_name %></td>
<td><%= casa_case.judge_name %></td>
<td><%= casa_case.decorate.transition_aged_youth_icon %></td>
<td>
<%= safe_join(casa_case&.volunteers&.map { |vol| link_to(vol.display_name, edit_volunteer_path(vol)) },
<% if casa_case.active? %>
<%= safe_join(casa_case&.volunteers&.map { |vol| link_to(vol.display_name, edit_volunteer_path(vol)) },
", ") %>
<% else %>
Case was deactivated on: <%= casa_case.updated_at.strftime("%m-%d-%Y") %>
<% end %>
</td>
<td><%= link_to 'Detail View', casa_case_path(casa_case) %></td>
<td><%= link_to 'Edit', edit_casa_case_path(casa_case) %></td>
Expand Down
9 changes: 7 additions & 2 deletions app/views/casa_cases/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
<div class="row">
<div class="col-sm-12 form-header">
<h1><%= @casa_case.decorate.transition_aged_youth_only_icon %> CASA Case Details</h1>
<%- if policy(:case_contact).new? %>
<%- if policy(:case_contact).new? & @casa_case.active? %>
<%= link_to "New Case Contact",
new_case_contact_path(case_contact: {casa_case_id: @casa_case.id}),
class: "btn btn-primary" %>
<%- end %>
<%= link_to 'Edit Case Details', edit_casa_case_path(@casa_case), class: "btn btn-primary" %>
<% if @casa_case.active? %>
<%= link_to 'Edit Case Details', edit_casa_case_path(@casa_case), class: "btn btn-primary" %>
<% end %>
</div>
</div>

<div class="card card-container">
<div class="card-body">
<% if !@casa_case.active%>
<%= render "inactive_case", casa_case: @casa_case%>
<% end %>
<p>
<h6><strong>Case number:</strong> <%= @casa_case.case_number %></h6>
</p>
Expand Down
6 changes: 4 additions & 2 deletions app/views/volunteers/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@
<td><%= link_to assignment.casa_case.case_number, casa_case_path(assignment.casa_case) %></td>
<td><%= assignment.casa_case.decorate.transition_aged_youth_icon %></td>
<td>
<% if @volunteer.active? %>
<% if @volunteer.active? && assignment.casa_case.active? %>
Volunteer is <span class="badge badge-success text-uppercase display-1">Active</span>
<% elsif @volunteer.active? %>
Case was deactivated on: <%= assignment.casa_case.updated_at.strftime("%m-%d-%Y") %>
<% else %>
Deactivated
<% end %>
</td>
<td>
<% if @volunteer.active? %>
<% if @volunteer.active? && assignment.casa_case.active? %>
<%- if Pundit.policy(current_user, @volunteer).unassign_case? %>
<%= button_to 'Unassign Case',
case_assignment_path(assignment, volunteer_id: @volunteer.id),
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
root to: "all_casa_admins/sessions#new", as: :unauthenticated_all_casa_root
end

resources :casa_cases
resources :casa_cases do
member do
patch :deactivate
patch :reactivate
end
end

resources :casa_admins, except: %i[destroy] do
member do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddActiveColumnToCasaCasesTable < ActiveRecord::Migration[6.0]
def change
add_column :casa_cases, :active, :boolean, default: true, null: false
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
t.boolean "court_report_submitted", default: false, null: false
t.datetime "court_date"
t.datetime "court_report_due_date"
t.boolean "active", default: true, null: false
t.bigint "hearing_type_id"
t.bigint "judge_id"
t.index ["casa_org_id"], name: "index_casa_cases_on_casa_org_id"
Expand Down
10 changes: 10 additions & 0 deletions spec/system/admin_edit_volunteer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@

expect(inactive_volunteer.reload).to be_active
end

context "with a deactivated case" do
it "displays inactive message" do
deactivated_casa_case = create(:casa_case, active: false, volunteers: [volunteer])
sign_in admin

visit edit_volunteer_path(volunteer)
expect(page).to have_text "Case was deactivated on: #{deactivated_casa_case.updated_at.strftime("%m-%d-%Y")}"
end
end
end
38 changes: 36 additions & 2 deletions spec/system/admin_edits_a_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
let!(:school) { create(:contact_type, name: "School", contact_type_group: contact_type_group) }
let!(:therapist) { create(:contact_type, name: "Therapist", contact_type_group: contact_type_group) }

it "clicks back button after editing case" do
before do
sign_in admin
end

it "clicks back button after editing case" do
visit edit_casa_case_path(casa_case)
check "Court report submitted"
has_checked_field? :court_report_submitted
Expand All @@ -20,7 +23,6 @@
end

it "edits case" do
sign_in admin
visit casa_case_path(casa_case.id)
click_on "Edit Case Details"
has_no_checked_field? :court_report_submitted
Expand All @@ -33,5 +35,37 @@
expect(page).to have_text("Day")
expect(page).to have_text("Month")
expect(page).to have_text("Year")
expect(page).not_to have_text("Deactivate Case")
end

it "deactivates a case" do
visit edit_casa_case_path(casa_case)

click_on "Deactivate CASA Case"
page.driver.browser.switch_to.alert.accept

expect(page).to have_text("Case #{casa_case.case_number} has been deactivated")
expect(page).to have_text("Case was deactivated on: #{casa_case.updated_at.strftime("%m-%d-%Y")}")
expect(page).to have_text("Reactivate CASA Case")
expect(page).to_not have_text("Court Date")
expect(page).to_not have_text("Court Report Due Date")
expect(page).to_not have_text("Day")
expect(page).to_not have_text("Month")
expect(page).to_not have_text("Year")
end

it "reactivates a case" do
visit edit_casa_case_path(casa_case)
click_on "Deactivate CASA Case"
page.driver.browser.switch_to.alert.accept
click_on "Reactivate CASA Case"

expect(page).to have_text("Case #{casa_case.case_number} has been reactivated.")
expect(page).to have_text("Deactivate CASA Case")
expect(page).to have_text("Court Date")
expect(page).to have_text("Court Report Due Date")
expect(page).to have_text("Day")
expect(page).to have_text("Month")
expect(page).to have_text("Year")
end
end
14 changes: 14 additions & 0 deletions spec/system/supervisor_edits_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@
expect(page).to have_text("Court date was not a valid date.")
expect(page).to have_text("Court report due date was not a valid date.")
end

it "views deactivated case" do
casa_case.deactivate
visit edit_casa_case_path(casa_case)

expect(page).to have_text("Case was deactivated on: #{casa_case.updated_at.strftime("%m-%d-%Y")}")
expect(page).not_to have_text("Court Date")
expect(page).not_to have_text("Court Report Due Date")
expect(page).not_to have_text("Day")
expect(page).not_to have_text("Month")
expect(page).not_to have_text("Year")
expect(page).not_to have_text("Reactivate Case")
expect(page).not_to have_text("Update Casa Case")
end
end
1 change: 1 addition & 0 deletions spec/system/volunteer_edits_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
expect(page).not_to have_text("Day")
expect(page).not_to have_text("Month")
expect(page).not_to have_text("Year")
expect(page).not_to have_text("Deactivate Case")

visit casa_case_path(casa_case)
expect(page).to have_text("Court Report Submission: Submitted")
Expand Down

0 comments on commit 51d10bf

Please sign in to comment.