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| %> + + + + + + <% end %> + +
NameSex<%= @column_name %>
+
+ <%= render PetAvatarComponent.new(pet)%> +
+

+ <%= 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 } %> +
+
+
+
+ <%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %> +
+
\ No newline at end of file diff --git a/app/views/organizations/staff/dashboard/index.html.erb b/app/views/organizations/staff/dashboard/index.html.erb index 42ee375fa..9b859b9cc 100644 --- a/app/views/organizations/staff/dashboard/index.html.erb +++ b/app/views/organizations/staff/dashboard/index.html.erb @@ -10,13 +10,13 @@
<%= 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" } %>
<%= 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" } %>
@@ -59,6 +59,6 @@ <% end %>
- <%= turbo_frame_tag "tasks-frame" %> + <%= render 'pets_with_incomplete_or_overdue_tasks' %> <% end %> <% end %> diff --git a/app/views/organizations/staff/dashboard/tasks.html.erb b/app/views/organizations/staff/dashboard/tasks.html.erb index 91a36e9b5..52b9aef28 100644 --- a/app/views/organizations/staff/dashboard/tasks.html.erb +++ b/app/views/organizations/staff/dashboard/tasks.html.erb @@ -1,48 +1 @@ - -
- - - - - - - - - - <% @pets.each do |pet| %> - - - - - - <% end %> - -
NameSex<%= 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 } %> -
-
-
-
- <%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %> -
-
\ No newline at end of file + <%= render 'pets_with_incomplete_or_overdue_tasks'%> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 355315170..ebd02e04c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/test/controllers/organizations/staff/dashboard_controller_test.rb b/test/controllers/organizations/staff/dashboard_controller_test.rb index 2e94a2ad8..3faea4a77 100644 --- a/test/controllers/organizations/staff/dashboard_controller_test.rb +++ b/test/controllers/organizations/staff/dashboard_controller_test.rb @@ -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 @@ -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 diff --git a/test/system/dashboard_test.rb b/test/system/dashboard_test.rb index 04f30b38c..7e06782ac 100644 --- a/test/system/dashboard_test.rb +++ b/test/system/dashboard_test.rb @@ -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 @@ -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|