From a4802eaead32799452068aadce673b81beed608b Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 29 Aug 2023 17:02:34 +0200
Subject: [PATCH] Connect researchers to lab

---
 .../admin/university/people_controller.rb     |  4 +--
 app/models/research/laboratory.rb             | 15 ++++++++--
 app/models/university/person/with_research.rb |  6 ++++
 .../research/laboratories/_list.html.erb      | 18 ++++++++++++
 .../admin/research/laboratories/show.html.erb | 29 ++++++++++++-------
 .../admin/university/people/_form.html.erb    |  4 +++
 .../people/experiences/_list.html.erb         |  2 +-
 .../people/researchers/_list.html.erb         | 18 ++++++++++++
 .../admin/university/people/show.html.erb     |  4 +++
 config/locales/university/en.yml              |  1 +
 config/locales/university/fr.yml              |  1 +
 ...rch_laboratories_university_researchers.rb |  8 +++++
 db/schema.rb                                  |  9 +++++-
 13 files changed, 102 insertions(+), 17 deletions(-)
 create mode 100644 app/views/admin/research/laboratories/_list.html.erb
 create mode 100644 app/views/admin/university/people/researchers/_list.html.erb
 create mode 100644 db/migrate/20230829141647_create_join_table_research_laboratories_university_researchers.rb

diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb
index 8ac8268ef..520f5f43e 100644
--- a/app/controllers/admin/university/people_controller.rb
+++ b/app/controllers/admin/university/people_controller.rb
@@ -108,11 +108,11 @@ class Admin::University::PeopleController < Admin::University::ApplicationContro
       :slug, :first_name, :last_name, :email, :gender, :birthdate,
       :phone_mobile, :phone_professional, :phone_personal,
       :address, :zipcode, :city, :country,
-      :meta_description, :summary,
+      :meta_description, :summary, 
       :biography,  :picture, :picture_delete, :picture_infos,
       :habilitation, :tenure, :url, :linkedin, :twitter, :mastodon,
       :is_researcher, :is_teacher, :is_administration, :is_alumnus, :user_id,
-      category_ids: []
+      research_laboratory_ids: [], category_ids: []
     ).merge(university_id: current_university.id)
   end
 end
diff --git a/app/models/research/laboratory.rb b/app/models/research/laboratory.rb
index eb0f676f3..9dc177cd3 100644
--- a/app/models/research/laboratory.rb
+++ b/app/models/research/laboratory.rb
@@ -34,9 +34,14 @@ class Research::Laboratory < ApplicationRecord
               dependent: :nullify
   has_many    :axes,
               class_name: 'Research::Laboratory::Axis',
-              foreign_key: :research_laboratory_id,
+              foreign_key: 'research_laboratory_id',
               dependent: :destroy
 
+  has_and_belongs_to_many :researchers,
+                          class_name: 'University::Person::Researcher',
+                          foreign_key: 'university_person_id',
+                          association_foreign_key: 'research_laboratory_id'
+
   validates :name, :address, :city, :zipcode, :country, presence: true
 
   scope :ordered, -> { order(:name) }
@@ -62,13 +67,17 @@ class Research::Laboratory < ApplicationRecord
     "data/laboratory.yml"
   end
 
+  def dependencies
+    axes +
+    researchers.map(&:researcher)
+  end
+
   def has_administrators?
     false
   end
 
   def has_researchers?
-    # TODO: Ajouter les researchers quand ils existeront
-    false
+    researchers.any?
   end
 
   def has_teachers?
diff --git a/app/models/university/person/with_research.rb b/app/models/university/person/with_research.rb
index f9102227b..7ac31c98c 100644
--- a/app/models/university/person/with_research.rb
+++ b/app/models/university/person/with_research.rb
@@ -25,6 +25,12 @@ module University::Person::WithResearch
                             foreign_key: 'director_id',
                             dependent: :nullify
 
+    has_and_belongs_to_many :research_laboratories,
+                            class_name: 'Research::Laboratory',
+                            foreign_key: 'research_laboratory_id',
+                            association_foreign_key: 'university_person_id'
+    alias :laboratories :research_laboratories
+
     scope :with_hal_identifier, -> { where.not(hal_form_identifier: [nil,'']) }
   end
 
diff --git a/app/views/admin/research/laboratories/_list.html.erb b/app/views/admin/research/laboratories/_list.html.erb
new file mode 100644
index 000000000..5fed163e1
--- /dev/null
+++ b/app/views/admin/research/laboratories/_list.html.erb
@@ -0,0 +1,18 @@
+<div class="table-responsive">
+  <table class="<%= table_classes(with_actions: false) %>">
+    <thead>
+      <tr>
+        <th><%= Research::Laboratory.human_attribute_name('name') %></th>
+        <th><%= Research::Laboratory.human_attribute_name('address') %></th>
+      </tr>
+    </thead>
+    <tbody>
+      <% laboratories.each do |laboratory| %>
+        <tr>
+          <td><%= link_to laboratory, admin_research_laboratory_path(laboratory) %></td>
+          <td><%= laboratory.full_address %></td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+</div>
diff --git a/app/views/admin/research/laboratories/show.html.erb b/app/views/admin/research/laboratories/show.html.erb
index 388c3b317..cc7823ae6 100644
--- a/app/views/admin/research/laboratories/show.html.erb
+++ b/app/views/admin/research/laboratories/show.html.erb
@@ -1,15 +1,24 @@
 <% content_for :title, @laboratory %>
 
-<%
-action = link_to    t('create'),
-                    new_admin_research_laboratory_axis_path(laboratory_id: @laboratory),
-                    class: button_classes
-subtitle = link_to  t('communication.website.see_all', number: @axes.count),
-                    admin_research_laboratory_axes_path(laboratory_id: @laboratory)
-%>
-<%= osuny_panel Research::Laboratory::Axis.model_name.human(count: 2), action: action, subtitle: subtitle do %>
-  <%= render 'admin/research/laboratories/axes/list', axes: @axes %>
-<% end %>
+<div class="row">
+  <div class="col-lg-6">
+    <%
+    action = link_to    t('create'),
+                        new_admin_research_laboratory_axis_path(laboratory_id: @laboratory),
+                        class: button_classes
+    subtitle = link_to  t('communication.website.see_all', number: @axes.count),
+                        admin_research_laboratory_axes_path(laboratory_id: @laboratory)
+    %>
+    <%= osuny_panel Research::Laboratory::Axis.model_name.human(count: 2), action: action, subtitle: subtitle do %>
+      <%= render 'admin/research/laboratories/axes/list', axes: @axes %>
+    <% end %>
+  </div>
+  <div class="col-lg-6">
+    <%= osuny_panel University::Person::Researcher.model_name.human(count: 2) do %>
+      <%= render 'admin/university/people/researchers/list', researchers: @laboratory.researchers %>
+    <% end %>
+  </div>
+</div>
 
 <% content_for :action_bar_left do %>
   <%= destroy_link @laboratory %>
diff --git a/app/views/admin/university/people/_form.html.erb b/app/views/admin/university/people/_form.html.erb
index 8a25e4c8e..1beeef56c 100644
--- a/app/views/admin/university/people/_form.html.erb
+++ b/app/views/admin/university/people/_form.html.erb
@@ -88,6 +88,10 @@
           </div>
         </div>
       <% end if current_university.is_really_a_university %>
+
+      <%= f.association :research_laboratories, 
+                        collection: current_university.research_laboratories,
+                        as: :check_boxes if person.is_researcher %>
     </div>
     <div class="col-lg-4">
       <%= osuny_panel t('metadata') do %>
diff --git a/app/views/admin/university/people/experiences/_list.html.erb b/app/views/admin/university/people/experiences/_list.html.erb
index bffbc5867..dd7ad699d 100644
--- a/app/views/admin/university/people/experiences/_list.html.erb
+++ b/app/views/admin/university/people/experiences/_list.html.erb
@@ -1,5 +1,5 @@
 <div class="table-responsive">
-  <table class="<%= table_classes %>">
+  <table class="<%= table_classes(with_actions: false) %>">
     <thead>
       <tr>
         <th><%= University::Person::Experience.human_attribute_name('description') %></th>
diff --git a/app/views/admin/university/people/researchers/_list.html.erb b/app/views/admin/university/people/researchers/_list.html.erb
new file mode 100644
index 000000000..3b7c26481
--- /dev/null
+++ b/app/views/admin/university/people/researchers/_list.html.erb
@@ -0,0 +1,18 @@
+<div class="table-responsive">
+  <table class="<%= table_classes(with_actions: false) %>">
+    <thead>
+      <tr>
+        <th><%= University::Person.human_attribute_name('first_name') %></th>
+        <th><%= University::Person.human_attribute_name('last_name') %></th>
+      </tr>
+    </thead>
+    <tbody>
+      <% researchers.each do |researcher| %>
+        <tr>
+          <td><%= link_to researcher.first_name, admin_university_person_path(researcher) %></td>
+          <td><%= link_to researcher.last_name, admin_university_person_path(researcher) %></td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+</div>
diff --git a/app/views/admin/university/people/show.html.erb b/app/views/admin/university/people/show.html.erb
index 6ed908096..969d79c04 100644
--- a/app/views/admin/university/people/show.html.erb
+++ b/app/views/admin/university/people/show.html.erb
@@ -31,6 +31,10 @@ action += link_to t('university.manage_experiences'),
   <%= render 'admin/university/people/experiences/list', experiences: @person.experiences.ordered %>
 <% end %>
 
+<%= osuny_panel Research::Laboratory.model_name.human(count: 2) do %>
+  <%= render 'admin/research/laboratories/list', laboratories: @person.laboratories.ordered %>
+<% end if @person.laboratories.any? %>
+
 <%= render 'admin/application/connections/list', about: @person %>
 
 <% content_for :action_bar_left do %>
diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml
index c6015c037..2c134393a 100644
--- a/config/locales/university/en.yml
+++ b/config/locales/university/en.yml
@@ -90,6 +90,7 @@ en:
         phone_professional: Professional phone
         picture: Profile picture
         research_journal_papers: Papiers
+        research_laboratories: Research laboratories
         researcher: Researcher
         roles: Roles
         slug: Slug
diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml
index b9e9bfb88..c88446772 100644
--- a/config/locales/university/fr.yml
+++ b/config/locales/university/fr.yml
@@ -90,6 +90,7 @@ fr:
         phone_professional: Téléphone professionnel
         picture: Photo de profil
         research_journal_papers: Papiers
+        research_laboratories: Laboratoires de recherche
         researcher: Chercheur·e
         roles: Rôles
         slug: Slug
diff --git a/db/migrate/20230829141647_create_join_table_research_laboratories_university_researchers.rb b/db/migrate/20230829141647_create_join_table_research_laboratories_university_researchers.rb
new file mode 100644
index 000000000..85c6e3921
--- /dev/null
+++ b/db/migrate/20230829141647_create_join_table_research_laboratories_university_researchers.rb
@@ -0,0 +1,8 @@
+class CreateJoinTableResearchLaboratoriesUniversityResearchers < ActiveRecord::Migration[7.0]
+  def change
+    create_join_table :research_laboratories, :university_people, column_options: {type: :uuid} do |t|
+      t.index [:research_laboratory_id, :university_person_id], name: 'laboratory_person'
+      t.index [:university_person_id, :research_laboratory_id], name: 'person_laboratory'
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5d7e86936..6d4fbf26c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema[7.0].define(version: 2023_08_22_071011) do
+ActiveRecord::Schema[7.0].define(version: 2023_08_29_141647) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -863,6 +863,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_08_22_071011) do
     t.index ["university_id"], name: "index_research_laboratories_on_university_id"
   end
 
+  create_table "research_laboratories_university_people", id: false, force: :cascade do |t|
+    t.uuid "research_laboratory_id", null: false
+    t.uuid "university_person_id", null: false
+    t.index ["research_laboratory_id", "university_person_id"], name: "laboratory_person"
+    t.index ["university_person_id", "research_laboratory_id"], name: "person_laboratory"
+  end
+
   create_table "research_laboratory_axes", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "research_laboratory_id", null: false
-- 
GitLab