From fd6fecd1bd03decaa470d5783c96b863d906fcf3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 24 Apr 2023 11:59:17 +0200
Subject: [PATCH] about website connected

---
 .../communication/website/with_connected_objects.rb      | 9 ++++++++-
 .../communication/website/with_old_dependencies.rb       | 6 +++---
 app/models/concerns/with_abouts.rb                       | 7 +++++++
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/app/models/communication/website/with_connected_objects.rb b/app/models/communication/website/with_connected_objects.rb
index 21fe01143..98dede27c 100644
--- a/app/models/communication/website/with_connected_objects.rb
+++ b/app/models/communication/website/with_connected_objects.rb
@@ -3,6 +3,8 @@ module Communication::Website::WithConnectedObjects
 
   included do
     has_many :connections
+
+    after_save :connect_about, if: :saved_change_to_about_id?
   end
 
   # Appelé par un objet avec des connexions lorsqu'il est destroyed
@@ -59,6 +61,11 @@ module Communication::Website::WithConnectedObjects
 
   protected
 
+  def connect_about
+    self.connect(about, self) if about.present? && about.try(:is_indirect_object?)
+    destroy_obsolete_connections
+  end
+
   def connect_object(indirect_object, direct_source, direct_source_type: nil)
     return unless should_connect?(indirect_object, direct_source)
     # puts "connect #{object} (#{object.class})"
@@ -81,7 +88,7 @@ module Communication::Website::WithConnectedObjects
     # On ne connecte pas le site à lui-même
     return false if indirect_object.is_a?(Communication::Website)
     # On ne connecte pas les objets directs (en principe ça n'arrive pas)
-    return false if indirect_object.respond_to?(:is_direct_object?) && indirect_object.is_direct_object?
+    return false if indirect_object.try(:is_direct_object?)
     true
   end
 end
\ No newline at end of file
diff --git a/app/models/communication/website/with_old_dependencies.rb b/app/models/communication/website/with_old_dependencies.rb
index 527d1aa98..39e51edea 100644
--- a/app/models/communication/website/with_old_dependencies.rb
+++ b/app/models/communication/website/with_old_dependencies.rb
@@ -41,15 +41,15 @@ module Communication::Website::WithOldDependencies
   end
 
   def administrators
-    about&.administrators
+    has_administrators? ? about.administrators : University::Person.none
   end
 
   def researchers
-    about&.researchers
+    has_researchers? ? about.researchers : University::Person.none
   end
 
   def teachers
-    about&.teachers
+    has_teachers? ? about.teachers : University::Person.none
   end
 
   def people_in_blocks
diff --git a/app/models/concerns/with_abouts.rb b/app/models/concerns/with_abouts.rb
index c2cbb413b..4be1b3809 100644
--- a/app/models/concerns/with_abouts.rb
+++ b/app/models/concerns/with_abouts.rb
@@ -6,6 +6,8 @@ module WithAbouts
                 polymorphic: true,
                 optional: true
 
+    before_validation :nullify_about_id_if_about_type_changed_to_blank
+
     scope :for_about_type, -> (type) { where(about_type: type) }
 
     def self.about_types
@@ -17,6 +19,11 @@ module WithAbouts
         Research::Journal.name,
       ]
     end
+  end
+
+  protected
 
+  def nullify_about_id_if_about_type_changed_to_blank
+    self.about_id = nil if about_type_changed? && about_type.blank?
   end
 end
-- 
GitLab