From 99e8a20c7f6144dcead3d30f8066b5949d66518d Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 4 Jan 2022 12:25:37 +0100
Subject: [PATCH] categories ok

---
 .../website/categories_controller.rb          |  9 ++--
 app/models/communication/website/category.rb  | 42 ++++++++++---------
 2 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/app/controllers/admin/communication/website/categories_controller.rb b/app/controllers/admin/communication/website/categories_controller.rb
index bb07fd3bd..8e0892e10 100644
--- a/app/controllers/admin/communication/website/categories_controller.rb
+++ b/app/controllers/admin/communication/website/categories_controller.rb
@@ -22,8 +22,7 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication
         skip_github_publication: true
       )
     end
-    github = Github.with_website @website
-    github.send_batch_to_website(categories, message: '[Category] Reorder categories.')
+    categories.first.sync_with_git
   end
 
   def children
@@ -54,7 +53,7 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication
 
   def create
     @category.website = @website
-    if @category.save
+    if @category.save_and_sync
       redirect_to admin_communication_website_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s)
     else
       breadcrumb
@@ -63,7 +62,7 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication
   end
 
   def update
-    if @category.update(category_params)
+    if @category.update_and_sync(category_params)
       redirect_to admin_communication_website_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s)
     else
       breadcrumb
@@ -73,7 +72,7 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication
   end
 
   def destroy
-    @category.destroy
+    @category.destroy_and_sync
     redirect_to admin_communication_website_categories_url, notice: t('admin.successfully_destroyed_html', model: @category.to_s)
   end
 
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index b0a3bae4e..8f294a62a 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -37,25 +37,25 @@ class Communication::Website::Category < ApplicationRecord
   include WithSlug # We override slug_unavailable? method
   include WithTree
 
-  has_one :imported_category,
-          class_name: 'Communication::Website::Imported::Category',
-          dependent: :destroy
-  belongs_to :university
-  belongs_to :website,
-             foreign_key: :communication_website_id
-  belongs_to :parent,
-            class_name: 'Communication::Website::Category',
-            optional: true
-  belongs_to :program,
-            class_name: 'Education::Program',
-            optional: true
-  has_one :imported_category,
-          class_name: 'Communication::Website::Imported::Category',
-          dependent: :destroy
-  has_many :children,
-           class_name: 'Communication::Website::Category',
-           foreign_key: :parent_id,
-           dependent: :destroy
+  has_one                 :imported_category,
+                          class_name: 'Communication::Website::Imported::Category',
+                          dependent: :destroy
+  belongs_to              :university
+  belongs_to              :website,
+                          foreign_key: :communication_website_id
+  belongs_to              :parent,
+                          class_name: 'Communication::Website::Category',
+                          optional: true
+  belongs_to              :program,
+                          class_name: 'Education::Program',
+                          optional: true
+  has_one                 :imported_category,
+                          class_name: 'Communication::Website::Imported::Category',
+                          dependent: :destroy
+  has_many                :children,
+                          class_name: 'Communication::Website::Category',
+                          foreign_key: :parent_id,
+                          dependent: :destroy
   has_and_belongs_to_many :posts,
                           class_name: 'Communication::Website::Post',
                           join_table: 'communication_website_categories_posts',
@@ -83,6 +83,10 @@ class Communication::Website::Category < ApplicationRecord
     "content/categories/#{path}/_index.html".gsub(/\/+/, '/')
   end
 
+  def git_dependencies(identifier)
+    posts
+  end
+
   def update_children_paths
     children.each do |child|
       child.update_column :path, child.generated_path
-- 
GitLab