From 3500172be64279bca0c522e333cd46c9f2a1e770 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Mon, 20 Feb 2023 16:13:37 +0100
Subject: [PATCH] wip

---
 .../websites/connections_controller.rb        |  1 -
 .../communication/websites_controller.rb      |  1 -
 app/models/communication/block.rb             | 15 ++++++++--
 .../communication/block/component/base.rb     |  4 +++
 .../communication/block/template/base.rb      |  4 +++
 .../communication/website/connection.rb       |  7 -----
 .../communication/website/with_connections.rb |  2 +-
 app/models/concerns/with_dependencies.rb      |  2 +-
 app/models/concerns/with_websites.rb          |  2 +-
 app/models/university/organization.rb         |  1 +
 .../admin/application/_dependencies.html.erb  | 21 ++++++++++++++
 .../admin/communication/blocks/show.html.erb  |  1 +
 .../websites/connections/show.html.erb        | 28 ++++---------------
 .../websites/pages/show.html.erb              |  1 +
 14 files changed, 53 insertions(+), 37 deletions(-)

diff --git a/app/controllers/admin/communication/websites/connections_controller.rb b/app/controllers/admin/communication/websites/connections_controller.rb
index 02820624b..6a15e0607 100644
--- a/app/controllers/admin/communication/websites/connections_controller.rb
+++ b/app/controllers/admin/communication/websites/connections_controller.rb
@@ -6,7 +6,6 @@ class Admin::Communication::Websites::ConnectionsController < Admin::Communicati
 
   def show
     @connection = @website.connections.find params[:id]
-    @others = @connection.for_same_object
     breadcrumb
     add_breadcrumb @connection
   end
diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index d83628439..5cb335ea0 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -5,7 +5,6 @@ class Admin::Communication::WebsitesController < Admin::Communication::Websites:
   def index
     @websites = apply_scopes(@websites).ordered.page(params[:page])
     breadcrumb
-    add_breadcrumb Communication::Website.model_name.human(count: 2), admin_communication_websites_path
   end
 
   def show
diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb
index 5bcac1570..e723210bf 100644
--- a/app/models/communication/block.rb
+++ b/app/models/communication/block.rb
@@ -39,9 +39,6 @@ class Communication::Block < ApplicationRecord
   # template_blobs would be a better name, because there are files
   has_many_attached :template_images
 
-  delegate :website, to: :about
-  delegate :websites, to: :about
-
   enum template_kind: {
     chapter: 50,
     image: 51,
@@ -86,6 +83,18 @@ class Communication::Block < ApplicationRecord
     template.data
   end
 
+  def website
+    about.website
+  rescue
+    nil
+  end
+
+  def websites
+    about.websites
+  rescue
+    []
+  end
+
   def display_dependencies
     template.display_dependencies
   end
diff --git a/app/models/communication/block/component/base.rb b/app/models/communication/block/component/base.rb
index f49dfdb03..0c5b241af 100644
--- a/app/models/communication/block/component/base.rb
+++ b/app/models/communication/block/component/base.rb
@@ -37,4 +37,8 @@ class Communication::Block::Component::Base
   def translate!
     # By default, does nothing. Specific cases are handled in their own definitions. (example: post)
   end
+
+  def to_s
+    self.class.to_s.demodulize
+  end
 end
diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb
index 98799b982..abe513efd 100644
--- a/app/models/communication/block/template/base.rb
+++ b/app/models/communication/block/template/base.rb
@@ -150,6 +150,10 @@ class Communication::Block::Template::Base
     hash
   end
 
+  def to_s
+    self.class.to_s.demodulize
+  end
+
   protected
 
   def build_component(property)
diff --git a/app/models/communication/website/connection.rb b/app/models/communication/website/connection.rb
index 56744bfba..1ab29f0ca 100644
--- a/app/models/communication/website/connection.rb
+++ b/app/models/communication/website/connection.rb
@@ -35,13 +35,6 @@ class Communication::Website::Connection < ApplicationRecord
     for_object(object).distinct(:website).collect(&:website).uniq
   end
 
-  def for_same_object
-    self.class.where( university: university, 
-                      website: website,
-                      object: object)
-              .where.not(id: id)
-  end
-
   def to_s
     "#{id.split('-').first}"
   end
diff --git a/app/models/communication/website/with_connections.rb b/app/models/communication/website/with_connections.rb
index 7760da784..c7b01d907 100644
--- a/app/models/communication/website/with_connections.rb
+++ b/app/models/communication/website/with_connections.rb
@@ -10,7 +10,7 @@ module Communication::Website::WithConnections
   def clean_connections!
     start = Time.now
     connect self
-    # connections.where('updated_at < ?', start).destroy_all
+    connections.where('updated_at < ?', start).destroy_all
   end
 
   def connect(object)
diff --git a/app/models/concerns/with_dependencies.rb b/app/models/concerns/with_dependencies.rb
index 75ea2c51e..3a4f865c8 100644
--- a/app/models/concerns/with_dependencies.rb
+++ b/app/models/concerns/with_dependencies.rb
@@ -20,7 +20,7 @@ module WithDependencies
     reference_dependencies.each do |dependency|
       add_dependency_to_array array, dependency
     end
-    array
+    array.compact
   end
 
   protected
diff --git a/app/models/concerns/with_websites.rb b/app/models/concerns/with_websites.rb
index dcdcc4fb1..ee0e237e3 100644
--- a/app/models/concerns/with_websites.rb
+++ b/app/models/concerns/with_websites.rb
@@ -12,7 +12,7 @@ module WithWebsites
   protected
   
   def connect_to_websites
-    if respond_to?(:website)
+    if respond_to?(:website) && !website.nil?
       website.connect self
     else
       websites.each { |website| website.connect self }
diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb
index baedf60ef..101c87ca3 100644
--- a/app/models/university/organization.rb
+++ b/app/models/university/organization.rb
@@ -39,6 +39,7 @@ class University::Organization < ApplicationRecord
   include Sanitizable
   include WithBlobs
   include WithBlocks
+  include WithDependencies
   include WithGit
   include WithPermalink
   include WithSlug
diff --git a/app/views/admin/application/_dependencies.html.erb b/app/views/admin/application/_dependencies.html.erb
index 2602c5909..5ab6bbc49 100644
--- a/app/views/admin/application/_dependencies.html.erb
+++ b/app/views/admin/application/_dependencies.html.erb
@@ -10,6 +10,27 @@ expanded ||= false
     aria-controls="dependencies"></i>
 <div id="dependencies" class="collapse">
   <%= osuny_panel 'Dependencies', subtitle: 'Only for server admins' do %>
+    <h2 class="h5">dependencies</h2>
+    <div class="table-responsive">
+      <table class="<%= table_classes %>">
+          <thead>
+              <tr>
+                  <th>Name</th>
+                  <th>Class</th>
+              </tr>
+          </thead>
+          <tbody>
+              <% dependencies.each do |dependency| %>
+                  <tr>
+                      <td><%= dependency.to_s %></td>
+                      <td><%= dependency.class %></td>
+                  </tr>
+              <% end %>
+          </tbody>
+      </table>
+    </div>
+    
+    
     <h2 class="h5">git_dependencies</h2>
     <div class="table-responsive">
       <table class="<%= table_classes %>">
diff --git a/app/views/admin/communication/blocks/show.html.erb b/app/views/admin/communication/blocks/show.html.erb
index 42dcbe027..649998b0a 100644
--- a/app/views/admin/communication/blocks/show.html.erb
+++ b/app/views/admin/communication/blocks/show.html.erb
@@ -1,6 +1,7 @@
 <% content_for :title, @block %>
 
 <%= render 'admin/application/dependencies',
+            dependencies: @block.dependencies,
             git_dependencies: @block.template.git_dependencies,
             active_storage_blobs: @block.template.active_storage_blobs,
             expanded: true %>
diff --git a/app/views/admin/communication/websites/connections/show.html.erb b/app/views/admin/communication/websites/connections/show.html.erb
index 10be4f529..3689e8487 100644
--- a/app/views/admin/communication/websites/connections/show.html.erb
+++ b/app/views/admin/communication/websites/connections/show.html.erb
@@ -1,24 +1,8 @@
 <% content_for :title, @connection %>
 
-<div class="row">
-  <div class="col-lg-6 mb-5">
-    <%= osuny_label Communication::Website::Connection.human_attribute_name('object') %>
-    <p><%= @connection.object %></p>
-    <%= osuny_label Communication::Website::Connection.human_attribute_name('object_type') %>
-    <p><%= @connection.object_type %></p>
-    <%= osuny_label Communication::Website::Connection.human_attribute_name('object_id') %>
-    <p><%= @connection.object_id %></p>
-  </div>
-  <div class="col-lg-6 mb-5">
-    <%= osuny_label Communication::Website::Connection.human_attribute_name('source') %>
-    <p><%= @connection.source %></p>
-    <%= osuny_label Communication::Website::Connection.human_attribute_name('source_type') %>
-    <p><%= @connection.source_type %></p>
-    <%= osuny_label Communication::Website::Connection.human_attribute_name('source_id') %>
-    <p><%= @connection.source_id %></p>
-  </div>
-</div>
-
-<%= osuny_panel Communication::Website::Connection.human_attribute_name('others') do %>
-  <%= render 'admin/communication/websites/connections/list', connections: @others %>
-<% end %>
\ No newline at end of file
+<%= osuny_label Communication::Website::Connection.human_attribute_name('object') %>
+<p><%= @connection.object %></p>
+<%= osuny_label Communication::Website::Connection.human_attribute_name('object_type') %>
+<p><%= @connection.object_type %></p>
+<%= osuny_label Communication::Website::Connection.human_attribute_name('object_id') %>
+<p><%= @connection.object_id %></p>
diff --git a/app/views/admin/communication/websites/pages/show.html.erb b/app/views/admin/communication/websites/pages/show.html.erb
index d5c54efe4..3ec87ada4 100644
--- a/app/views/admin/communication/websites/pages/show.html.erb
+++ b/app/views/admin/communication/websites/pages/show.html.erb
@@ -8,6 +8,7 @@
       <%= render 'admin/communication/blocks/list', about: @page %>
       <%= render 'admin/communication/websites/pages/show/special_page', about: @page %>
       <%= render 'admin/application/dependencies',
+                  dependencies: @page.dependencies,
                   git_dependencies: @page.git_dependencies(@page.website),
                   active_storage_blobs: @page.active_storage_blobs %>
     </div>
-- 
GitLab