From e6474424500fd04d9653c8d02f2a44613081173e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 2 Dec 2022 16:44:47 +0100
Subject: [PATCH] create previous links

---
 app/models/communication/website/category.rb  |  5 ++++
 app/models/communication/website/page.rb      |  5 ++++
 app/models/communication/website/post.rb      |  8 +++++++
 .../communication/website/previous_link.rb    |  5 ----
 app/models/concerns/with_website_permalink.rb | 11 ++++++++-
 .../concerns/with_website_previous_links.rb   | 24 +++++++++++++++++++
 app/models/university/person.rb               |  1 +
 7 files changed, 53 insertions(+), 6 deletions(-)
 create mode 100644 app/models/concerns/with_website_previous_links.rb

diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index b64503042..e3af2efb6 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -46,6 +46,7 @@ class Communication::Website::Category < ApplicationRecord
   include WithTree
   include WithPosition
   include WithWebsitePermalink
+  include WithWebsitePreviousLinks
 
   has_one                 :imported_category,
                           class_name: 'Communication::Website::Imported::Category',
@@ -111,6 +112,10 @@ class Communication::Website::Category < ApplicationRecord
     raw_permalink_for_website(website).gsub(':slug', self.path)
   end
 
+  def previous_computed_permalink_for_website(website)
+    raw_permalink_for_website(website).gsub(':slug', self.path_was)
+  end
+
   protected
 
   def last_ordered_element
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 29e2add7d..2ca22a786 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -54,6 +54,7 @@ class Communication::Website::Page < ApplicationRecord
   include WithTree
   include WithPath
   include WithWebsitePermalink
+  include WithWebsitePreviousLinks
 
   has_summernote :text
 
@@ -145,6 +146,10 @@ class Communication::Website::Page < ApplicationRecord
     path
   end
 
+  def previous_computed_permalink_for_website(website)
+    path_was
+  end
+
   protected
 
   def check_accessibility
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 50488f275..9f0286c19 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -44,6 +44,7 @@ class Communication::Website::Post < ApplicationRecord
   include WithMenuItemTarget
   include WithSlug # We override slug_unavailable? method
   include WithWebsitePermalink
+  include WithWebsitePreviousLinks
 
   has_summernote :text
 
@@ -150,6 +151,13 @@ class Communication::Website::Post < ApplicationRecord
       .gsub(':year/:month/:day', published_at.strftime("%Y/%m/%d"))
   end
 
+  def previous_computed_permalink_for_website(website)
+    return unless website.id == communication_website_id && published_was && published_at_was
+    raw_permalink_for_website(website)
+      .gsub(':slug', self.slug_was)
+      .gsub(':year/:month/:day', published_at_was.strftime("%Y/%m/%d"))
+  end
+
   def to_s
     "#{title}"
   end
diff --git a/app/models/communication/website/previous_link.rb b/app/models/communication/website/previous_link.rb
index 44ca9255e..b099d84c6 100644
--- a/app/models/communication/website/previous_link.rb
+++ b/app/models/communication/website/previous_link.rb
@@ -30,15 +30,10 @@ class Communication::Website::PreviousLink < ApplicationRecord
   belongs_to :about, polymorphic: true
 
   before_validation :set_university, on: :create
-  after_destroy_commit :sync_about
 
   private
 
   def set_university
     self.university_id = website.university_id
   end
-
-  def sync_about
-    about.sync_with_git
-  end
 end
diff --git a/app/models/concerns/with_website_permalink.rb b/app/models/concerns/with_website_permalink.rb
index f3691e458..d9a08b7d4 100644
--- a/app/models/concerns/with_website_permalink.rb
+++ b/app/models/concerns/with_website_permalink.rb
@@ -8,8 +8,17 @@ module WithWebsitePermalink
       computed_permalink.present? ? Static.clean_path(computed_permalink) : nil
     end
 
+    def previous_permalink_for_website(website)
+      computed_permalink = previous_computed_permalink_for_website(website)
+      computed_permalink.present? ? Static.clean_path(computed_permalink) : nil
+    end
+
     def computed_permalink_for_website(website)
-      raw_permalink_for_website(website).gsub(':slug', self.slug)
+      raw_permalink_for_website(website)&.gsub(':slug', self.slug)
+    end
+
+    def previous_computed_permalink_for_website(website)
+      raw_permalink_for_website(website)&.gsub(':slug', self.slug_was)
     end
 
     protected
diff --git a/app/models/concerns/with_website_previous_links.rb b/app/models/concerns/with_website_previous_links.rb
new file mode 100644
index 000000000..09134b509
--- /dev/null
+++ b/app/models/concerns/with_website_previous_links.rb
@@ -0,0 +1,24 @@
+module WithWebsitePreviousLinks
+  extend ActiveSupport::Concern
+
+  included do
+
+    has_many  :previous_links,
+              class_name: "Communication::Website::PreviousLink",
+              as: :about,
+              dependent: :destroy
+
+    after_validation :manage_previous_links, on: [:create, :update]
+
+    def manage_previous_links
+      websites_for_self.each do |website|
+        old_permalink = previous_permalink_for_website(website)
+        new_permalink = permalink_for_website(website)
+
+        # If the object had a permalink and now is different, we create a previous link
+        previous_links.create(website: website, link: old_permalink) if old_permalink.present? && new_permalink != old_permalink
+      end
+    end
+
+  end
+end
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 622c91a59..0585737af 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -57,6 +57,7 @@ class University::Person < ApplicationRecord
   include WithRoles
   include WithBlocks
   include WithWebsitePermalink
+  include WithWebsitePreviousLinks
 
   LIST_OF_ROLES = [
     :administration,
-- 
GitLab