From 73ce2aadbc431e9dfd0a83d860098603d5c35457 Mon Sep 17 00:00:00 2001
From: Aaryan <53212802+Aaryanpal@users.noreply.github.com>
Date: Tue, 3 Sep 2024 23:21:46 +0530
Subject: [PATCH] 926 Staff Dashboard - make overdue tasks the default shown
table (#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
---
app/controllers/concerns/pet_taskable.rb | 21 ++++++++
.../staff/dashboard_controller.rb | 48 ++++++++----------
app/models/pet.rb | 2 +
.../organizations/dashboard_policy.rb | 4 +-
..._with_incomplete_or_overdue_tasks.html.erb | 43 ++++++++++++++++
.../staff/dashboard/index.html.erb | 6 +--
.../staff/dashboard/tasks.html.erb | 49 +------------------
config/routes.rb | 4 +-
.../staff/dashboard_controller_test.rb | 12 ++---
test/system/dashboard_test.rb | 5 +-
10 files changed, 103 insertions(+), 91 deletions(-)
create mode 100644 app/controllers/concerns/pet_taskable.rb
create mode 100644 app/views/organizations/staff/dashboard/_pets_with_incomplete_or_overdue_tasks.html.erb
diff --git a/app/controllers/concerns/pet_taskable.rb b/app/controllers/concerns/pet_taskable.rb
new file mode 100644
index 000000000..6354035a1
--- /dev/null
+++ b/app/controllers/concerns/pet_taskable.rb
@@ -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
diff --git a/app/controllers/organizations/staff/dashboard_controller.rb b/app/controllers/organizations/staff/dashboard_controller.rb
index 8e7a81eda..fde448347 100644
--- a/app/controllers/organizations/staff/dashboard_controller.rb
+++ b/app/controllers/organizations/staff/dashboard_controller.rb
@@ -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"
@@ -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
@@ -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
diff --git a/app/models/pet.rb b/app/models/pet.rb
index 2d30987ac..b0723c14f 100644
--- a/app/models/pet.rb
+++ b/app/models/pet.rb
@@ -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
diff --git a/app/policies/organizations/dashboard_policy.rb b/app/policies/organizations/dashboard_policy.rb
index cc81e04ef..37c528a81 100644
--- a/app/policies/organizations/dashboard_policy.rb
+++ b/app/policies/organizations/dashboard_policy.rb
@@ -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
diff --git a/app/views/organizations/staff/dashboard/_pets_with_incomplete_or_overdue_tasks.html.erb b/app/views/organizations/staff/dashboard/_pets_with_incomplete_or_overdue_tasks.html.erb
new file mode 100644
index 000000000..979515931
--- /dev/null
+++ b/app/views/organizations/staff/dashboard/_pets_with_incomplete_or_overdue_tasks.html.erb
@@ -0,0 +1,43 @@
+<%= @header_title%>
+
+
+
+
+
+
+
+ <% @pets.each do |pet| %>
+ Name
+ Sex
+ <%= @column_name %>
+
+
+ <% end %>
+
+
+
+
+ <%= link_to pet.name, staff_pet_path(pet), class: 'text-inherit', data: { turbo: false } %>
+
+
+
+
+
+
Name | -Sex | -<%= column_name %> | -
---|---|---|
-
-
-
- <% if pet.images.attached? %>
- <%= image_tag pet.images.first, class: 'card-img' %>
- <% else %>
- <%= image_tag('coming_soon.jpg', class: 'card-img') %>
- <% end %>
-
-
-
- - <%= link_to pet.name, staff_pet_path(pet), class: 'text-inherit', data: { turbo: false } %> -- |
-
-
- <%= pet.sex %>
-
- |
-
-
- <%= link_to pet.incomplete_tasks_count, staff_pet_path(pet, active_tab: 'tasks'), class: 'text-inherit', data: { turbo: false } %>
-
- |
-