From 6001f7bf2fd4ed35b6c87d2f1ac698db6ee0a7fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Wed, 29 Dec 2021 15:30:20 +0100
Subject: [PATCH] reorder

---
 .../admin/communication/website/categories_controller.rb | 8 +++++++-
 .../admin/communication/website/pages_controller.rb      | 1 -
 app/models/communication/website/category.rb             | 9 +++++++++
 app/models/education/program.rb                          | 3 +--
 .../communication/website/categories/static.html.erb     | 1 +
 5 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/app/controllers/admin/communication/website/categories_controller.rb b/app/controllers/admin/communication/website/categories_controller.rb
index d0fa3d229..bb07fd3bd 100644
--- a/app/controllers/admin/communication/website/categories_controller.rb
+++ b/app/controllers/admin/communication/website/categories_controller.rb
@@ -11,13 +11,19 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication
   def reorder
     parent_id = params[:parentId].blank? ? nil : params[:parentId]
     ids = params[:ids] || []
+    categories = []
     ids.each.with_index do |id, index|
       category = @website.categories.find(id)
+      categories << category
+      categories.concat(category.descendents) if parent_id != category.parent_id
       category.update(
         parent_id: parent_id,
-        position: index + 1
+        position: index + 1,
+        skip_github_publication: true
       )
     end
+    github = Github.with_website @website
+    github.send_batch_to_website(categories, message: '[Category] Reorder categories.')
   end
 
   def children
diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb
index d9788ae24..17fb1954a 100644
--- a/app/controllers/admin/communication/website/pages_controller.rb
+++ b/app/controllers/admin/communication/website/pages_controller.rb
@@ -11,7 +11,6 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web
     parent_id = params[:parentId].blank? ? nil : params[:parentId]
     ids = params[:ids] || []
     pages = []
-    github_files = []
     ids.each.with_index do |id, index|
       page = @website.pages.find(id)
       pages << page
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index c9c96a35a..a055f367a 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -65,6 +65,8 @@ class Communication::Website::Category < ApplicationRecord
   validates :name, presence: true
   validates :slug, uniqueness: { scope: :communication_website_id }
 
+  after_save :update_children_paths, if: :saved_change_to_path?
+
   scope :ordered, -> { order(:position) }
 
   before_create :set_position
@@ -83,6 +85,13 @@ class Communication::Website::Category < ApplicationRecord
     "content/categories/#{path}/_index.html".gsub(/\/+/, '/')
   end
 
+  def update_children_paths
+    children.each do |child|
+      child.update_column :path, child.generated_path
+      child.update_children_paths
+    end
+  end
+
   protected
 
   def set_position
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index 55ed4b6ad..f9b15c3ad 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -90,7 +90,6 @@ class Education::Program < ApplicationRecord
 
   validates_presence_of :name
 
-  before_validation :make_path
   after_save :update_children_paths, if: :saved_change_to_path?
   after_save_commit :set_websites_categories, unless: :skip_websites_categories_callback
 
@@ -125,6 +124,6 @@ class Education::Program < ApplicationRecord
   end
 
   def set_websites_categories
-    websites.find_each(&:set_programs_categories!)
+    # websites.find_each(&:set_programs_categories!)
   end
 end
diff --git a/app/views/admin/communication/website/categories/static.html.erb b/app/views/admin/communication/website/categories/static.html.erb
index cc16d5377..9fe83ce6e 100644
--- a/app/views/admin/communication/website/categories/static.html.erb
+++ b/app/views/admin/communication/website/categories/static.html.erb
@@ -1,5 +1,6 @@
 ---
 title: "<%= @category.name %>"
 slug: "<%= @category.slug %>"
+position: <%= @category.position %>
 ---
 <%= prepare_for_github @category.description, @category.university %>
-- 
GitLab