From a738379f7d148c78a9f39a188ac37d72e1103ddb Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 29 Jun 2022 19:37:09 +0200
Subject: [PATCH] dependencies

---
 app/models/communication/website/page.rb      |  2 +-
 .../admin/application/_dependencies.html.erb  | 58 +++++++++++++++++++
 .../admin/communication/blocks/show.html.erb  | 41 ++-----------
 .../websites/pages/show.html.erb              |  3 +
 4 files changed, 66 insertions(+), 38 deletions(-)
 create mode 100644 app/views/admin/application/_dependencies.html.erb

diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index badd0b135..ad4e22bd9 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -126,7 +126,7 @@ class Communication::Website::Page < ApplicationRecord
     dependencies += website.education_programs if kind_education_programs?
     dependencies += [parent] if has_parent?
     dependencies += [website.config_permalinks] if is_special_page?
-    dependencies
+    dependencies.flatten
   end
 
   def git_destroy_dependencies(website)
diff --git a/app/views/admin/application/_dependencies.html.erb b/app/views/admin/application/_dependencies.html.erb
new file mode 100644
index 000000000..2247b2c39
--- /dev/null
+++ b/app/views/admin/application/_dependencies.html.erb
@@ -0,0 +1,58 @@
+<%
+expanded ||= false
+%>
+<% if current_user.server_admin? %>
+    <div class="card flex-fill w-100">
+        <div class="card-header">
+            <div class="card-actions float-end">
+            <button class="accordion-button bg-transparent <%= 'collapsed' unless expanded %> mt-n1"
+                    type="button"
+                    data-bs-toggle="collapse"
+                    data-bs-target="#dependencies"
+                    aria-expanded="<%= expanded %>"
+                    aria-controls="dependencies">
+            </button>
+            </div>
+            <h2 class="card-title mb-0 h5">Dependencies</h2>
+        </div>
+        <div class="card-body collapse <%= 'show' if expanded %>" id="dependencies">
+            <h2 class="h5">git_dependencies</h2>
+            <table class="table">
+                <thead>
+                    <tr>
+                        <th>Id</th>
+                        <th>Class</th>
+                        <th>Name</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <% git_dependencies.each do |dependency| %>
+                        <tr>
+                            <td><%= dependency.id %></td>
+                            <td><%= dependency.class %></td>
+                            <td><%= dependency.to_s %></td>
+                        </tr>
+                    <% end %>
+                </tbody>
+            </table>
+
+            <h2 class="h5 mt-5">active_storage_blobs</h2>
+            <table class="table">
+                <thead>
+                    <tr>
+                        <th>Id</th>
+                        <th>Filename</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <% active_storage_blobs.each do |blob| %>
+                        <tr>
+                            <td><%= blob.id %></td>
+                            <td><%= blob.filename %></td>
+                        </tr>
+                    <% end %>
+                </tbody>
+            </table>
+        </div>
+    </div>
+<% end %>
\ No newline at end of file
diff --git a/app/views/admin/communication/blocks/show.html.erb b/app/views/admin/communication/blocks/show.html.erb
index 6b358527c..42dcbe027 100644
--- a/app/views/admin/communication/blocks/show.html.erb
+++ b/app/views/admin/communication/blocks/show.html.erb
@@ -1,42 +1,9 @@
 <% content_for :title, @block %>
 
-<h2>git_dependencies</h2>
-<table class="table">
-    <thead>
-        <tr>
-            <th>Id</th>
-            <th>Class</th>
-            <th>Name</th>
-        </tr>
-    </thead>
-    <tbody>
-        <% @block.template.git_dependencies.each do |dependency| %>
-            <tr>
-                <td><%= dependency.id %></td>
-                <td><%= dependency.class %></td>
-                <td><%= dependency.to_s %></td>
-            </tr>
-        <% end %>
-    </tbody>
-</table>
-
-<h2>active_storage_blobs</h2>
-<table class="table">
-    <thead>
-        <tr>
-            <th>Id</th>
-            <th>Filename</th>
-        </tr>
-    </thead>
-    <tbody>
-        <% @block.template.active_storage_blobs.each do |blob| %>
-            <tr>
-                <td><%= blob.id %></td>
-                <td><%= blob.filename %></td>
-            </tr>
-        <% end %>
-    </tbody>
-</table>
+<%= render 'admin/application/dependencies',
+            git_dependencies: @block.template.git_dependencies,
+            active_storage_blobs: @block.template.active_storage_blobs,
+            expanded: true %>
 
 <% content_for :action_bar_right do %>
     <%= edit_link @block %>
diff --git a/app/views/admin/communication/websites/pages/show.html.erb b/app/views/admin/communication/websites/pages/show.html.erb
index fb7289a8a..bad7daf9c 100644
--- a/app/views/admin/communication/websites/pages/show.html.erb
+++ b/app/views/admin/communication/websites/pages/show.html.erb
@@ -28,6 +28,9 @@
         </div>
       </div>
       <%= render 'admin/communication/blocks/list', about: @page %>
+      <%= render 'admin/application/dependencies',
+                  git_dependencies: @page.git_dependencies(@page.website),
+                  active_storage_blobs: @page.active_storage_blobs %>
     </div>
     <div class="col-md-4">
       <%= render 'admin/application/a11y/widget', about: @page %>
-- 
GitLab