From df53cce9d107e9b5831d6a9c2c6eafaac0f2bcf3 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 5 Jan 2022 12:54:47 +0100
Subject: [PATCH] destroy without dependencies

---
 app/models/communication/website/git_file.rb |  7 +++++--
 app/models/communication/website/page.rb     |  2 +-
 app/models/communication/website/post.rb     |  2 +-
 app/models/concerns/with_git.rb              | 12 +++++++++++-
 app/models/research/journal/article.rb       |  6 +-----
 app/models/research/journal/volume.rb        |  6 +-----
 app/services/git/repository.rb               |  5 ++++-
 7 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb
index 4cef3c3d0..83607a85d 100644
--- a/app/models/communication/website/git_file.rb
+++ b/app/models/communication/website/git_file.rb
@@ -25,8 +25,11 @@ class Communication::Website::GitFile < ApplicationRecord
   belongs_to :website, class_name: 'Communication::Website'
   belongs_to :about, polymorphic: true
 
-  def self.sync(website, object, identifier)
+  attr_accessor :will_be_destroyed
+
+  def self.sync(website, object, identifier, destroy: false)
     git_file = where(website: website, about: object, identifier: identifier).first_or_create
+    git_file.will_be_destroyed = destroy
     website.git_repository.add_git_file git_file
   end
 
@@ -43,7 +46,7 @@ class Communication::Website::GitFile < ApplicationRecord
   end
 
   def should_destroy?
-    path.nil?
+    will_be_destroyed || path.nil?
   end
 
   def path
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 365463b5c..abb50444c 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -72,7 +72,7 @@ class Communication::Website::Page < ApplicationRecord
   scope :recent, -> { order(updated_at: :desc).limit(5) }
 
   def git_path_static
-    "content/pages/#{path}/_index.html".gsub(/\/+/, '/')
+    "content/pages/#{path}/_index.html".gsub(/\/+/, '/') if published
   end
 
   def git_dependencies_static
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index c852f337c..ae04c83bc 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -68,7 +68,7 @@ class Communication::Website::Post < ApplicationRecord
   end
 
   def git_path_static
-    "content/posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
+    "content/posts/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at
   end
 
   def git_dependencies_static
diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb
index 2a495b996..f276cdb01 100644
--- a/app/models/concerns/with_git.rb
+++ b/app/models/concerns/with_git.rb
@@ -32,8 +32,18 @@ module WithGit
   end
 
   def destroy_and_sync
-    # TODO
+    destroy_from_git
     destroy
+    true
+  end
+
+  def destroy_from_git
+    websites_with_fallback.each do |website|
+      identifiers.each do |identifier|
+        Communication::Website::GitFile.sync website, self, identifier, destroy: true
+      end
+      website.git_repository.sync!
+    end
   end
 
   def sync_with_git
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index 471619898..c3a6ca938 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -56,11 +56,7 @@ class Research::Journal::Article < ApplicationRecord
   end
 
   def git_path_static
-    if published_at
-      "content/articles/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
-    else
-      nil
-    end
+    "content/articles/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at
   end
 
   def git_dependencies_static
diff --git a/app/models/research/journal/volume.rb b/app/models/research/journal/volume.rb
index 0537912b2..90873539a 100644
--- a/app/models/research/journal/volume.rb
+++ b/app/models/research/journal/volume.rb
@@ -47,11 +47,7 @@ class Research::Journal::Volume < ApplicationRecord
   end
 
   def git_path_static
-    if published_at
-      "content/volumes/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
-    else
-      nil
-    end
+    "content/volumes/#{published_at.year}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" if published_at
   end
 
   def git_dependencies_static
diff --git a/app/services/git/repository.rb b/app/services/git/repository.rb
index afbd697a9..41b23c809 100644
--- a/app/services/git/repository.rb
+++ b/app/services/git/repository.rb
@@ -6,7 +6,10 @@ class Git::Repository
   end
 
   def add_git_file(git_file)
-    @commit_message = "[#{ git_file.about.class.name }] Save #{ git_file.about }" if git_files.empty?
+    if git_files.empty?
+      action = git_file.should_destroy? ? "Destroy" : "Save"
+      @commit_message = "[#{ git_file.about.class.name }] #{ action } #{ git_file.about }"
+    end
     git_files << git_file
   end
 
-- 
GitLab