From 750c17ed4b3fdae9c0f8bdb8d3145825f525316f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 28 Apr 2022 18:31:19 +0200
Subject: [PATCH] wip scope for program

---
 app/models/education/school.rb                 |  2 +-
 app/models/university/person.rb                | 17 +++++++++++------
 app/models/university/person/involvement.rb    |  4 ++++
 app/models/university/person/with_education.rb | 13 ++++++-------
 app/models/university/person/with_roles.rb     | 15 +++++++++++++++
 5 files changed, 37 insertions(+), 14 deletions(-)
 create mode 100644 app/models/university/person/with_roles.rb

diff --git a/app/models/education/school.rb b/app/models/education/school.rb
index 663bf2e00..f46defd6f 100644
--- a/app/models/education/school.rb
+++ b/app/models/education/school.rb
@@ -91,7 +91,7 @@ class Education::School < ApplicationRecord
   validates :name, :address, :city, :zipcode, :country, presence: true
 
   scope :ordered, -> { order(:name) }
-  scope :for_program, -> (program_id) { } # TODO @Sebou
+  scope :for_program, -> (program_id) { joins(:programs).where(education_programs: { id: program_id }) }
   scope :for_search_term, -> (term) {
     where("
       unaccent(education_schools.address) ILIKE unaccent(:term) OR
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 6e791092f..6e497a1bf 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -44,6 +44,7 @@ class University::Person < ApplicationRecord
   include WithBlobs
   include WithSlug
   include WithPicture
+  include WithRoles
   include WithEducation
 
   LIST_OF_ROLES = [
@@ -77,11 +78,6 @@ class University::Person < ApplicationRecord
                           class_name: 'University::Person::Involvement',
                           dependent: :destroy
 
-  has_many                :involvements_as_administrator,
-                          -> { where(kind: 'administrator') },
-                          class_name: 'University::Person::Involvement',
-                          dependent: :destroy
-
   has_many                :author_websites,
                           -> { distinct },
                           through: :communication_website_posts,
@@ -117,7 +113,16 @@ class University::Person < ApplicationRecord
   scope :researchers,     -> { where(is_researcher: true) }
   scope :alumni,          -> { where(is_alumnus: true) }
   scope :for_role, -> (role) { where("is_#{role}": true) }
-  scope :for_program, -> (program_id) { } # TODO @Sebou
+  scope :for_program, -> (program_id) {
+    left_joins(:education_programs_as_administrator, :education_programs_as_teacher)
+      .where(education_programs: { id: program_id })
+      .or(
+        left_joins(:education_programs_as_administrator, :education_programs_as_teacher)
+          .where(education_programs_as_teachers_university_people: { id: program_id })
+      )
+      .select("university_people.*")
+      .distinct
+  }
   scope :for_search_term, -> (term) {
     where("
       unaccent(concat(university_people.first_name, ' ', university_people.last_name)) ILIKE unaccent(:term) OR
diff --git a/app/models/university/person/involvement.rb b/app/models/university/person/involvement.rb
index 988be24c9..735c3f481 100644
--- a/app/models/university/person/involvement.rb
+++ b/app/models/university/person/involvement.rb
@@ -32,6 +32,10 @@ class University::Person::Involvement < ApplicationRecord
 
   belongs_to :person, class_name: 'University::Person'
   belongs_to :target, polymorphic: true
+  belongs_to  :university_role,
+              -> { where(university_person_involvements: { target_type: 'University::Role' }) },
+              class_name: "University::Role",
+              foreign_key: "target_id"
 
   validates :person_id, uniqueness: { scope: [:target_id, :target_type] }
   validates :target_id, uniqueness: { scope: [:person_id, :target_type] }
diff --git a/app/models/university/person/with_education.rb b/app/models/university/person/with_education.rb
index 7ceb01ad4..eef6b9c79 100644
--- a/app/models/university/person/with_education.rb
+++ b/app/models/university/person/with_education.rb
@@ -12,6 +12,12 @@ module University::Person::WithEducation
                             source: :target,
                             source_type: "Education::Program"
 
+    has_many                :education_programs_as_administrator,
+                            -> { distinct },
+                            through: :roles_as_administrator,
+                            source: :target,
+                            source_type: "Education::Program"
+
     has_many                :experiences
 
     has_and_belongs_to_many :cohorts,
@@ -30,13 +36,6 @@ module University::Person::WithEducation
                             association_foreign_key: 'education_program_id'
   end
 
-  def education_programs_as_administrator
-    university.education_programs
-              .joins(:involvements_through_roles)
-              .where(university_person_involvements: { person_id: id })
-              .distinct
-  end
-
   def add_to_cohort(cohort)
     cohorts << cohort unless cohort.in?(cohorts)
     diploma_years << cohort.academic_year unless cohort.academic_year.in? diploma_years
diff --git a/app/models/university/person/with_roles.rb b/app/models/university/person/with_roles.rb
new file mode 100644
index 000000000..d9742809f
--- /dev/null
+++ b/app/models/university/person/with_roles.rb
@@ -0,0 +1,15 @@
+module University::Person::WithRoles
+  extend ActiveSupport::Concern
+
+  included do
+    has_many                :involvements_as_administrator,
+                            -> { where(kind: 'administrator') },
+                            class_name: 'University::Person::Involvement',
+                            dependent: :destroy
+
+    has_many                :roles_as_administrator,
+                            through: :involvements_as_administrator,
+                            source: :target,
+                            source_type: "University::Role"
+  end
+end
-- 
GitLab