From f8364706e4ccb1360bf79166753f7b45a970cdd0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 28 Apr 2023 11:21:07 +0200
Subject: [PATCH] exportable_to_git? + rangement
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Arnaud Levy <arnaud.levy@noesya.coop>
Co-authored-by: Pierre-André Boissinot <pierreandre.boissinot@noesya.coop>
---
 app/models/communication/website.rb           |  1 +
 app/models/communication/website/git_file.rb  | 10 +++-
 .../communication/website/with_configs.rb     |  9 ++++
 .../website/with_git_repository.rb            |  6 +++
 .../website/with_old_dependencies.rb          | 52 ++-----------------
 app/models/concerns/with_git.rb               | 13 ++---
 app/models/concerns/with_git_files.rb         |  4 ++
 7 files changed, 37 insertions(+), 58 deletions(-)

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 6e9502353..2de5b4a46 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -88,6 +88,7 @@ class Communication::Website < ApplicationRecord
   end
 
   def dependencies
+    configs +
     pages +
     posts +
     categories +
diff --git a/app/models/communication/website/git_file.rb b/app/models/communication/website/git_file.rb
index 08c744661..b4352b3dc 100644
--- a/app/models/communication/website/git_file.rb
+++ b/app/models/communication/website/git_file.rb
@@ -29,8 +29,14 @@ class Communication::Website::GitFile < ApplicationRecord
   attr_accessor :will_be_destroyed
 
   def self.sync(website, object, destroy: false)
-    # A dependency does not always have a GitFile (ex: Communication::Block)
-    return unless object.respond_to?(:git_files)
+    # All exportable objects must respond to this method
+    # WithGitFiles defines it
+    # AsDirectObject includes WithGitFiles, therefore all direct objects are exportable
+    # AsIndirectObject does not include it, but some indirect objects have it (Person, Organization...)
+    # Some objects need to declare that property:
+    # - the website itself
+    # - configs (which inherit from the website)
+    return unless object.try(:exportable_to_git?)
     # Permalinks must be calculated BEFORE renders
     manage_permalink object, website
     # Blobs need to be completely analyzed, which is async
diff --git a/app/models/communication/website/with_configs.rb b/app/models/communication/website/with_configs.rb
index 34cb40689..5c5ff105e 100644
--- a/app/models/communication/website/with_configs.rb
+++ b/app/models/communication/website/with_configs.rb
@@ -3,6 +3,15 @@ module Communication::Website::WithConfigs
 
   included do
 
+    def configs
+      [
+        config_default_languages,
+        config_default_permalinks,
+        config_development_config,
+        config_production_config
+      ]
+    end
+
     def config_default_languages
       @config_default_languages ||= Communication::Website::Configs::DefaultLanguages.find(id)
     end
diff --git a/app/models/communication/website/with_git_repository.rb b/app/models/communication/website/with_git_repository.rb
index fd6bb72da..3f76c0fe0 100644
--- a/app/models/communication/website/with_git_repository.rb
+++ b/app/models/communication/website/with_git_repository.rb
@@ -24,4 +24,10 @@ module Communication::Website::WithGitRepository
     self.git_repository.sync!
   end
   handle_asynchronously :destroy_obsolete_git_files, queue: :default
+
+  # Le website devient data/website.yml
+  # Les configs héritent du modèle website et s'exportent en différents fichiers
+  def exportable_to_git?
+    true
+  end
 end
diff --git a/app/models/communication/website/with_old_dependencies.rb b/app/models/communication/website/with_old_dependencies.rb
index 39e51edea..0498e80c6 100644
--- a/app/models/communication/website/with_old_dependencies.rb
+++ b/app/models/communication/website/with_old_dependencies.rb
@@ -52,46 +52,12 @@ module Communication::Website::WithOldDependencies
     has_teachers? ? about.teachers : University::Person.none
   end
 
-  def people_in_blocks
-    @people_in_blocks ||= blocks_dependencies.reject { |dependency| !dependency.is_a? University::Person }
+  def people
+    connected_persons
   end
 
   def organizations
-    organizations_in_blocks
-  end
-
-  def organizations_in_blocks
-    @organizations_in_blocks ||= blocks_dependencies.reject { |dependency| !dependency.is_a? University::Organization }
-  end
-
-  def people_with_facets_in_blocks
-    @people_with_facets_in_blocks ||= blocks_dependencies.reject { |dependency| !dependency.class.to_s.start_with?('University::Person') }
-  end
-
-  def people
-    # TODO: Scoper aux langues du website dans le cas où une personne serait traduite dans + de langues
-    @people ||= begin
-      people = []
-      people += authors if has_authors?
-      people += teachers if has_teachers?
-      people += administrators if has_administrators?
-      people += researchers if has_researchers?
-      people += people_in_blocks if has_people_in_blocks?
-      people.uniq.compact
-    end
-  end
-
-  def people_with_facets
-    # TODO: Scoper aux langues du website dans le cas où une personne serait traduite dans + de langues
-    @people_with_facets ||= begin
-      people_with_facets = people
-      people_with_facets += authors.compact.map(&:author) if has_authors?
-      people_with_facets += teachers.compact.map(&:teacher) if has_teachers?
-      people_with_facets += administrators.compact.map(&:administrator) if has_administrators?
-      people_with_facets += researchers.compact.map(&:researcher) if has_researchers?
-      people_with_facets += people_with_facets_in_blocks if has_people_in_blocks?
-      people_with_facets.uniq.compact
-    end
+    connected_organizations
   end
 
   # Deprecated, needs refactor for performance
@@ -105,23 +71,15 @@ module Communication::Website::WithOldDependencies
   end
 
   def has_organizations?
-    has_organizations_in_blocks?
+    connected_organizations.any?
   end
 
   def has_authors?
     authors.compact.any?
   end
 
-  def has_people_in_blocks?
-    people_in_blocks.compact.any?
-  end
-
-  def has_organizations_in_blocks?
-    organizations_in_blocks.compact.any?
-  end
-
   def has_persons?
-    has_authors? || has_administrators? || has_researchers? || has_teachers? || has_people_in_blocks?
+    connected_people.any?
   end
 
   def has_administrators?
diff --git a/app/models/concerns/with_git.rb b/app/models/concerns/with_git.rb
index b06c6f5bf..0752db0be 100644
--- a/app/models/concerns/with_git.rb
+++ b/app/models/concerns/with_git.rb
@@ -1,3 +1,7 @@
+# Donne la capacité de se synchroniser avec Git lors d'une opération ActiveRecord
+# Utilisé par :
+# - website
+# - objets directs
 module WithGit
   extend ActiveSupport::Concern
 
@@ -45,13 +49,4 @@ module WithGit
     website.git_repository.sync!
   end
 
-  def for_website?(website)
-    website.id == website_id
-  end
-
-  protected
-
-  def website_for_self
-    is_a?(Communication::Website) ? self : website
-  end
 end
diff --git a/app/models/concerns/with_git_files.rb b/app/models/concerns/with_git_files.rb
index 1e5ad1268..8d77e4a8a 100644
--- a/app/models/concerns/with_git_files.rb
+++ b/app/models/concerns/with_git_files.rb
@@ -12,6 +12,10 @@ module WithGitFiles
     raise NotImplementedError
   end
 
+  def exportable_to_git?
+    true
+  end
+
   def git_path_content_prefix(website)
     # Handle language-less objects
     # TODO I18n: Right now, we use the language of the object, fallbacking on the language of the website. In the end, we'll only use the language of the object
-- 
GitLab