Skip to content

Commit

Permalink
926 Staff Dashboard - make overdue tasks the default shown table (rub…
Browse files Browse the repository at this point in the history
…yforgood#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
  • Loading branch information
Aaryanpal authored Sep 3, 2024
1 parent 87c602c commit 73ce2aa
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 91 deletions.
21 changes: 21 additions & 0 deletions app/controllers/concerns/pet_taskable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module PetTaskable
extend ActiveSupport::Concern

included do
scope :with_overdue_tasks, -> {
left_joins(:tasks)
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
.where(tasks: {completed: false})
.where("tasks.due_date < ?", Time.current)
.group("pets.id")
}

scope :with_incomplete_tasks, -> {
left_joins(:tasks)
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
.where(tasks: {completed: false})
.where("tasks.due_date IS NULL OR tasks.due_date >= ?", Time.current)
.group("pets.id")
}
end
end
48 changes: 21 additions & 27 deletions app/controllers/organizations/staff/dashboard_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Organizations::Staff::DashboardController < Organizations::BaseController
before_action :context_authorize!, only: %i[index incomplete_tasks overdue_tasks]
before_action :context_authorize!, only: %i[index pets_with_incomplete_tasks pets_with_overdue_tasks]
before_action :set_pets_with_overdue_tasks, only: %i[index pets_with_overdue_tasks]
before_action :set_pets_with_incomplete_tasks, only: :pets_with_incomplete_tasks
include Pagy::Backend
layout "dashboard"

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

def incomplete_tasks
@pagy, @pets = pagy(
Pet
.left_joins(:tasks)
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
.where(tasks: {completed: false})
.where("tasks.due_date IS NULL OR tasks.due_date >= ?", Time.current)
.group("pets.id"),
items: 5
)
@column_name = "Incomplete Tasks"
def pets_with_incomplete_tasks
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/tasks", locals: {column_name: @column_name})
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/pets_with_incomplete_or_overdue_tasks")
end
format.html { render :tasks, locals: {column_name: @column_name} }
format.html { render :tasks }
end
end

def overdue_tasks
@pagy, @pets = pagy(
Pet
.left_joins(:tasks)
.select("pets.*, COUNT(tasks.id) AS incomplete_tasks_count")
.where(tasks: {completed: false})
.where("tasks.due_date < ?", Time.current)
.group("pets.id"),
items: 5
)
@column_name = "Overdue Tasks"
def pets_with_overdue_tasks
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/tasks", locals: {column_name: @column_name})
render turbo_stream: turbo_stream.replace("tasks-frame", partial: "organizations/staff/dashboard/pets_with_incomplete_or_overdue_tasks")
end
format.html { render :tasks, locals: {column_name: @column_name} }
format.html { render :tasks }
end
end

Expand All @@ -58,4 +40,16 @@ def context_authorize!
authorize! :dashboard,
context: {organization: Current.organization}
end

def set_pets_with_overdue_tasks
@pagy, @pets = pagy(Pet.with_overdue_tasks, limit: 5)
@column_name = "Count"
@header_title = "Overdue Pet Tasks"
end

def set_pets_with_incomplete_tasks
@pagy, @pets = pagy(Pet.with_incomplete_tasks, limit: 5)
@column_name = "Incomplete Tasks"
@header_title = "Incomplete Pet Tasks"
end
end
2 changes: 2 additions & 0 deletions app/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
# fk_rails_... (organization_id => organizations.id)
#
class Pet < ApplicationRecord
include PetTaskable

acts_as_tenant(:organization)

has_many :adopter_applications, dependent: :destroy
Expand Down
4 changes: 2 additions & 2 deletions app/policies/organizations/dashboard_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def index?
permission?(:view_organization_dashboard)
end

def incomplete_tasks?
def pets_with_incomplete_tasks?
permission?(:view_organization_dashboard)
end

def overdue_tasks?
def pets_with_overdue_tasks?
permission?(:view_organization_dashboard)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<turbo-frame id="tasks-frame">
<h3 class = "text-capitalize"><%= @header_title%></h3>
<div class="card">
<table class="table mb-0 text-nowrap table-hover table-centered">
<thead>
<tr>
<th scope="col">Name</th>
<th class="text-center" scope="col">Sex</th>
<th class="text-center" scope="col"><%= @column_name %></th>
</tr>
</thead>
<tbody>
<% @pets.each do |pet| %>
<tr>
<td>
<div class="d-flex align-items-center">
<%= render PetAvatarComponent.new(pet)%>
<div class="ms-3">
<h4 class="mb-0">
<%= link_to pet.name, staff_pet_path(pet), class: 'text-inherit', data: { turbo: false } %>
</h4>
</div>
</div>
</td>
<td>
<div class="d-flex justify-content-center">
<%= pet.sex %>
</div>
</td>
<td>
<div class="d-flex justify-content-center">
<%= link_to pet.incomplete_tasks_count, staff_pet_path(pet, active_tab: 'tasks'), class: 'text-inherit', data: { turbo: false } %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="d-flex justify-content-center align-items-center mt-2">
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
</div>
</turbo-frame>
6 changes: 3 additions & 3 deletions app/views/organizations/staff/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
<div>
<div>
<%= link_to "#{@not_completed_not_overdue_tasks_count} Incomplete",
incomplete_tasks_staff_dashboard_index_path,
pets_with_incomplete_tasks_staff_dashboard_index_path,
data: { turbo_frame: "tasks-frame" }
%>
</div>
<div>
<%= link_to "#{@not_completed_overdue_tasks_count} Overdue",
overdue_tasks_staff_dashboard_index_path,
pets_with_overdue_tasks_staff_dashboard_index_path,
data: { turbo_frame: "tasks-frame" }
%>
</div>
Expand Down Expand Up @@ -59,6 +59,6 @@
<% end %>
</div>
</div>
<%= turbo_frame_tag "tasks-frame" %>
<%= render 'pets_with_incomplete_or_overdue_tasks' %>
<% end %>
<% end %>
49 changes: 1 addition & 48 deletions app/views/organizations/staff/dashboard/tasks.html.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
<turbo-frame id="tasks-frame">
<div class="card">
<table class="table mb-0 text-nowrap table-hover table-centered">
<thead>
<tr>
<th scope="col">Name</th>
<th class="text-center" scope="col">Sex</th>
<th class="text-center" scope="col"><%= column_name %></th>
</tr>
</thead>
<tbody>
<% @pets.each do |pet| %>
<tr>
<td>
<div class="d-flex align-items-center">
<div class="icon-shape icon-lg rounded-3 border">
<% if pet.images.attached? %>
<%= image_tag pet.images.first, class: 'card-img' %>
<% else %>
<%= image_tag('coming_soon.jpg', class: 'card-img') %>
<% end %>
</div>
<div class="ms-3">
<h4 class="mb-0">
<%= link_to pet.name, staff_pet_path(pet), class: 'text-inherit', data: { turbo: false } %>
</h4>
</div>
</div>
</td>
<td>
<div class="d-flex justify-content-center">
<%= pet.sex %>
</div>
</td>
<td>
<div class="d-flex justify-content-center">
<%= link_to pet.incomplete_tasks_count, staff_pet_path(pet, active_tab: 'tasks'), class: 'text-inherit', data: { turbo: false } %>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="d-flex justify-content-center align-items-center mt-2">
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
</div>
</turbo-frame>
<%= render 'pets_with_incomplete_or_overdue_tasks'%>
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
resources :faqs
resources :dashboard, only: [:index] do
collection do
get :incomplete_tasks
get :overdue_tasks
get :pets_with_incomplete_tasks
get :pets_with_overdue_tasks
end
end
resources :matches, only: %i[create destroy]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class Organizations::Staff::DashboardControllerTest < ActionDispatch::Integratio
context "#incomplete_tasks" do
should "be authorized" do
assert_authorized_to(
:incomplete_tasks?, :dashboard,
:pets_with_incomplete_tasks?, :dashboard,
context: {organization: @organization},
with: Organizations::DashboardPolicy
) do
get incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
get pets_with_incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
end
end

should "return turbo_stream response" do
get incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
get pets_with_incomplete_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
assert_response :success
assert_match "tasks-frame", response.body
end
Expand All @@ -45,16 +45,16 @@ class Organizations::Staff::DashboardControllerTest < ActionDispatch::Integratio
context "#overdue_tasks" do
should "be authorized" do
assert_authorized_to(
:overdue_tasks?, :dashboard,
:pets_with_overdue_tasks?, :dashboard,
context: {organization: @organization},
with: Organizations::DashboardPolicy
) do
get overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
get pets_with_overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
end
end

should "return turbo_stream response" do
get overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
get pets_with_overdue_tasks_staff_dashboard_index_url, headers: {"Turbo-Frame" => "tasks-frame"}
assert_response :success
assert_match "tasks-frame", response.body
end
Expand Down
5 changes: 2 additions & 3 deletions test/system/dashboard_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class DashboardTest < ApplicationSystemTestCase
test "viewing incomplete tasks" do
click_link "Incomplete"
assert_selector "table"
assert_text "Incomplete Tasks"

assert_text "Incomplete Pet Tasks"
within "table" do
@pets.each do |pet|
assert_text pet.name
Expand All @@ -32,7 +31,7 @@ class DashboardTest < ApplicationSystemTestCase
test "viewing overdue tasks" do
click_link "Overdue"
assert_selector "table"
assert_text "Overdue Tasks"
assert_text "Overdue Pet Tasks"

within "table" do
@pets.each do |pet|
Expand Down

0 comments on commit 73ce2aa

Please sign in to comment.