From da55e4d759a1031ab3cf51cd6dc0f277a05c53a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Wed, 12 Jan 2022 12:43:23 +0100
Subject: [PATCH] University::Person#picture

---
 .../admin/university/people_controller.rb     |  5 ++--
 app/models/university/person.rb               |  6 +++-
 app/models/university/person/with_picture.rb  | 19 ++++++++++++
 .../admin/university/people/_form.html.erb    | 30 ++++++++++++++++---
 .../university/people/_main_infos.html.erb    | 17 +++++++++++
 .../admin/university/people/static.html.erb   |  3 ++
 config/locales/university/en.yml              |  1 +
 config/locales/university/fr.yml              |  1 +
 8 files changed, 75 insertions(+), 7 deletions(-)
 create mode 100644 app/models/university/person/with_picture.rb

diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb
index bbce1fa39..71a44e4ee 100644
--- a/app/controllers/admin/university/people_controller.rb
+++ b/app/controllers/admin/university/people_controller.rb
@@ -56,8 +56,9 @@ class Admin::University::PeopleController < Admin::University::ApplicationContro
 
   def person_params
     params.require(:university_person).permit(
-      :first_name, :last_name, :email, :phone, :biography, :slug, :user_id,
-      :is_researcher, :is_teacher, :is_administration
+      :slug, :first_name, :last_name, :email, :phone,
+      :biography,  :picture, :picture_delete, :picture_infos,
+      :is_researcher, :is_teacher, :is_administration, :user_id
     ).merge(university_id: current_university.id)
   end
 end
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index abd7bb54f..76d5fcf36 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -29,6 +29,7 @@
 class University::Person < ApplicationRecord
   include WithGit
   include WithSlug
+  include WithPicture
 
   has_rich_text :biography
 
@@ -107,7 +108,10 @@ class University::Person < ApplicationRecord
 
   def git_dependencies(website)
     dependencies = []
-    dependencies << self if for_website?(website)
+    if for_website?(website)
+      dependencies << self
+      dependencies << best_picture.blob
+    end
     dependencies << administrator if administrator.for_website?(website)
     dependencies << author if author.for_website?(website)
     dependencies << researcher if researcher.for_website?(website)
diff --git a/app/models/university/person/with_picture.rb b/app/models/university/person/with_picture.rb
new file mode 100644
index 000000000..31be4dd3d
--- /dev/null
+++ b/app/models/university/person/with_picture.rb
@@ -0,0 +1,19 @@
+module University::Person::WithPicture
+  extend ActiveSupport::Concern
+
+  included do
+    has_one_attached_deletable :picture
+  end
+
+  def best_picture
+    @best_picture ||= begin
+      best_picture = picture
+      best_picture = user.picture if !picture.attached? && user.present?
+      best_picture
+    end
+  end
+
+  def best_picture_inherits_from_user?
+    user.present? && best_picture == user.picture
+  end
+end
diff --git a/app/views/admin/university/people/_form.html.erb b/app/views/admin/university/people/_form.html.erb
index 2881ea5b6..63a546057 100644
--- a/app/views/admin/university/people/_form.html.erb
+++ b/app/views/admin/university/people/_form.html.erb
@@ -25,6 +25,24 @@
           <%= f.input :biography, as: :rich_text_area %>
         </div>
       </div>
+      <div class="card flex-fill w-100">
+        <div class="card-header">
+          <h5 class="card-title mb-0"><%= University::Person.human_attribute_name('abilities') %></h5>
+        </div>
+        <div class="card-body">
+          <div class="row">
+            <div class="col-md-6">
+              <%= f.input :is_teacher %>
+            </div>
+            <div class="col-md-6">
+              <%= f.input :is_researcher %>
+            </div>
+            <div class="col-md-6">
+              <%= f.input :is_administration %>
+            </div>
+          </div>
+        </div>
+      </div>
     </div>
     <div class="col-md-4">
       <div class="card flex-fill w-100">
@@ -43,12 +61,16 @@
       </div>
       <div class="card flex-fill w-100">
         <div class="card-header">
-          <h5 class="card-title mb-0"><%= University::Person.human_attribute_name('abilities') %></h5>
+          <h5 class="card-title mb-0"><%= University::Person.human_attribute_name('picture') %></h5>
         </div>
         <div class="card-body">
-          <%= f.input :is_teacher %>
-          <%= f.input :is_researcher %>
-          <%= f.input :is_administration %>
+          <%= f.input :picture,
+                  as: :single_deletable_file,
+                  label: false,
+                  input_html: { accept: '.jpg,.jpeg,.png' },
+                  preview: 200,
+                  resize: 1,
+                  direct_upload: true %>
         </div>
       </div>
     </div>
diff --git a/app/views/admin/university/people/_main_infos.html.erb b/app/views/admin/university/people/_main_infos.html.erb
index 9a5f7ebe4..0ac59b529 100644
--- a/app/views/admin/university/people/_main_infos.html.erb
+++ b/app/views/admin/university/people/_main_infos.html.erb
@@ -32,5 +32,22 @@
         <% end %>
       </div>
     </div>
+    <% if person.best_picture.attached? %>
+      <div class="card flex-fill w-100">
+        <div class="card-header">
+          <h2 class="card-title mb-0 h5"><%= t('activerecord.attributes.university/person.picture') %></h2>
+        </div>
+        <div class="card-body">
+          <% if person.best_picture_inherits_from_user? %>
+            <p>
+              <span class="small text-muted">
+                <%= t 'admin.inheritance.sentence_html', link: link_to(person.user, [:admin, person.user]) %>
+              </span>
+            </p>
+          <% end %>
+          <%= kamifusen_tag person.best_picture, class: 'img-fluid' %>
+        </div>
+      </div>
+    <% end %>
   </div>
 </div>
diff --git a/app/views/admin/university/people/static.html.erb b/app/views/admin/university/people/static.html.erb
index 53dc13c4d..a90dfeb62 100644
--- a/app/views/admin/university/people/static.html.erb
+++ b/app/views/admin/university/people/static.html.erb
@@ -6,6 +6,9 @@ first_name: "<%= @person.first_name %>"
 last_name: "<%= @person.last_name %>"
 phone: "<%= @person.phone %>"
 email: "<%= @person.email %>"
+<% if @person.best_picture.attached? %>
+image: "<%= @person.best_picture.blob.id %>"
+<% end %>
 roles:
 <% if @person.author.for_website?(@website) %>
   - author
diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml
index ef74dc7e0..edcf48f2b 100644
--- a/config/locales/university/en.yml
+++ b/config/locales/university/en.yml
@@ -28,6 +28,7 @@ en:
         last_name: Last name
         name: Name
         phone: Phone
+        picture: Profile picture
         research_journal_articles: Articles
         slug: Slug
         user: User
diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml
index 56e561bb6..8434dd1ed 100644
--- a/config/locales/university/fr.yml
+++ b/config/locales/university/fr.yml
@@ -28,6 +28,7 @@ fr:
         last_name: Nom de famille
         name: Nom
         phone: Téléphone
+        picture: Photo de profil
         research_journal_articles: Articles
         slug: Slug
         user: Utilisateur
-- 
GitLab