From f97bc7dd15ecd7bef4121ca6a7cd545c19019674 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 25 Feb 2022 22:53:49 +0100
Subject: [PATCH] fix uninitialized blocks

---
 app/models/communication/block/template.rb    |  2 +-
 .../communication/blocks/_static.html.erb     |  3 +-
 .../blocks/templates/gallery/_show.html.erb   | 32 ++++++++++---------
 .../blocks/templates/gallery/_static.html.erb |  2 +-
 .../organization_chart/_show.html.erb         |  2 +-
 .../organization_chart/_static.html.erb       |  2 +-
 .../blocks/templates/partners/_show.html.erb  |  2 +-
 .../templates/partners/_static.html.erb       |  2 +-
 8 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/app/models/communication/block/template.rb b/app/models/communication/block/template.rb
index 91e27ae02..a8653c9df 100644
--- a/app/models/communication/block/template.rb
+++ b/app/models/communication/block/template.rb
@@ -12,7 +12,7 @@ class Communication::Block::Template
   protected
 
   def data
-    block.data
+    block.data || {}
   end
 
   def university
diff --git a/app/views/admin/communication/blocks/_static.html.erb b/app/views/admin/communication/blocks/_static.html.erb
index 7f8e7a416..da263b2fe 100644
--- a/app/views/admin/communication/blocks/_static.html.erb
+++ b/app/views/admin/communication/blocks/_static.html.erb
@@ -4,5 +4,6 @@ blocks:
   - template: <%= block.template %>
     position: <%= block.position %>
     data:
-<%= render "admin/communication/blocks/templates/#{block.template}/static", block: block if block.data %><% end %>
+<%= render "admin/communication/blocks/templates/#{block.template}/static",
+            block: block if block.data && block.data.present? %><% end %>
 <% end %>
diff --git a/app/views/admin/communication/blocks/templates/gallery/_show.html.erb b/app/views/admin/communication/blocks/templates/gallery/_show.html.erb
index 6043b61ce..420ed90b3 100644
--- a/app/views/admin/communication/blocks/templates/gallery/_show.html.erb
+++ b/app/views/admin/communication/blocks/templates/gallery/_show.html.erb
@@ -1,16 +1,18 @@
-<h2><%= @block.data['title'] %></h2>
-<div class="row">
-<% @block.data['elements'].each do |image| %>
-    <div class="col-xxl-2 col-xl-3 col-6">
-      <article class="card">
-        <% unless image.dig('file', 'id').blank? %>
-          <% blob = ActiveStorage::Blob.find(image.dig('file', 'id')) %>
-          <%= kamifusen_tag blob, width: 500, class: 'img-fluid mb-2' %>
-        <% end %>
-        <div class="card-body">
-          <p class="mb-0"><%= image['alt'] %></p>
-        </div>
-      </article>
-    </div>
+<% if @block.data %>
+  <h2><%= @block.data['title'] %></h2>
+  <div class="row">
+  <% @block.data['elements'].each do |image| %>
+      <div class="col-xxl-2 col-xl-3 col-6">
+        <article class="card">
+          <% unless image.dig('file', 'id').blank? %>
+            <% blob = ActiveStorage::Blob.find(image.dig('file', 'id')) %>
+            <%= kamifusen_tag blob, width: 500, class: 'img-fluid mb-2' %>
+          <% end %>
+          <div class="card-body">
+            <p class="mb-0"><%= image['alt'] %></p>
+          </div>
+        </article>
+      </div>
+  <% end %>
+  </div>
 <% end %>
-</div>
diff --git a/app/views/admin/communication/blocks/templates/gallery/_static.html.erb b/app/views/admin/communication/blocks/templates/gallery/_static.html.erb
index d9b7f93e2..49ebec44b 100644
--- a/app/views/admin/communication/blocks/templates/gallery/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/gallery/_static.html.erb
@@ -1,7 +1,7 @@
-<% if !block.data.nil? && !block.data.is_a?(String) && block.data.has_key?('elements') %>
       title: >
           <%= prepare_text_for_static block.data['title'] %>
       images:
+<% if block.data.has_key?('elements') %>
 <% block.data['elements'].each do |image|
   file_id = nil
   if image.has_key?('file')
diff --git a/app/views/admin/communication/blocks/templates/organization_chart/_show.html.erb b/app/views/admin/communication/blocks/templates/organization_chart/_show.html.erb
index cdee4815f..b2fe5da06 100644
--- a/app/views/admin/communication/blocks/templates/organization_chart/_show.html.erb
+++ b/app/views/admin/communication/blocks/templates/organization_chart/_show.html.erb
@@ -16,4 +16,4 @@
       </div>
     <% end %>
   </div>
-<% end %>
+<% end if @block.data %>
diff --git a/app/views/admin/communication/blocks/templates/organization_chart/_static.html.erb b/app/views/admin/communication/blocks/templates/organization_chart/_static.html.erb
index d9a5b0442..f2694c15f 100644
--- a/app/views/admin/communication/blocks/templates/organization_chart/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/organization_chart/_static.html.erb
@@ -1,4 +1,4 @@
-<% if !block.data.nil? && !block.data.is_a?(String) && block.data.has_key?('elements') %>
+<% if block.data.has_key?('elements') %>
 <% block.data['elements'].each do |element| %>
       - title: >
           <%= prepare_text_for_static element['title'] %>
diff --git a/app/views/admin/communication/blocks/templates/partners/_show.html.erb b/app/views/admin/communication/blocks/templates/partners/_show.html.erb
index 22a91333e..463abb31d 100644
--- a/app/views/admin/communication/blocks/templates/partners/_show.html.erb
+++ b/app/views/admin/communication/blocks/templates/partners/_show.html.erb
@@ -16,4 +16,4 @@
       </div>
     <% end %>
   </div>
-<% end %>
+<% end if @block.data %>
diff --git a/app/views/admin/communication/blocks/templates/partners/_static.html.erb b/app/views/admin/communication/blocks/templates/partners/_static.html.erb
index fd0bf2c78..90043893c 100644
--- a/app/views/admin/communication/blocks/templates/partners/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/partners/_static.html.erb
@@ -1,4 +1,4 @@
-<% if !block.data.nil? && !block.data.is_a?(String) && block.data.has_key?('elements') %>
+<% if block.data.has_key?('elements') %>
 <% block.data['elements'].each do |element| %>
       - title: >
           <%= element['title'] %>
-- 
GitLab