From 82fdc02d04731381674da980d71a87fd3b9bf5b7 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Fri, 8 Oct 2021 17:09:12 +0200
Subject: [PATCH] user picture

---
 app/controllers/admin/users_controller.rb | 2 +-
 app/models/user.rb                        | 5 ++++-
 app/views/admin/users/_form.html.erb      | 3 ++-
 app/views/admin/users/show.html.erb       | 7 +++++++
 4 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index e764e9b48..7d2bf84fc 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -57,6 +57,6 @@ class Admin::UsersController < Admin::ApplicationController
   end
 
   def user_params
-    params.require(:user).permit(:first_name, :last_name, :role, :language_id)
+    params.require(:user).permit(:first_name, :last_name, :role, :language_id, :picture, :picture_delete)
   end
 end
diff --git a/app/models/user.rb b/app/models/user.rb
index 5eb31aafd..416495bf5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -45,12 +45,15 @@
 class User < ApplicationRecord
   include WithDevise
 
+  # has_one_attached_resizable :picture
+  has_one_attached :picture
+
   belongs_to :university
   belongs_to :language
   has_one :researcher, class_name: 'Research::Researcher'
 
   def to_s
-    first_name || last_name ? "#{first_name} #{last_name}"
+    (first_name.present? || last_name.present?) ? "#{first_name} #{last_name}"
                             : "#{email}"
   end
 end
diff --git a/app/views/admin/users/_form.html.erb b/app/views/admin/users/_form.html.erb
index 078836949..5bf94c0c8 100644
--- a/app/views/admin/users/_form.html.erb
+++ b/app/views/admin/users/_form.html.erb
@@ -7,9 +7,10 @@
     </div>
     <div class="col-md-4">
       <%= f.input :first_name %>
+      <%= f.input :last_name %>
     </div>
     <div class="col-md-4">
-      <%= f.input :last_name %>
+      <%= f.input :picture %>
     </div>
   </div>
   <% content_for :buttons do %>
diff --git a/app/views/admin/users/show.html.erb b/app/views/admin/users/show.html.erb
index 09a05a457..5f238acc4 100644
--- a/app/views/admin/users/show.html.erb
+++ b/app/views/admin/users/show.html.erb
@@ -27,6 +27,13 @@
   </p>
 <% end %>
 
+<% if @user.picture.attached? && @user.picture.variable? %>
+  <p>
+    <strong>Picture:</strong>
+    <%= image_tag @user.picture.variant(resize: '200x') %>
+  </p>
+<% end %>
+
 
 <% content_for :buttons do %>
   <%= edit_link @user %>
-- 
GitLab