From 3e748e6ffdde235e6536d280ea63cf3704a418e0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 3 Feb 2022 11:19:37 +0100
Subject: [PATCH] sort_by in fields_for because sql order breaks temporary
 objects

---
 .../admin/education/program/role/people_controller.rb      | 2 +-
 app/views/admin/education/program/roles/_form.html.erb     | 2 +-
 app/views/admin/education/programs/_form.html.erb          | 7 ++++++-
 app/views/admin/education/school/roles/_form.html.erb      | 2 +-
 4 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/app/controllers/admin/education/program/role/people_controller.rb b/app/controllers/admin/education/program/role/people_controller.rb
index 945c77259..ff0fe767c 100644
--- a/app/controllers/admin/education/program/role/people_controller.rb
+++ b/app/controllers/admin/education/program/role/people_controller.rb
@@ -48,7 +48,7 @@ class Admin::Education::Program::Role::PeopleController < Admin::Education::Prog
 
   def breadcrumb
     super
-    add_breadcrumb Education::Program::Role.model_name.human(count: 2)
+    add_breadcrumb University::Role.model_name.human(count: 2)
     add_breadcrumb(@role, admin_education_program_role_path(@role, { program_id: @program.id }))
     if @involvement
       @involvement.persisted?  ? add_breadcrumb(@involvement, admin_education_program_role_person_path(@involvement, { program_id: @program.id, role_id: @role.id }))
diff --git a/app/views/admin/education/program/roles/_form.html.erb b/app/views/admin/education/program/roles/_form.html.erb
index 25272f9c0..54d99730b 100644
--- a/app/views/admin/education/program/roles/_form.html.erb
+++ b/app/views/admin/education/program/roles/_form.html.erb
@@ -24,7 +24,7 @@
               } %>
 
           <div class="mb-3" id="involvements" data-sortable="inputs">
-            <%= f.simple_fields_for :involvements, role.involvements.ordered, include_id: false do |involvement_f| %>
+            <%= f.simple_fields_for :involvements, role.involvements.sort_by(&:position), include_id: false do |involvement_f| %>
               <%= render 'admin/education/program/roles/involvement_fields', f: involvement_f, include_id: true %>
             <% end %>
           </div>
diff --git a/app/views/admin/education/programs/_form.html.erb b/app/views/admin/education/programs/_form.html.erb
index f418b7667..461c20397 100644
--- a/app/views/admin/education/programs/_form.html.erb
+++ b/app/views/admin/education/programs/_form.html.erb
@@ -78,7 +78,12 @@
                                   } %>
 
       <div class="row mb-3" id="involvements">
-        <%= f.simple_fields_for :university_person_involvements, program.university_person_involvements.ordered_by_name, include_id: false do |involvement_f| %>
+        <%
+        sorted_involvements = program.university_person_involvements.sort_by { |involvement|
+          [involvement.person&.last_name, involvement.person&.first_name]
+        }
+        %>
+        <%= f.simple_fields_for :university_person_involvements, sorted_involvements, include_id: false do |involvement_f| %>
           <%= render 'admin/education/programs/involvement_fields', f: involvement_f, include_id: true %>
         <% end %>
       </div>
diff --git a/app/views/admin/education/school/roles/_form.html.erb b/app/views/admin/education/school/roles/_form.html.erb
index dccebb954..c5ef4a1c6 100644
--- a/app/views/admin/education/school/roles/_form.html.erb
+++ b/app/views/admin/education/school/roles/_form.html.erb
@@ -25,7 +25,7 @@
               } %>
 
           <div class="mb-3" id="involvements" data-sortable="inputs">
-            <%= f.simple_fields_for :involvements, role.involvements.ordered, include_id: false do |involvement_f| %>
+            <%= f.simple_fields_for :involvements, role.involvements.sort_by(&:position), include_id: false do |involvement_f| %>
               <%= render 'admin/education/school/roles/involvement_fields', f: involvement_f, include_id: true %>
             <% end %>
           </div>
-- 
GitLab