From f8130a0e0570a9b57f313aebe8140047070a4d17 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Tue, 7 Dec 2021 17:48:04 +0100
Subject: [PATCH] blob handled in github file

---
 .../communication/website/github_file.rb      | 22 +++++++++++++++++--
 .../website/with_publishable_objects.rb       | 21 ------------------
 2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/app/models/communication/website/github_file.rb b/app/models/communication/website/github_file.rb
index 17aa44451..79febf595 100644
--- a/app/models/communication/website/github_file.rb
+++ b/app/models/communication/website/github_file.rb
@@ -78,7 +78,17 @@ class Communication::Website::GithubFile < ApplicationRecord
 
   def remove_from_github
     return unless github.valid?
-    github.remove(github_path, github_remove_commit_message)
+    github.remove github_path, github_remove_commit_message
+    remove_media_from_github
+  end
+
+  def remove_media_from_github
+    return unless about.respond_to?(:active_storage_blobs)
+    about.active_storage_blobs.each { |blob| remove_blob_from_github(blob) }
+  end
+
+  def remove_blob_from_github(blob)
+    github.remove github_blob_path(blob), github_blob_remove_commit_message
   end
 
   def github
@@ -96,7 +106,7 @@ class Communication::Website::GithubFile < ApplicationRecord
   def github_blob_params(blob)
     blob.analyze unless blob.analyzed?
     {
-      path: "_data/media/#{blob.id[0..1]}/#{blob.id}.yml",
+      path: github_blob_path(blob),
       data: ApplicationController.render(
         template: 'active_storage/blobs/jekyll',
         layout: false,
@@ -105,6 +115,10 @@ class Communication::Website::GithubFile < ApplicationRecord
     }
   end
 
+  def github_blob_path(blob)
+    "_data/media/#{blob.id[0..1]}/#{blob.id}.yml"
+  end
+
   def github_commit_message
     "[#{about.class.name.demodulize}] Save #{about.to_s}"
   end
@@ -116,4 +130,8 @@ class Communication::Website::GithubFile < ApplicationRecord
   def github_remove_commit_message
     "[#{about.class.name.demodulize}] Remove #{about.to_s}"
   end
+
+  def github_blob_remove_commit_message(blob)
+    "[Medium] Remove ##{blob.id}"
+  end
 end
diff --git a/app/models/communication/website/with_publishable_objects.rb b/app/models/communication/website/with_publishable_objects.rb
index 78b8d7f2b..d2c019325 100644
--- a/app/models/communication/website/with_publishable_objects.rb
+++ b/app/models/communication/website/with_publishable_objects.rb
@@ -2,32 +2,11 @@ module Communication::Website::WithPublishableObjects
   extend ActiveSupport::Concern
 
   included do
-
     def publish_about_object
       # TODO: Handle Research::Journal then use the commented version.
       # about.force_publish! unless about.nil?
       about.force_publish! if about.is_a?(Education::School)
     end
 
-    def publish_blob(blob)
-      return unless github.valid?
-      blob.analyze unless blob.analyzed?
-      github_path = "_data/media/#{blob.id[0..1]}/#{blob.id}.yml"
-      github_commit_message = "[Medium] Save ##{blob.id}"
-      data = ApplicationController.render(
-        template: 'active_storage/blobs/jekyll',
-        layout: false,
-        assigns: { blob: blob }
-      )
-      github.publish(path: github_path, commit: github_commit_message, data: data)
-    end
-    handle_asynchronously :publish_blob, queue: 'default'
-
-    def remove_blob(blob)
-      return unless github.valid?
-      github_path = "_data/media/#{blob.id[0..1]}/#{blob.id}.yml"
-      github_commit_message = "[Medium] Remove ##{blob.id}"
-      github.remove(github_path, github_commit_message)
-    end
   end
 end
-- 
GitLab