From 150f1eb1aa16b0dc712bd87986a81ebbb3be7ea3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 12 May 2023 15:19:01 +0200
Subject: [PATCH] fix
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Pierre-André Boissinot <pierreandre.boissinot@noesya.coop>
---
 app/models/communication/website.rb           | 21 +----------
 .../website/with_git_repository.rb            |  6 ++++
 .../communication/website/with_languages.rb   | 36 +++++++++++++++++++
 3 files changed, 43 insertions(+), 20 deletions(-)
 create mode 100644 app/models/communication/website/with_languages.rb

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index f798c3443..7dcf06a8e 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -43,6 +43,7 @@ class Communication::Website < ApplicationRecord
   include WithGit
   include WithGitRepository
   include WithImport
+  include WithLanguages
   include WithOldDependencies
   include WithProgramCategories
   include WithReferences
@@ -57,16 +58,6 @@ class Communication::Website < ApplicationRecord
     gitlab: 1
   }
 
-  belongs_to :default_language, class_name: "Language"
-  has_and_belongs_to_many :languages,
-                          class_name: 'Language',
-                          join_table: 'communication_websites_languages',
-                          foreign_key: 'communication_website_id',
-                          association_foreign_key: 'language_id'
-
-  validates :languages, length: { minimum: 1 }
-  validate :languages_must_include_default_language
-
   before_validation :sanitize_fields
 
   scope :ordered, -> { order(:name) }
@@ -102,12 +93,6 @@ class Communication::Website < ApplicationRecord
     []
   end
 
-  def best_language_for(iso_code)
-    # We look for the language by the ISO code in the websites languages.
-    # If not found, we fallback to the default language.
-    languages.find_by(iso_code: iso_code) || default_language
-  end
-
   def website
     self
   end
@@ -126,8 +111,4 @@ class Communication::Website < ApplicationRecord
     self.repository = Osuny::Sanitizer.sanitize(self.repository, 'string')
     self.url = Osuny::Sanitizer.sanitize(self.url, 'string')
   end
-
-  def languages_must_include_default_language
-    errors.add(:languages, :must_include_default) unless language_ids.include?(default_language_id)
-  end
 end
diff --git a/app/models/communication/website/with_git_repository.rb b/app/models/communication/website/with_git_repository.rb
index d8e99687e..dd22d6592 100644
--- a/app/models/communication/website/with_git_repository.rb
+++ b/app/models/communication/website/with_git_repository.rb
@@ -5,6 +5,8 @@ module Communication::Website::WithGitRepository
     has_many :website_git_files,
              class_name: 'Communication::Website::GitFile',
              dependent: :destroy
+
+    after_save :destroy_obsolete_git_files, if: :should_clean_on_git?
   end
 
   def git_repository
@@ -30,4 +32,8 @@ module Communication::Website::WithGitRepository
   def exportable_to_git?
     true
   end
+
+  def should_clean_on_git?
+    saved_change_to_about_id? || language_was_removed
+  end
 end
diff --git a/app/models/communication/website/with_languages.rb b/app/models/communication/website/with_languages.rb
new file mode 100644
index 000000000..835a37c44
--- /dev/null
+++ b/app/models/communication/website/with_languages.rb
@@ -0,0 +1,36 @@
+module Communication::Website::WithLanguages
+  extend ActiveSupport::Concern
+
+  included do
+    attr_accessor :language_was_removed
+
+    belongs_to :default_language, class_name: "Language"
+    has_and_belongs_to_many :languages,
+                            class_name: 'Language',
+                            join_table: 'communication_websites_languages',
+                            foreign_key: 'communication_website_id',
+                            association_foreign_key: 'language_id',
+                            after_remove: :flag_languages_change
+
+    validates :languages, length: { minimum: 1 }
+    validate :languages_must_include_default_language
+
+  end
+
+  def best_language_for(iso_code)
+    # We look for the language by the ISO code in the websites languages.
+    # If not found, we fallback to the default language.
+    languages.find_by(iso_code: iso_code) || default_language
+  end
+
+  protected
+
+  def languages_must_include_default_language
+    errors.add(:languages, :must_include_default) unless language_ids.include?(default_language_id)
+  end
+
+  def flag_languages_change(_)
+    @language_was_removed = true
+  end
+
+end
-- 
GitLab