Skip to content

Commit

Permalink
Merge pull request rubyforgood#560 from rubyforgood/supervisor_dashboard
Browse files Browse the repository at this point in the history
Supervisor dashboard performance
  • Loading branch information
compwron authored Aug 20, 2020
2 parents 871366a + f732fc0 commit d5191f3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def show

# Return all active/inactive volunteers, inactive will be filtered by default
@volunteers = policy_scope(
Volunteer.includes(:case_assignments, :casa_cases, versions: [:item])
Volunteer.includes(:supervisor, :case_assignments, :case_contacts, :casa_cases, versions: [:item])
).decorate

@casa_cases = policy_scope(CasaCase.includes(:case_assignments, :volunteers).all)
@casa_cases = policy_scope(CasaCase.includes(:case_assignments, :volunteers))

@case_contacts = policy_scope(
CaseContact.all
).order(occurred_at: :desc).decorate

@supervisors = policy_scope(Supervisor.all)
@supervisors = policy_scope(Supervisor.includes(:supervisor_volunteers, :volunteers))
end
end
13 changes: 12 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,24 @@ def case_contacts_for(casa_case_id)
end

def recent_contacts_made(days_counter = 60)
case_contacts.where(contact_made: true, occurred_at: days_counter.days.ago..Date.today).count
case_contacts.where(contact_made: true, occurred_at: days_counter.days.ago..Date.today).size
end

def most_recent_contact
case_contacts.where(contact_made: true).order(:occurred_at).last
end

def volunteers_serving_transistion_aged_youth
volunteers.includes(:casa_cases)
.where(casa_cases: {transition_aged_youth: true}).size
end

def no_contact_for_two_weeks
volunteers.includes(:case_contacts)
.where(case_contacts: {contact_made: true})
.where.not(case_contacts: { occurred_at: 14.days.ago..Date.today}).size
end

def past_names
# get past_names from paper_trail gem, version_limit is 10 so no performance concerns
versions.map { |version| version&.reify&.display_name }
Expand Down
9 changes: 3 additions & 6 deletions app/views/dashboard/_supervisors_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@
<td id="name">
<%= link_to(supervisor.display_name, edit_supervisor_path(supervisor)) %>
</td>
<td id="volunteer-assignments"> <%= supervisor.volunteers.count %></td>
<td id="volunteer-assignments"> <%= supervisor.volunteers.size %></td>
<td id="serving-transition-aged-youth">
<%= supervisor.volunteers.select { |v| v.serving_transition_aged_youth? }.count %>
<%= supervisor.volunteers_serving_transistion_aged_youth %>
</td>
<td id="no-contact">
<%=
days_since_contact = 14
supervisor.volunteers.select { |v| v.recent_contacts_made(days_since_contact) < 1 }.count
%>
<%= supervisor.no_contact_for_two_weeks %>
</td>
<td><%= link_to 'Edit', edit_supervisor_path(supervisor) %></td>
</tr>
Expand Down
31 changes: 29 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@
expect(result.length).to eq(1)
end

describe "supervisors" do
context "#volunteers_serving_transistion_aged_youth" do
it 'returns the number of transition aged youth on a supervisor' do
assignment1 = create(:case_assignment, casa_case: create(:casa_case, transition_aged_youth: true))
assignment2 = create(:case_assignment, casa_case: create(:casa_case, transition_aged_youth: true))
assignment3 = create(:case_assignment, casa_case: create(:casa_case, transition_aged_youth: false))
supervisor = create(:supervisor)
create(:volunteer, case_assignments: [assignment1], supervisor: supervisor)
create(:volunteer, case_assignments: [assignment2], supervisor: supervisor)
create(:volunteer, case_assignments: [assignment3], supervisor: supervisor)
expect(supervisor.volunteers_serving_transistion_aged_youth).to eq(2)
end
end

context "#no_contact_for_two_weeks" do
it 'returns the number of volunteers who have not made contact in over 2 weeks' do
supervisor = create(:supervisor)

volunteer = create(:volunteer, :with_casa_cases, supervisor: supervisor)

case_of_interest = volunteer.casa_cases.first
create(:case_contact, creator: volunteer, casa_case: case_of_interest, contact_made: true, occurred_at: 3.weeks.ago)
expect(supervisor.no_contact_for_two_weeks).to eq(1)
end
end
end

describe "#active_for_authentication?" do
it "is false when the user is inactive" do
user = create(:volunteer, :inactive)
Expand Down Expand Up @@ -73,8 +100,8 @@
context "when the user has a transition-aged-youth case" do
it "is true" do
case_assignments = [
case_assignment_with_a_transition_aged_youth,
case_assignment_without_transition_aged_youth
case_assignment_with_a_transition_aged_youth,
case_assignment_without_transition_aged_youth
]
user = create(:volunteer, case_assignments: case_assignments)

Expand Down

0 comments on commit d5191f3

Please sign in to comment.