From 9040bb66b5661a1c645c7bf8d5da63e08786d9bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Thu, 27 Jan 2022 02:13:18 +0100
Subject: [PATCH] reorder

---
 .../admin/education/program/role/people_controller.rb  |  8 +++++++-
 .../admin/education/program/roles_controller.rb        |  8 ++++----
 .../admin/education/program/teachers_controller.rb     |  8 +++++++-
 .../admin/education/school/role/people_controller.rb   |  8 +++++++-
 .../admin/education/school/roles_controller.rb         |  7 +++++++
 app/models/university/person/involvement.rb            | 10 +++++-----
 app/models/university/role.rb                          |  2 +-
 7 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/app/controllers/admin/education/program/role/people_controller.rb b/app/controllers/admin/education/program/role/people_controller.rb
index 8a606f4dc..945c77259 100644
--- a/app/controllers/admin/education/program/role/people_controller.rb
+++ b/app/controllers/admin/education/program/role/people_controller.rb
@@ -2,7 +2,9 @@ class Admin::Education::Program::Role::PeopleController < Admin::Education::Prog
   load_and_authorize_resource :role, class: University::Role, through: :program, param: :role_id, through_association: :university_roles
   load_and_authorize_resource :involvement, class: University::Person::Involvement, through: :role, parent: false
 
-  before_action :get_available_people, except: :destroy
+  include Admin::Reorderable
+
+  before_action :get_available_people, except: [:reorder, :destroy]
 
   def new
     breadcrumb
@@ -59,4 +61,8 @@ class Admin::Education::Program::Role::PeopleController < Admin::Education::Prog
           .permit(:description, :position, :person_id)
           .merge(university_id: @program.university_id, kind: :administrator)
   end
+
+  def model
+    University::Person::Involvement
+  end
 end
diff --git a/app/controllers/admin/education/program/roles_controller.rb b/app/controllers/admin/education/program/roles_controller.rb
index 48b07bba4..5827579b8 100644
--- a/app/controllers/admin/education/program/roles_controller.rb
+++ b/app/controllers/admin/education/program/roles_controller.rb
@@ -3,10 +3,6 @@ class Admin::Education::Program::RolesController < Admin::Education::Program::Ap
 
   include Admin::Reorderable
 
-  def reorder
-    super { |first_role| first_role.sync_program }
-  end
-
   def show
     @involvements = @role.involvements.ordered
     breadcrumb
@@ -61,4 +57,8 @@ class Admin::Education::Program::RolesController < Admin::Education::Program::Ap
           .permit(:description, :position)
           .merge(target: @program, university_id: current_university.id)
   end
+
+  def model
+    University::Role
+  end
 end
diff --git a/app/controllers/admin/education/program/teachers_controller.rb b/app/controllers/admin/education/program/teachers_controller.rb
index c26bde871..43d532c63 100644
--- a/app/controllers/admin/education/program/teachers_controller.rb
+++ b/app/controllers/admin/education/program/teachers_controller.rb
@@ -5,7 +5,9 @@ class Admin::Education::Program::TeachersController < Admin::Education::Program:
                               through_association: :university_person_involvements,
                               parent: false
 
-  before_action :get_available_people, except: :destroy
+  include Admin::Reorderable
+
+  before_action :get_available_people, except: [:reorder, :destroy]
 
   def new
     breadcrumb
@@ -61,4 +63,8 @@ class Admin::Education::Program::TeachersController < Admin::Education::Program:
           .permit(:description, :position, :person_id)
           .merge(university_id: @program.university_id, kind: :teacher)
   end
+
+  def model
+    University::Person::Involvement
+  end
 end
diff --git a/app/controllers/admin/education/school/role/people_controller.rb b/app/controllers/admin/education/school/role/people_controller.rb
index c1747a9dc..71358f1dd 100644
--- a/app/controllers/admin/education/school/role/people_controller.rb
+++ b/app/controllers/admin/education/school/role/people_controller.rb
@@ -2,7 +2,9 @@ class Admin::Education::School::Role::PeopleController < Admin::Education::Schoo
   load_and_authorize_resource :role, class: University::Role, through: :school, param: :role_id, through_association: :university_roles
   load_and_authorize_resource :involvement, class: University::Person::Involvement, through: :role, parent: false
 
-  before_action :get_available_people, except: :destroy
+  include Admin::Reorderable
+
+  before_action :get_available_people, except: [:reorder, :destroy]
 
   def new
     breadcrumb
@@ -59,4 +61,8 @@ class Admin::Education::School::Role::PeopleController < Admin::Education::Schoo
           .permit(:description, :position, :person_id)
           .merge(university_id: @school.university_id, kind: :administrator)
   end
+
+  def model
+    University::Person::Involvement
+  end
 end
diff --git a/app/controllers/admin/education/school/roles_controller.rb b/app/controllers/admin/education/school/roles_controller.rb
index 6e0633b1b..09d108e7f 100644
--- a/app/controllers/admin/education/school/roles_controller.rb
+++ b/app/controllers/admin/education/school/roles_controller.rb
@@ -1,7 +1,10 @@
 class Admin::Education::School::RolesController < Admin::Education::School::ApplicationController
   load_and_authorize_resource class: University::Role, through: :school, through_association: :university_roles
 
+  include Admin::Reorderable
+
   def index
+    @roles = @roles.ordered
     breadcrumb
   end
 
@@ -59,4 +62,8 @@ class Admin::Education::School::RolesController < Admin::Education::School::Appl
           .permit(:description, :position)
           .merge(target: @school, university_id: @school.university_id)
   end
+
+  def model
+    University::Role
+  end
 end
diff --git a/app/models/university/person/involvement.rb b/app/models/university/person/involvement.rb
index d555cccbc..f2c8f6c28 100644
--- a/app/models/university/person/involvement.rb
+++ b/app/models/university/person/involvement.rb
@@ -33,19 +33,19 @@ class University::Person::Involvement < ApplicationRecord
   belongs_to :person
   belongs_to :target, polymorphic: true
 
-  after_commit :sync_target
+  after_commit :sync_with_git
 
   def to_s
     "#{person}"
   end
 
+  def sync_with_git
+    target.sync_with_git if target.respond_to? :sync_with_git
+  end
+
   protected
 
   def last_ordered_element
     self.class.unscoped.where(university_id: university_id, target: target).ordered.last
   end
-
-  def sync_target
-    target.sync_with_git
-  end
 end
diff --git a/app/models/university/role.rb b/app/models/university/role.rb
index 2e4987077..a128dbb4f 100644
--- a/app/models/university/role.rb
+++ b/app/models/university/role.rb
@@ -32,7 +32,7 @@ class University::Role < ApplicationRecord
   end
 
   def sync_with_git
-    target.sync_with_git
+    target.sync_with_git if target&.respond_to? :sync_with_git
   end
 
   protected
-- 
GitLab