Skip to content

Commit 73ce2aa

Browse files
authored
926 Staff Dashboard - make overdue tasks the default shown table (rubyforgood#943)
* Added table name and set Overdue table as default in Dashboard * Linted the code * Added PetAvatarComponent for displaying Pet's image * Move common query logic to PetTaskable concern Rename table name Update tests to reflect new table name Lint the code * Cleaned up unnecessary code * Rename method for improved context clarity
1 parent 87c602c commit 73ce2aa

File tree

10 files changed

+103
-91
lines changed

10 files changed

+103
-91
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module PetTaskable
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
scope :with_overdue_tasks, -> {
6+
left_joins(:tasks)
7+
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
8+
.where(tasks: {completed: false})
9+
.where("tasks.due_date < ?", Time.current)
10+
.group("pets.id")
11+
}
12+
13+
scope :with_incomplete_tasks, -> {
14+
left_joins(:tasks)
15+
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
16+
.where(tasks: {completed: false})
17+
.where("tasks.due_date IS NULL OR tasks.due_date >= ?", Time.current)
18+
.group("pets.id")
19+
}
20+
end
21+
end
Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Organizations::Staff::DashboardController < Organizations::BaseController
2-
before_action :context_authorize!, only: %i[index incomplete_tasks overdue_tasks]
2+
before_action :context_authorize!, only: %i[index pets_with_incomplete_tasks pets_with_overdue_tasks]
3+
before_action :set_pets_with_overdue_tasks, only: %i[index pets_with_overdue_tasks]
4+
before_action :set_pets_with_incomplete_tasks, only: :pets_with_incomplete_tasks
35
include Pagy::Backend
46
layout "dashboard"
57

@@ -14,41 +16,21 @@ def index
1416
@under_review_count = Pet.filter_by_application_status("under_review").count
1517
end
1618

17-
def incomplete_tasks
18-
@pagy, @pets = pagy(
19-
Pet
20-
.left_joins(:tasks)
21-
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
22-
.where(tasks: {completed: false})
23-
.where("tasks.due_date IS NULL OR tasks.due_date >= ?", Time.current)
24-
.group("pets.id"),
25-
items: 5
26-
)
27-
@column_name = "Incomplete Tasks"
19+
def pets_with_incomplete_tasks
2820
respond_to do |format|
2921
format.turbo_stream do
30-
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/tasks", locals: {column_name: @column_name})
22+
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/pets_with_incomplete_or_overdue_tasks")
3123
end
32-
format.html { render :tasks, locals: {column_name: @column_name} }
24+
format.html { render :tasks }
3325
end
3426
end
3527

36-
def overdue_tasks
37-
@pagy, @pets = pagy(
38-
Pet
39-
.left_joins(:tasks)
40-
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
41-
.where(tasks: {completed: false})
42-
.where("tasks.due_date < ?", Time.current)
43-
.group("pets.id"),
44-
items: 5
45-
)
46-
@column_name = "Overdue Tasks"
28+
def pets_with_overdue_tasks
4729
respond_to do |format|
4830
format.turbo_stream do
49-
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/tasks", locals: {column_name: @column_name})
31+
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/pets_with_incomplete_or_overdue_tasks")
5032
end
51-
format.html { render :tasks, locals: {column_name: @column_name} }
33+
format.html { render :tasks }
5234
end
5335
end
5436

@@ -58,4 +40,16 @@ def context_authorize!
5840
authorize! :dashboard,
5941
context: {organization: Current.organization}
6042
end
43+
44+
def set_pets_with_overdue_tasks
45+
@pagy, @pets = pagy(Pet.with_overdue_tasks, limit: 5)
46+
@column_name = "Count"
47+
@header_title = "Overdue Pet Tasks"
48+
end
49+
50+
def set_pets_with_incomplete_tasks
51+
@pagy, @pets = pagy(Pet.with_incomplete_tasks, limit: 5)
52+
@column_name = "Incomplete Tasks"
53+
@header_title = "Incomplete Pet Tasks"
54+
end
6155
end

app/models/pet.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
# fk_rails_... (organization_id => organizations.id)
2929
#
3030
class Pet < ApplicationRecord
31+
include PetTaskable
32+
3133
acts_as_tenant(:organization)
3234

3335
has_many :adopter_applications, dependent: :destroy

app/policies/organizations/dashboard_policy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ def index?
66
permission?(:view_organization_dashboard)
77
end
88

9-
def incomplete_tasks?
9+
def pets_with_incomplete_tasks?
1010
permission?(:view_organization_dashboard)
1111
end
1212

13-
def overdue_tasks?
13+
def pets_with_overdue_tasks?
1414
permission?(:view_organization_dashboard)
1515
end
1616
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<turbo-frame id="tasks-frame">
2+
<h3 class = "text-capitalize"><%= @header_title%></h3>
3+
<div class="card">
4+
<table class="table mb-0 text-nowrap table-hover table-centered">
5+
<thead>
6+
<tr>
7+
<th scope="col">Name</th>
8+
<th class="text-center" scope="col">Sex</th>
9+
<th class="text-center" scope="col"><%= @column_name %></th>
10+
</tr>
11+
</thead>
12+
<tbody>
13+
<% @pets.each do |pet| %>
14+
<tr>
15+
<td>
16+
<div class="d-flex align-items-center">
17+
<%= render PetAvatarComponent.new(pet)%>
18+
<div class="ms-3">
19+
<h4 class="mb-0">
20+
<%= link_to pet.name, staff_pet_path(pet), class: 'text-inherit', data: { turbo: false } %>
21+
</h4>
22+
</div>
23+
</div>
24+
</td>
25+
<td>
26+
<div class="d-flex justify-content-center">
27+
<%= pet.sex %>
28+
</div>
29+
</td>
30+
<td>
31+
<div class="d-flex justify-content-center">
32+
<%= link_to pet.incomplete_tasks_count, staff_pet_path(pet, active_tab: 'tasks'), class: 'text-inherit', data: { turbo: false } %>
33+
</div>
34+
</td>
35+
</tr>
36+
<% end %>
37+
</tbody>
38+
</table>
39+
</div>
40+
<div class="d-flex justify-content-center align-items-center mt-2">
41+
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
42+
</div>
43+
</turbo-frame>

app/views/organizations/staff/dashboard/index.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
<div>
1111
<div>
1212
<%= link_to "#{@not_completed_not_overdue_tasks_count} Incomplete",
13-
incomplete_tasks_staff_dashboard_index_path,
13+
pets_with_incomplete_tasks_staff_dashboard_index_path,
1414
data: { turbo_frame: "tasks-frame" }
1515
%>
1616
</div>
1717
<div>
1818
<%= link_to "#{@not_completed_overdue_tasks_count} Overdue",
19-
overdue_tasks_staff_dashboard_index_path,
19+
pets_with_overdue_tasks_staff_dashboard_index_path,
2020
data: { turbo_frame: "tasks-frame" }
2121
%>
2222
</div>
@@ -59,6 +59,6 @@
5959
<% end %>
6060
</div>
6161
</div>
62-
<%= turbo_frame_tag "tasks-frame" %>
62+
<%= render 'pets_with_incomplete_or_overdue_tasks' %>
6363
<% end %>
6464
<% end %>
Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1 @@
1-
<turbo-frame id="tasks-frame">
2-
<div class="card">
3-
<table class="table mb-0 text-nowrap table-hover table-centered">
4-
<thead>
5-
<tr>
6-
<th scope="col">Name</th>
7-
<th class="text-center" scope="col">Sex</th>
8-
<th class="text-center" scope="col"><%= column_name %></th>
9-
</tr>
10-
</thead>
11-
<tbody>
12-
<% @pets.each do |pet| %>
13-
<tr>
14-
<td>
15-
<div class="d-flex align-items-center">
16-
<div class="icon-shape icon-lg rounded-3 border">
17-
<% if pet.images.attached? %>
18-
<%= image_tag pet.images.first, class: 'card-img' %>
19-
<% else %>
20-
<%= image_tag('coming_soon.jpg', class: 'card-img') %>
21-
<% end %>
22-
</div>
23-
<div class="ms-3">
24-
<h4 class="mb-0">
25-
<%= link_to pet.name, staff_pet_path(pet), class: 'text-inherit', data: { turbo: false } %>
26-
</h4>
27-
</div>
28-
</div>
29-
</td>
30-
<td>
31-
<div class="d-flex justify-content-center">
32-
<%= pet.sex %>
33-
</div>
34-
</td>
35-
<td>
36-
<div class="d-flex justify-content-center">
37-
<%= link_to pet.incomplete_tasks_count, staff_pet_path(pet, active_tab: 'tasks'), class: 'text-inherit', data: { turbo: false } %>
38-
</div>
39-
</td>
40-
</tr>
41-
<% end %>
42-
</tbody>
43-
</table>
44-
</div>
45-
<div class="d-flex justify-content-center align-items-center mt-2">
46-
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
47-
</div>
48-
</turbo-frame>
1+
<%= render 'pets_with_incomplete_or_overdue_tasks'%>

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
resources :faqs
2626
resources :dashboard, only: [:index] do
2727
collection do
28-
get :incomplete_tasks
29-
get :overdue_tasks
28+
get :pets_with_incomplete_tasks
29+
get :pets_with_overdue_tasks
3030
end
3131
end
3232
resources :matches, only: %i[create destroy]

test/controllers/organizations/staff/dashboard_controller_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class Organizations::Staff::DashboardControllerTest < ActionDispatch::Integratio
2727
context "#incomplete_tasks" do
2828
should "be authorized" do
2929
assert_authorized_to(
30-
:incomplete_tasks?, :dashboard,
30+
:pets_with_incomplete_tasks?, :dashboard,
3131
context: {organization: @organization},
3232
with: Organizations::DashboardPolicy
3333
) do
34-
get incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
34+
get pets_with_incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
3535
end
3636
end
3737

3838
should "return turbo_stream response" do
39-
get incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
39+
get pets_with_incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
4040
assert_response :success
4141
assert_match "tasks-frame", response.body
4242
end
@@ -45,16 +45,16 @@ class Organizations::Staff::DashboardControllerTest < ActionDispatch::Integratio
4545
context "#overdue_tasks" do
4646
should "be authorized" do
4747
assert_authorized_to(
48-
:overdue_tasks?, :dashboard,
48+
:pets_with_overdue_tasks?, :dashboard,
4949
context: {organization: @organization},
5050
with: Organizations::DashboardPolicy
5151
) do
52-
get overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
52+
get pets_with_overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
5353
end
5454
end
5555

5656
should "return turbo_stream response" do
57-
get overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
57+
get pets_with_overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
5858
assert_response :success
5959
assert_match "tasks-frame", response.body
6060
end

test/system/dashboard_test.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class DashboardTest < ApplicationSystemTestCase
1919
test "viewing incomplete tasks" do
2020
click_link "Incomplete"
2121
assert_selector "table"
22-
assert_text "Incomplete Tasks"
23-
22+
assert_text "Incomplete Pet Tasks"
2423
within "table" do
2524
@pets.each do |pet|
2625
assert_text pet.name
@@ -32,7 +31,7 @@ class DashboardTest < ApplicationSystemTestCase
3231
test "viewing overdue tasks" do
3332
click_link "Overdue"
3433
assert_selector "table"
35-
assert_text "Overdue Tasks"
34+
assert_text "Overdue Pet Tasks"
3635

3736
within "table" do
3837
@pets.each do |pet|

0 commit comments

Comments
 (0)