From 02b0f373eca2814a4563ff47365461c8b3e763a6 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Thu, 16 Feb 2023 18:16:05 +0100
Subject: [PATCH] cats

---
 app/models/communication/website/category.rb         | 8 ++++++++
 app/models/communication/website/with_connections.rb | 9 ++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index e69b1a3ad..6c245b315 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -97,6 +97,14 @@ class Communication::Website::Category < ApplicationRecord
     "admin/communication/websites/categories/static"
   end
 
+  def direct_dependencies
+    active_storage_blobs +
+    blocks +
+    children +
+    posts +
+    [parent]
+  end
+
   def git_dependencies(website)
     [self, parent].compact + siblings + descendants + active_storage_blobs + posts + website.menus
   end
diff --git a/app/models/communication/website/with_connections.rb b/app/models/communication/website/with_connections.rb
index 547a0a0a9..c90c7af3d 100644
--- a/app/models/communication/website/with_connections.rb
+++ b/app/models/communication/website/with_connections.rb
@@ -8,12 +8,14 @@ module Communication::Website::WithConnections
               through: :connections,
               source: :object,
               source_type: 'University::Organization'
+    
+    after_save :clean_connections!
   end
 
   def clean_connections!
-    start = Time.now
+    # start = Time.now
     connect self
-    connections.where('updated_at < ?', time).destroy_all
+    # connections.where('updated_at < ?', start).destroy_all
   end
 
   def connect(object, source = nil)
@@ -40,7 +42,8 @@ module Communication::Website::WithConnections
 
   def connect_object(object, source)
     puts "connect_object #{object} from #{source}"
-    connections.where(university: university, object: object, source: source).first_or_create
+    connection = connections.where(university: university, object: object, source: source).first_or_create
+    connection.touch if connection.persisted?
   end
 
   def disconnect_object(object, source)
-- 
GitLab