From 4368ddc92569a67ea6c0e57e15ea8b8cc113aa58 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 29 Aug 2022 12:44:53 +0200
Subject: [PATCH] edit experience

---
 .../extranet/experiences_controller.rb        | 44 +++++++++++++++++++
 app/models/university/person/experience.rb    |  5 +++
 app/models/user/with_person.rb                |  2 +
 app/views/extranet/account/show.html.erb      |  6 ++-
 .../_experience.html.erb                      |  4 +-
 app/views/extranet/experiences/_form.html.erb |  7 +++
 app/views/extranet/experiences/edit.html.erb  |  3 ++
 app/views/extranet/experiences/new.html.erb   |  3 ++
 app/views/extranet/persons/show.html.erb      |  2 +-
 config/locales/en.yml                         |  6 ---
 config/locales/extranet/en.yml                |  7 +++
 config/locales/extranet/fr.yml                |  9 ++++
 config/locales/fr.yml                         |  6 ---
 config/locales/university/en.yml              |  1 +
 config/locales/university/fr.yml              |  4 +-
 config/routes/extranet.rb                     | 23 +++++-----
 16 files changed, 105 insertions(+), 27 deletions(-)
 create mode 100644 app/controllers/extranet/experiences_controller.rb
 rename app/views/extranet/{persons => experiences}/_experience.html.erb (82%)
 create mode 100644 app/views/extranet/experiences/_form.html.erb
 create mode 100644 app/views/extranet/experiences/edit.html.erb
 create mode 100644 app/views/extranet/experiences/new.html.erb
 create mode 100644 config/locales/extranet/en.yml
 create mode 100644 config/locales/extranet/fr.yml

diff --git a/app/controllers/extranet/experiences_controller.rb b/app/controllers/extranet/experiences_controller.rb
new file mode 100644
index 000000000..72a2538af
--- /dev/null
+++ b/app/controllers/extranet/experiences_controller.rb
@@ -0,0 +1,44 @@
+class Extranet::ExperiencesController < Extranet::ApplicationController
+  load_and_authorize_resource class: University::Person::Experience,
+                              through: :current_user,
+                              through_association: :experiences
+  def new
+    breadcrumb
+  end
+  
+  def edit
+    breadcrumb
+  end
+
+  def create
+    @experience.university = current_university
+    if @experience.save
+      redirect_to account_path, notice: 'Ok'
+    else
+      breadcrumb
+      render :new
+    end
+  end
+
+  def update
+    if @experience.update experience_params
+      redirect_to account_path, notice: 'Ok'
+    else
+      breadcrumb
+      render :edit
+    end
+  end
+
+  protected
+
+  def experience_params
+    params.require(:university_person_experience)
+          .permit(:description, :from_year, :to_year, :organization_id)
+  end
+
+  def breadcrumb
+    super
+    add_breadcrumb t('extranet.account.my'), account_path
+    add_breadcrumb @experience
+  end
+end
\ No newline at end of file
diff --git a/app/models/university/person/experience.rb b/app/models/university/person/experience.rb
index 4785cae2f..10e597629 100644
--- a/app/models/university/person/experience.rb
+++ b/app/models/university/person/experience.rb
@@ -31,4 +31,9 @@ class University::Person::Experience < ApplicationRecord
   belongs_to :organization, class_name: "University::Organization"
 
   scope :ordered, -> { order(from_year: :desc)}
+
+  def to_s
+    persisted?  ? "#{description}"
+                : self.class.human_attribute_name('new')
+  end
 end
diff --git a/app/models/user/with_person.rb b/app/models/user/with_person.rb
index 15057f8b5..4e095ff63 100644
--- a/app/models/user/with_person.rb
+++ b/app/models/user/with_person.rb
@@ -4,6 +4,8 @@ module User::WithPerson
   included do
     has_one :person, class_name: 'University::Person', dependent: :nullify
 
+    delegate :experiences, to: :person
+
     after_create_commit :find_or_create_person, unless: :server_admin?
   end
 
diff --git a/app/views/extranet/account/show.html.erb b/app/views/extranet/account/show.html.erb
index b0e49e832..f0cee2369 100644
--- a/app/views/extranet/account/show.html.erb
+++ b/app/views/extranet/account/show.html.erb
@@ -12,11 +12,13 @@
     </div>
     <div class="experiences">
       <p class="mb-4">Parcours professionel</p>
-      <%= link_to 'Ajouter une expérience', '', class: 'btn btn-sm btn-primary mb-4' %>
+      <%= link_to University::Person::Experience.human_attribute_name('new'),
+                  new_experience_path,
+                  class: 'btn btn-sm btn-primary mb-4' %>
       <% if @person.experiences.any? %>
         <ul>
           <% @person.experiences.ordered.each do |experience| %>
-            <%= render 'extranet/persons/experience', experience: experience, edit: true %>
+            <%= render 'extranet/experiences/experience', experience: experience, edit: true %>
           <% end %>
         </ul>
         <% end %>
diff --git a/app/views/extranet/persons/_experience.html.erb b/app/views/extranet/experiences/_experience.html.erb
similarity index 82%
rename from app/views/extranet/persons/_experience.html.erb
rename to app/views/extranet/experiences/_experience.html.erb
index f5c49059a..73a8f1e67 100644
--- a/app/views/extranet/persons/_experience.html.erb
+++ b/app/views/extranet/experiences/_experience.html.erb
@@ -8,7 +8,9 @@ edit ||= false
       <%= "#{experience.from_year} —" if experience.from_year %>
       <%= experience.to_year || t('today') %>
     </p>
-    <%= link_to 'Modifier cette expérience', '', class: 'btn btn-sm btn-primary mt-2' if edit %>
+    <%= link_to University::Person::Experience.human_attribute_name('edit'),
+                edit_experience_path(experience),
+                class: 'btn btn-sm btn-primary mt-2' if edit %>
   </div>
   <div>
     <% if experience.organization.present? %>
diff --git a/app/views/extranet/experiences/_form.html.erb b/app/views/extranet/experiences/_form.html.erb
new file mode 100644
index 000000000..32e17ddcb
--- /dev/null
+++ b/app/views/extranet/experiences/_form.html.erb
@@ -0,0 +1,7 @@
+<%= simple_form_for experience, url: experience.persisted? ? experience_path(experience) : experiences_path do |f| %>
+  <%= f.association :organization %>
+  <%= f.input :description %>
+  <%= f.input :from_year %>
+  <%= f.input :to_year %>
+  <%= submit f %>
+<% end %>
\ No newline at end of file
diff --git a/app/views/extranet/experiences/edit.html.erb b/app/views/extranet/experiences/edit.html.erb
new file mode 100644
index 000000000..acfba4993
--- /dev/null
+++ b/app/views/extranet/experiences/edit.html.erb
@@ -0,0 +1,3 @@
+<% content_for :title, @experience %>
+
+<%= render 'form', experience: @experience %>
\ No newline at end of file
diff --git a/app/views/extranet/experiences/new.html.erb b/app/views/extranet/experiences/new.html.erb
new file mode 100644
index 000000000..acfba4993
--- /dev/null
+++ b/app/views/extranet/experiences/new.html.erb
@@ -0,0 +1,3 @@
+<% content_for :title, @experience %>
+
+<%= render 'form', experience: @experience %>
\ No newline at end of file
diff --git a/app/views/extranet/persons/show.html.erb b/app/views/extranet/persons/show.html.erb
index 426038b11..ad3730db9 100644
--- a/app/views/extranet/persons/show.html.erb
+++ b/app/views/extranet/persons/show.html.erb
@@ -12,7 +12,7 @@
         <p class="mb-4">Parcours professionel</p>
         <ul>
           <% @person.experiences.ordered.each do |experience| %>
-            <%= render 'extranet/persons/experience', experience: experience %>
+            <%= render 'extranet/experiences/experience', experience: experience %>
           <% end %>
         </ul>
       </div>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 1fc6eabd7..81d8721d0 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -159,12 +159,6 @@ en:
         finished: Finished
         finished_with_errors: Finished with errors
         pending: Pending
-  extranet:
-    account: 
-      my: My account
-      edit: Edit
-      updated: Updated
-      logout: Log out
   false: No
   featured_image:
     title: Image
diff --git a/config/locales/extranet/en.yml b/config/locales/extranet/en.yml
new file mode 100644
index 000000000..a71ded4f9
--- /dev/null
+++ b/config/locales/extranet/en.yml
@@ -0,0 +1,7 @@
+en:
+  extranet:
+    account: 
+      my: My account
+      edit: Edit
+      updated: Updated
+      logout: Log out
diff --git a/config/locales/extranet/fr.yml b/config/locales/extranet/fr.yml
new file mode 100644
index 000000000..4c4347226
--- /dev/null
+++ b/config/locales/extranet/fr.yml
@@ -0,0 +1,9 @@
+fr:
+  extranet:
+    experiences:
+      new: Ajouter une expérience
+    account: 
+      my: Mon compte
+      edit: Modifier
+      updated: Mise à jour effectuée
+      logout: Déconnexion
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index f4512be94..50fb3b687 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -162,12 +162,6 @@ fr:
         finished: Traité
         finished_with_errors: Traité avec des erreurs
         pending: En cours de traitement
-  extranet:
-    account: 
-      my: Mon compte
-      edit: Modifier
-      updated: Mise à jour effectuée
-      logout: Déconnexion
   false: Non
   featured_image:
     title: Image
diff --git a/config/locales/university/en.yml b/config/locales/university/en.yml
index 9c88adb86..9baf021dc 100644
--- a/config/locales/university/en.yml
+++ b/config/locales/university/en.yml
@@ -73,6 +73,7 @@ en:
         organization: Organization
         from_year: Start year
         to_year: End year
+        new: Add experience
       university/person/involvement:
         description: Mission (in this context)
         person: Person
diff --git a/config/locales/university/fr.yml b/config/locales/university/fr.yml
index b80dd0997..6ce31cfef 100644
--- a/config/locales/university/fr.yml
+++ b/config/locales/university/fr.yml
@@ -70,8 +70,10 @@ fr:
         zipcode: Code postal
       university/person/experience:
         description: Description
-        organization: Organisation
+        edit: Modifier l'expérience
         from_year: Année de début
+        new: Ajouter une expérience
+        organization: Organisation
         to_year: Année de fin
       university/person/involvement:
         description: Mission (dans ce contexte)
diff --git a/config/routes/extranet.rb b/config/routes/extranet.rb
index 6870b88c5..905cf1b2e 100644
--- a/config/routes/extranet.rb
+++ b/config/routes/extranet.rb
@@ -1,12 +1,15 @@
-get 'cohorts'           => 'extranet/cohorts#index', as: :education_cohorts
-get 'cohorts/:id'       => 'extranet/cohorts#show', as: :education_cohort
-get 'organizations'     => 'extranet/organizations#index', as: :university_organizations
+get 'cohorts' => 'extranet/cohorts#index', as: :education_cohorts
+get 'cohorts/:id' => 'extranet/cohorts#show', as: :education_cohort
+get 'organizations' => 'extranet/organizations#index', as: :university_organizations
 get 'organizations/:id' => 'extranet/organizations#show', as: :university_organization
-get 'persons'           => 'extranet/persons#index', as: :university_persons
-get 'persons/:id'       => 'extranet/persons#show', as: :university_person
-get 'years'             => 'extranet/academic_years#index', as: :education_academic_years
-get 'years/:id'         => 'extranet/academic_years#show', as: :education_academic_year
-get 'account'           => 'extranet/account#show', as: :account 
-get 'account/edit'      => 'extranet/account#edit', as: :edit_account 
-patch 'account'         => 'extranet/account#update'
+get 'persons' => 'extranet/persons#index', as: :university_persons
+get 'persons/:id' => 'extranet/persons#show', as: :university_person
+get 'years' => 'extranet/academic_years#index', as: :education_academic_years
+get 'years/:id' => 'extranet/academic_years#show', as: :education_academic_year
+get 'account' => 'extranet/account#show', as: :account 
+get 'account/edit' => 'extranet/account#edit', as: :edit_account 
+patch 'account' => 'extranet/account#update'
+scope :account do 
+  resources :experiences, controller: 'extranet/experiences', except: [:index, :show]
+end
 root to: 'extranet/home#index'
-- 
GitLab