Skip to content

Commit

Permalink
Merge pull request rubyforgood#2004 from librod89/lr-1369-hide-unassi…
Browse files Browse the repository at this point in the history
…gned-volunteers

Hide/Show unassigned volunteers on Supervisor Edit page
  • Loading branch information
compwron authored May 7, 2021
2 parents c451ff7 + 7953eda commit 56a9416
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
10 changes: 9 additions & 1 deletion app/controllers/supervisors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class SupervisorsController < ApplicationController
before_action :available_volunteers, only: [:edit, :update]
before_action :set_supervisor, only: [:edit, :update, :activate, :deactivate]
before_action :all_volunteers_ever_assigned, only: [:edit, :update]
before_action :all_volunteers_ever_assigned, only: [:update]
before_action :supervisor_has_unassigned_volunteers, only: [:edit]
after_action :verify_authorized

def index
Expand All @@ -30,6 +31,9 @@ def create

def edit
authorize @supervisor
if params[:include_unassigned] == "true"
all_volunteers_ever_assigned
end
end

def update
Expand Down Expand Up @@ -73,6 +77,10 @@ def all_volunteers_ever_assigned
@all_volunteers_ever_assigned = @supervisor.volunteers_ever_assigned
end

def supervisor_has_unassigned_volunteers
@supervisor_has_unassigned_volunteers = @supervisor.volunteers_ever_assigned.count > @supervisor.volunteers.count
end

def available_volunteers
@available_volunteers = Volunteer.with_no_supervisor(current_user.casa_org)
end
Expand Down
14 changes: 11 additions & 3 deletions app/views/supervisors/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@

<div class="card card-container">
<div class="card-body">
<% if @all_volunteers_ever_assigned.any? %>
<h3>Assigned Volunteers</h3>
<% if (@all_volunteers_ever_assigned || @supervisor.volunteers).any? %>
<h3 class="pull-left">Assigned Volunteers</h3>
<% if @supervisor_has_unassigned_volunteers %>
<% button_text = @all_volunteers_ever_assigned.nil? ? "Include unassigned" : "Hide unassigned" %>
<%= button_to button_text,
edit_supervisor_path(@supervisor),
params: { include_unassigned: @all_volunteers_ever_assigned.nil? },
method: :get,
class: "btn btn-outline-primary pull-right" %>
<% end %>
<table class='table volunteer-list'>
<thead>
<tr>
Expand All @@ -56,7 +64,7 @@
</tr>
</thead>
<tbody>
<% @all_volunteers_ever_assigned.each do |volunteer| %>
<% (@all_volunteers_ever_assigned || @supervisor.volunteers).each do |volunteer| %>
<tr>
<td><%= link_to volunteer.display_name, edit_volunteer_path(volunteer) %></td>
<td><%= volunteer.email %></td>
Expand Down
18 changes: 18 additions & 0 deletions spec/requests/supervisors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@

expect(response).to be_successful
end

it "returns volunteers ever assigned if include_unassigned param is present" do
sign_in admin

get edit_supervisor_url(supervisor), params: {include_unassigned: true}

expect(response).to be_successful
expect(assigns(:all_volunteers_ever_assigned)).to_not be_nil
end

it "returns no volunteers ever assigned if include_unassigned param is false" do
sign_in admin

get edit_supervisor_url(supervisor), params: {include_unassigned: false}

expect(response).to be_successful
expect(assigns(:all_volunteers_ever_assigned)).to be_nil
end
end

describe "PATCH /update" do
Expand Down
30 changes: 30 additions & 0 deletions spec/system/supervisors/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,36 @@
expect(page).to have_text("There are no active, unassigned volunteers available.")
end
end

context "when there are assigned volunteers" do
let(:supervisor) { create(:supervisor, :with_volunteers, casa_org: organization) }

it "shows assigned volunteers" do
visit edit_supervisor_path(supervisor)

expect(page).to have_text "Assigned Volunteers"
expect(page).to_not have_button("Include unassigned")
supervisor.volunteers.each do |volunteer|
expect(page).to have_text volunteer.email
end
end

context "when there are previously unassigned volunteers" do
let!(:unassigned_volunteer) { create(:supervisor_volunteer, :inactive, supervisor: supervisor).volunteer }

it "does not show them by default" do
visit edit_supervisor_path(supervisor)

expect(page).to_not have_text unassigned_volunteer.email
expect(page).to have_button("Include unassigned")

click_on "Include unassigned"

expect(page).to have_button("Hide unassigned")
expect(page).to have_text unassigned_volunteer.email
end
end
end
end
end
end

0 comments on commit 56a9416

Please sign in to comment.