From 669eb441bd87b07a7187afa47255830f8641f91e Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 13 Mar 2023 16:43:13 +0100
Subject: [PATCH] filters for orgas / people

---
 app/controllers/admin/university/organizations_controller.rb | 1 +
 app/controllers/admin/university/people_controller.rb        | 1 +
 app/models/university/organization.rb                        | 1 +
 app/models/university/person.rb                              | 1 +
 app/services/filters/admin/university/organizations.rb       | 5 +++++
 app/services/filters/admin/university/people.rb              | 5 +++++
 config/locales/en.yml                                        | 1 +
 config/locales/fr.yml                                        | 1 +
 8 files changed, 16 insertions(+)

diff --git a/app/controllers/admin/university/organizations_controller.rb b/app/controllers/admin/university/organizations_controller.rb
index 9f297faf3..47fa31a3b 100644
--- a/app/controllers/admin/university/organizations_controller.rb
+++ b/app/controllers/admin/university/organizations_controller.rb
@@ -4,6 +4,7 @@ class Admin::University::OrganizationsController < Admin::University::Applicatio
                               through_association: :organizations
 
   has_scope :for_search_term
+  has_scope :for_category
   has_scope :for_kind
 
   def index
diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb
index eff89dcb3..6ad5728a0 100644
--- a/app/controllers/admin/university/people_controller.rb
+++ b/app/controllers/admin/university/people_controller.rb
@@ -5,6 +5,7 @@ class Admin::University::PeopleController < Admin::University::ApplicationContro
 
 
   has_scope :for_search_term
+  has_scope :for_category
   has_scope :for_role
 
   def index
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index 3541ed39a..ada7676b2 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -74,6 +74,7 @@ class University::Organization < ApplicationRecord
 
   scope :ordered, -> { order(:name) }
   scope :for_kind, -> (kind) { where(kind: kind) }
+  scope :for_category, -> (category_id) { includes(:categories).where(categories: { id: category_id })}
   scope :for_search_term, -> (term) {
     where("
       unaccent(university_organizations.address) ILIKE unaccent(:term) OR
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 9532be84e..f101aa1dd 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -141,6 +141,7 @@ 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_category, -> (category_id) { includes(:categories).where(categories: { id: category_id })}
   scope :for_program, -> (program_id) {
     left_joins(:education_programs_as_administrator, :education_programs_as_teacher)
       .where(education_programs: { id: program_id })
diff --git a/app/services/filters/admin/university/organizations.rb b/app/services/filters/admin/university/organizations.rb
index bfd4cbb4f..117954fb7 100644
--- a/app/services/filters/admin/university/organizations.rb
+++ b/app/services/filters/admin/university/organizations.rb
@@ -3,6 +3,11 @@ module Filters
     def initialize(user)
       super
       add_search
+      if user.university.organizations_categories.any?
+        add :for_category,
+            user.university.organizations_categories.ordered,
+            I18n.t('filters.attributes.category')
+      end
       add :for_kind,
           ::University::Organization::kinds.keys.map { |r| { to_s: I18n.t("enums.university.organization.kind.#{r}"), id: r } },
           I18n.t('filters.attributes.kind')
diff --git a/app/services/filters/admin/university/people.rb b/app/services/filters/admin/university/people.rb
index 138f8291d..6b8d329ce 100644
--- a/app/services/filters/admin/university/people.rb
+++ b/app/services/filters/admin/university/people.rb
@@ -3,6 +3,11 @@ module Filters
     def initialize(user)
       super
       add_search
+      if user.university.people_categories.any?
+        add :for_category,
+            user.university.people_categories.ordered,
+            I18n.t('filters.attributes.category')
+      end
       add :for_role,
           ::University::Person::LIST_OF_ROLES.map { |r| { to_s: I18n.t("activerecord.attributes.university/person.#{r}"), id: r } },
           I18n.t('filters.attributes.role')
diff --git a/config/locales/en.yml b/config/locales/en.yml
index d579c70fc..85a703d66 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -190,6 +190,7 @@ en:
     remove: Remove image
   filters:
     attributes:
+      category: Filter by category
       date: Filter by date
       element: Filter by %{element}
       kind: Filter by kind
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 4f1a549dc..1e441444b 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -190,6 +190,7 @@ fr:
     remove: Supprimer l'image
   filters:
     attributes:
+      category: Filtrer par catégorie
       date: Filtrer par date
       element: Filtrer par %{element}
       kind: Filtrer par type
-- 
GitLab