From 90b61a17ec81a5b125cdd7eb752fafd2dc1e3748 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 4 Jan 2022 15:30:08 +0100
Subject: [PATCH] menu

---
 app/helpers/admin/application_helper.rb       |  5 +++--
 app/models/communication/website.rb           | 16 ---------------
 app/models/communication/website/category.rb  |  5 -----
 app/models/communication/website/menu.rb      |  4 ----
 app/models/communication/website/menu/item.rb | 14 +++++--------
 app/models/education/program.rb               |  4 ----
 app/models/university/with_education.rb       |  9 ---------
 .../website/menu/items/_form.html.erb         |  6 +++---
 .../website/menu/items/kind_switch.js.erb     | 20 +++++++++----------
 .../website/menus/static.html.erb             |  5 +++++
 .../website/pages/_form.html.erb              |  4 ++--
 .../website/posts/_form.html.erb              |  2 +-
 .../admin/education/programs/_form.html.erb   |  2 +-
 .../admin/education/schools/_form.html.erb    |  2 +-
 .../admin/education/teachers/_form.html.erb   |  2 +-
 .../admin/education/teachers/_list.html.erb   |  3 ++-
 16 files changed, 34 insertions(+), 69 deletions(-)
 create mode 100644 app/views/admin/communication/website/menus/static.html.erb

diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb
index c2d92946f..8eecec816 100644
--- a/app/helpers/admin/application_helper.rb
+++ b/app/helpers/admin/application_helper.rb
@@ -72,12 +72,13 @@ module Admin::ApplicationHelper
     sanitize text
   end
 
-  def collection_tree(list, except)
+  def collection_tree(list, except = nil)
     collection = []
     list.root.ordered.each do |object|
       collection.concat(object.self_and_children(0))
     end
-    collection.reject { |o| o[:id] == except.id }
+    collection = collection.reject { |o| o[:id] == except.id } unless except.nil?
+    collection
   end
 
   private
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index b8aba04c7..1b9d8372d 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -41,22 +41,6 @@ class Communication::Website < ApplicationRecord
     "#{name}"
   end
 
-  def list_of_categories
-    all_categories = []
-    categories.root.ordered.each do |category|
-      all_categories.concat(category.self_and_children(0))
-    end
-    all_categories
-  end
-
-  def list_of_programs
-    all_programs = []
-    programs.root.ordered.each do |program|
-      all_programs.concat(program.self_and_children(0))
-    end
-    all_programs
-  end
-
   def git_path_static
     "data/website.yml"
   end
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 56c83147c..1e46fb203 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -70,11 +70,6 @@ class Communication::Website::Category < ApplicationRecord
 
   before_create :set_position
 
-  def list_of_other_categories
-    new_record? ? website.list_of_categories
-                : website.list_of_categories.reject! { |p| p[:id] == id }
-  end
-
   def to_s
     "#{name}"
   end
diff --git a/app/models/communication/website/menu.rb b/app/models/communication/website/menu.rb
index e66a5a16d..7396c580d 100644
--- a/app/models/communication/website/menu.rb
+++ b/app/models/communication/website/menu.rb
@@ -40,8 +40,4 @@ class Communication::Website::Menu < ApplicationRecord
   def git_path_static
     "data/menus/#{identifier}.yml"
   end
-
-  def to_static(github_file)
-    items.root.ordered.map(&:to_static_hash).to_yaml
-  end
 end
diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb
index 39f079a91..5bac73f1f 100644
--- a/app/models/communication/website/menu/item.rb
+++ b/app/models/communication/website/menu/item.rb
@@ -62,7 +62,7 @@ class Communication::Website::Menu::Item < ApplicationRecord
   validates :about, presence: true, if: :has_about?
 
   before_create :set_position
-  after_commit :force_publish_menu, unless: :skip_publication_callback
+  after_commit :sync_menu
 
   scope :ordered, -> { order(position: :asc) }
 
@@ -115,19 +115,15 @@ class Communication::Website::Menu::Item < ApplicationRecord
     kind_page? || kind_program? || kind_news_category? || kind_news_article?
   end
 
-  def force_publish_menu
-    menu.force_publish!
+  def sync_menu
+    menu.sync_with_git
   end
 
   protected
 
   def set_position
     last_element = menu.items.where(parent_id: parent_id).ordered.last
-
-    unless last_element.nil?
-      self.position = last_element.position + 1
-    else
-      self.position = 1
-    end
+    self.position = last_element.nil? ? 1
+                                      : last_element.position + 1
   end
 end
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index 998df27c4..c90e7a3a0 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -118,10 +118,6 @@ class Education::Program < ApplicationRecord
     end
   end
 
-  def list_of_other_programs
-    university.list_of_programs.reject! { |p| p[:id] == id }
-  end
-
   def set_websites_categories
     websites.find_each(&:set_programs_categories!)
   end
diff --git a/app/models/university/with_education.rb b/app/models/university/with_education.rb
index 16be46c1b..5acddc90f 100644
--- a/app/models/university/with_education.rb
+++ b/app/models/university/with_education.rb
@@ -4,14 +4,5 @@ module University::WithEducation
   included do
     has_many :education_programs, class_name: 'Education::Program', dependent: :destroy
     has_many :education_schools, class_name: 'Education::School', dependent: :destroy
-
-    def list_of_programs
-      all_programs = []
-      education_programs.root.ordered.each do |program|
-        all_programs.concat(program.self_and_children(0))
-      end
-      all_programs
-    end
-
   end
 end
diff --git a/app/views/admin/communication/website/menu/items/_form.html.erb b/app/views/admin/communication/website/menu/items/_form.html.erb
index e69a39840..44ccf4854 100644
--- a/app/views/admin/communication/website/menu/items/_form.html.erb
+++ b/app/views/admin/communication/website/menu/items/_form.html.erb
@@ -21,11 +21,11 @@
             <%
               if item.has_about?
                 if item.kind_page?
-                  about_collection = @website.list_of_pages
+                  about_collection = collection_tree(@website.pages)
                 elsif item.kind_program?
-                  about_collection = @website.list_of_programs
+                  about_collection = collection_tree(@website.programs)
                 elsif item.kind_news_category?
-                  about_collection = @website.list_of_categories
+                  about_collection = collection_tree(@website.categories)
                 elsif item.kind_news_article?
                   about_collection = @website.posts.ordered.map { |e| { label: e.to_s, id: e.id } }
                 end
diff --git a/app/views/admin/communication/website/menu/items/kind_switch.js.erb b/app/views/admin/communication/website/menu/items/kind_switch.js.erb
index cb32d1252..e883ff32b 100644
--- a/app/views/admin/communication/website/menu/items/kind_switch.js.erb
+++ b/app/views/admin/communication/website/menu/items/kind_switch.js.erb
@@ -14,16 +14,16 @@ function hideAbout() {
     hideAbout();
 <% elsif @kind == 'page' %>
     <%
-        options = ['<option value="" label=" "></option>']
-        @website.list_of_pages.each do |page|
-            options << "<option value=\"#{page[:id]}\">#{page[:label]}</option>"
-        end
+    options = ['<option value="" label=" "></option>']
+    collection_tree(@website.pages).each do |page|
+        options << "<option value=\"#{page[:id]}\">#{page[:label]}</option>"
+    end
     %>
     displayAbout('Communication::Website::Page', "<%= j(raw(options.join(''))) %>");
 <% elsif @kind == 'program' %>
     <%
     options = ['<option value="" label=" "></option>']
-    @website.list_of_programs.each do |page|
+    collection_tree(@website.programs).each do |page|
         options << "<option value=\"#{page[:id]}\">#{page[:label]}</option>"
     end
     %>
@@ -31,17 +31,17 @@ function hideAbout() {
 <% elsif @kind == 'news_category' %>
     <%
         options = ['<option value="" label=" "></option>']
-        @website.list_of_categories.each do |category|
+        collection_tree(@website.categories).each do |category|
             options << "<option value=\"#{category[:id]}\">#{category[:label]}</option>"
         end
     %>
     displayAbout('Communication::Website::Category', "<%= j(raw(options.join(''))) %>");
 <% elsif @kind == 'news_article' %>
     <%
-        options = ['<option value="" label=" "></option>']
-        @website.posts.ordered.each do |post|
-            options << "<option value=\"#{post.id}\">#{post.to_s}</option>"
-        end
+    options = ['<option value="" label=" "></option>']
+    @website.posts.ordered.each do |post|
+        options << "<option value=\"#{post.id}\">#{post.to_s}</option>"
+    end
     %>
     displayAbout('Communication::Website::Post', "<%= j(raw(options.join(''))) %>");
 <% else %>
diff --git a/app/views/admin/communication/website/menus/static.html.erb b/app/views/admin/communication/website/menus/static.html.erb
new file mode 100644
index 000000000..035a94b4a
--- /dev/null
+++ b/app/views/admin/communication/website/menus/static.html.erb
@@ -0,0 +1,5 @@
+<%= raw @menu.items
+             .root
+             .ordered
+             .map(&:to_static_hash)
+             .to_yaml %>
diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb
index 4cb44c14b..b7b890ce6 100644
--- a/app/views/admin/communication/website/pages/_form.html.erb
+++ b/app/views/admin/communication/website/pages/_form.html.erb
@@ -10,7 +10,7 @@
           <%= f.input :description %>
           <%= f.input :text, as: :rich_text_area %>
           <%= f.association :related_category,
-                            collection: @website.list_of_categories,
+                            collection: collection_tree(@website.categories),
                             label_method: ->(p) { sanitize p[:label] },
                             value_method: ->(p) { p[:id] } if @website.categories.any? %>
         </div>
@@ -30,7 +30,7 @@
                       } %>
           <%= f.input :published %>
           <%= f.association :parent,
-                            collection: collection_tree(page.website.pages, page),
+                            collection: collection_tree(@website.pages, page),
                             label_method: ->(p) { sanitize p[:label] },
                             value_method: ->(p) { p[:id] } %>
           <ul>
diff --git a/app/views/admin/communication/website/posts/_form.html.erb b/app/views/admin/communication/website/posts/_form.html.erb
index e2d1226be..f575dced1 100644
--- a/app/views/admin/communication/website/posts/_form.html.erb
+++ b/app/views/admin/communication/website/posts/_form.html.erb
@@ -30,7 +30,7 @@
           <%= f.association :author, collection: current_university.administration_members.authors.ordered %>
           <%= f.association :categories,
                             as: :check_boxes,
-                            collection: @website.list_of_categories.map { |category| [
+                            collection: collection_tree(@website.categories).map { |category| [
                               sanitize(category[:label]),
                               category[:id],
                               { data: { parent: category[:parent_id] } }
diff --git a/app/views/admin/education/programs/_form.html.erb b/app/views/admin/education/programs/_form.html.erb
index 66de89881..93e2959b5 100644
--- a/app/views/admin/education/programs/_form.html.erb
+++ b/app/views/admin/education/programs/_form.html.erb
@@ -22,7 +22,7 @@
                             as: :check_boxes,
                             collection: current_university.education_schools.ordered %>
           <%= f.association :parent,
-                            collection: program.persisted? ? program.list_of_other_programs : current_university.list_of_programs,
+                            collection: collection_tree(current_university.programs, program),
                             label_method: ->(p) { sanitize p[:label] },
                             value_method: ->(p) { p[:id] } %>
         </div>
diff --git a/app/views/admin/education/schools/_form.html.erb b/app/views/admin/education/schools/_form.html.erb
index 93a0809b8..87c4e36d4 100644
--- a/app/views/admin/education/schools/_form.html.erb
+++ b/app/views/admin/education/schools/_form.html.erb
@@ -29,7 +29,7 @@
         <div class="card-body">
           <%= f.association :programs,
                             as: :check_boxes,
-                            collection: current_university.list_of_programs,
+                            collection: collection_tree(current_university.education_programs,
                             label_method: ->(p) { sanitize p[:label] },
                             value_method: ->(p) { p[:id] }  %>
         </div>
diff --git a/app/views/admin/education/teachers/_form.html.erb b/app/views/admin/education/teachers/_form.html.erb
index dd685aeac..82d37bc42 100644
--- a/app/views/admin/education/teachers/_form.html.erb
+++ b/app/views/admin/education/teachers/_form.html.erb
@@ -7,7 +7,7 @@
       <%= f.association :education_programs,
                         as: :check_boxes,
                         legend_tag: false,
-                        collection: current_university.list_of_programs,
+                        collection: collection_tree(current_university.education_programs),
                         label_method: ->(p) { sanitize p[:label] },
                         value_method: ->(p) { p[:id] }  %>
     </div>
diff --git a/app/views/admin/education/teachers/_list.html.erb b/app/views/admin/education/teachers/_list.html.erb
index 5ef8fafad..9f59a100f 100644
--- a/app/views/admin/education/teachers/_list.html.erb
+++ b/app/views/admin/education/teachers/_list.html.erb
@@ -9,7 +9,8 @@
   <tbody>
     <% teachers.each do |teacher| %>
       <tr>
-        <td><%= link_to teacher, admin_education_teacher_path(teacher) %></td>
+        <td><%= link_to teacher.last_name, admin_education_teacher_path(teacher) %></td>
+        <td><%= link_to teacher.first_name, admin_education_teacher_path(teacher) %></td>
         <td class="text-end">
           <div class="btn-group" role="group">
             <%= link_to t('edit'),
-- 
GitLab