From 67813928da94f0fb45b2709850c6fe8c79433385 Mon Sep 17 00:00:00 2001 From: Leni Kadali <52788034+lenikadali@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:15:40 +0300 Subject: [PATCH 1/2] Add scope, variable to templates Added a new scope to SupervisorVolunteer so that we can return the learning hours of *all* volunteers (including those without hours entered) Also added the supervisor_volunteer variable to the templates so that we use it instead of @learning_hour --- app/models/supervisor_volunteer.rb | 8 ++++++++ .../_supervisor_admin_learning_hours.html.erb | 2 +- app/views/learning_hours/index.html.erb | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/supervisor_volunteer.rb b/app/models/supervisor_volunteer.rb index a4dfbd9e1b..90c6f8006d 100644 --- a/app/models/supervisor_volunteer.rb +++ b/app/models/supervisor_volunteer.rb @@ -7,6 +7,14 @@ class SupervisorVolunteer < ApplicationRecord validates :volunteer_id, uniqueness: {scope: :is_active}, if: :is_active? validate :ensure_supervisor_and_volunteer_belong_to_same_casa_org, if: -> { supervisor.present? && volunteer.present? } + scope :supervisor_volunteers_learning_hours, ->(supervisor_id) { + left_outer_joins(volunteer: [{supervisor: :learning_hours}]) + .where(supervisor_volunteers: {supervisor_id: supervisor_id}) + .select("users.id as user_id, users.display_name, SUM(learning_hours.duration_minutes + learning_hours.duration_hours * 60) AS total_time_spent") + .group("users.display_name, users.id") + .order("users.display_name") + } + private def ensure_supervisor_and_volunteer_belong_to_same_casa_org diff --git a/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb b/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb index 3eb8ee3140..da43723ef8 100644 --- a/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb +++ b/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb @@ -23,7 +23,7 @@ - <% @learning_hours.each do |learning_hour| %> + <% supervisor_volunteers.each do |learning_hour| %> <%= link_to(learning_hour.display_name, learning_hours_volunteer_path(learning_hour.user_id)) %> <%= format_time(learning_hour.total_time_spent) %> diff --git a/app/views/learning_hours/index.html.erb b/app/views/learning_hours/index.html.erb index 3ba5ba2b3f..0b49c3562d 100644 --- a/app/views/learning_hours/index.html.erb +++ b/app/views/learning_hours/index.html.erb @@ -1,5 +1,5 @@ <% if current_user.volunteer? %> <%= render "volunteer_learning_hours", learning_hours: @learning_hours %> <% else %> - <%= render "supervisor_admin_learning_hours" %> + <%= render "supervisor_admin_learning_hours" supervisor_volunteer: supervisor_volunteer %> <% end %> From 05acdf57d838bd58dd84eb9f65b369252c696723 Mon Sep 17 00:00:00 2001 From: Leni Kadali <52788034+lenikadali@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:08:57 +0300 Subject: [PATCH 2/2] Revert SupervisorVolunteer changes, use LearningHour Reverted the changes made to the SupervisorVolunteer model and switched to use LearningHour as originally intended --- app/models/learning_hour.rb | 2 +- app/models/supervisor_volunteer.rb | 8 -------- .../_supervisor_admin_learning_hours.html.erb | 2 +- app/views/learning_hours/index.html.erb | 2 +- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/app/models/learning_hour.rb b/app/models/learning_hour.rb index 8ea0e97a92..483800f94a 100644 --- a/app/models/learning_hour.rb +++ b/app/models/learning_hour.rb @@ -17,7 +17,7 @@ class LearningHour < ApplicationRecord validates :learning_hour_topic, presence: true, if: :user_org_learning_topic_enable? scope :supervisor_volunteers_learning_hours, ->(supervisor_id) { - joins(user: :supervisor_volunteer) + left_outer_joins(user: [{ supervisor_volunteer: {supervisor: :learning_hours} }]) .where(supervisor_volunteers: {supervisor_id: supervisor_id}) .select("users.id as user_id, users.display_name, SUM(learning_hours.duration_minutes + learning_hours.duration_hours * 60) AS total_time_spent") .group("users.display_name, users.id") diff --git a/app/models/supervisor_volunteer.rb b/app/models/supervisor_volunteer.rb index 90c6f8006d..a4dfbd9e1b 100644 --- a/app/models/supervisor_volunteer.rb +++ b/app/models/supervisor_volunteer.rb @@ -7,14 +7,6 @@ class SupervisorVolunteer < ApplicationRecord validates :volunteer_id, uniqueness: {scope: :is_active}, if: :is_active? validate :ensure_supervisor_and_volunteer_belong_to_same_casa_org, if: -> { supervisor.present? && volunteer.present? } - scope :supervisor_volunteers_learning_hours, ->(supervisor_id) { - left_outer_joins(volunteer: [{supervisor: :learning_hours}]) - .where(supervisor_volunteers: {supervisor_id: supervisor_id}) - .select("users.id as user_id, users.display_name, SUM(learning_hours.duration_minutes + learning_hours.duration_hours * 60) AS total_time_spent") - .group("users.display_name, users.id") - .order("users.display_name") - } - private def ensure_supervisor_and_volunteer_belong_to_same_casa_org diff --git a/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb b/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb index da43723ef8..3eb8ee3140 100644 --- a/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb +++ b/app/views/learning_hours/_supervisor_admin_learning_hours.html.erb @@ -23,7 +23,7 @@ - <% supervisor_volunteers.each do |learning_hour| %> + <% @learning_hours.each do |learning_hour| %> <%= link_to(learning_hour.display_name, learning_hours_volunteer_path(learning_hour.user_id)) %> <%= format_time(learning_hour.total_time_spent) %> diff --git a/app/views/learning_hours/index.html.erb b/app/views/learning_hours/index.html.erb index 0b49c3562d..3ba5ba2b3f 100644 --- a/app/views/learning_hours/index.html.erb +++ b/app/views/learning_hours/index.html.erb @@ -1,5 +1,5 @@ <% if current_user.volunteer? %> <%= render "volunteer_learning_hours", learning_hours: @learning_hours %> <% else %> - <%= render "supervisor_admin_learning_hours" supervisor_volunteer: supervisor_volunteer %> + <%= render "supervisor_admin_learning_hours" %> <% end %>