From fb151122389b2cb2ba45b0c0ce6c9ceb8f598a51 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 8 Jun 2022 08:05:49 +0200
Subject: [PATCH] wip

---
 app/helpers/admin/application_helper.rb            |  2 +-
 app/models/communication/block/component/image.rb  |  7 +------
 app/models/communication/block/component/layout.rb |  3 +++
 app/models/communication/block/template/base.rb    |  1 +
 app/models/communication/block/template/chapter.rb |  4 +---
 app/models/communication/block/template/gallery.rb | 14 --------------
 .../blocks/components/image/_static.html.erb       |  5 +++--
 .../components/{layouts => layout}/_edit.html.erb  |  0
 .../blocks/components/layout/_static.html.erb      |  1 +
 .../blocks/components/rich_text/_static.html.erb   |  5 +++--
 .../blocks/components/string/_static.html.erb      |  7 ++++---
 .../blocks/components/text/_static.html.erb        |  7 ++++---
 .../blocks/templates/gallery/_edit.html.erb        |  3 +--
 .../blocks/templates/gallery/_static.html.erb      |  8 +++++++-
 14 files changed, 30 insertions(+), 37 deletions(-)
 create mode 100644 app/models/communication/block/component/layout.rb
 rename app/views/admin/communication/blocks/components/{layouts => layout}/_edit.html.erb (100%)
 create mode 100644 app/views/admin/communication/blocks/components/layout/_static.html.erb

diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb
index edad92b43..8a02cf60f 100644
--- a/app/helpers/admin/application_helper.rb
+++ b/app/helpers/admin/application_helper.rb
@@ -74,7 +74,7 @@ module Admin::ApplicationHelper
   end
 
   def prepare_text_for_static(text, depth = 1)
-    indentation = '  ' * depth # Beware, it works only at level one, with 2 spaces
+    indentation = '  ' * depth
     text = text.to_s.dup
     text = strip_tags text
     text = text.strip
diff --git a/app/models/communication/block/component/image.rb b/app/models/communication/block/component/image.rb
index 2976c5af0..12a1fd7e4 100644
--- a/app/models/communication/block/component/image.rb
+++ b/app/models/communication/block/component/image.rb
@@ -1,12 +1,7 @@
 class Communication::Block::Component::Image < Communication::Block::Component::Base
 
-  # def data
-  #   # Loading the blob when needed, not saved in the database
-  #   @data['blob'] = template.blob_with_id @data['id'] if @data&.has_key? 'id'
-  #   @data
-  # end
-
   def blob
+    return if data['id'].blank?
     @blob ||= template.blob_with_id data['id']
   end
 
diff --git a/app/models/communication/block/component/layout.rb b/app/models/communication/block/component/layout.rb
new file mode 100644
index 000000000..6709b568c
--- /dev/null
+++ b/app/models/communication/block/component/layout.rb
@@ -0,0 +1,3 @@
+class Communication::Block::Component::Layout < Communication::Block::Component::Base
+
+end
diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb
index 7723cc8ad..72d295668 100644
--- a/app/models/communication/block/template/base.rb
+++ b/app/models/communication/block/template/base.rb
@@ -27,6 +27,7 @@ class Communication::Block::Template::Base
 
   def self.has_layouts(list)
     self.layouts = list
+    has_component :layout, :layout
   end
 
   def self.has_elements(element_class)
diff --git a/app/models/communication/block/template/chapter.rb b/app/models/communication/block/template/chapter.rb
index e0d9a26b1..688a91de7 100644
--- a/app/models/communication/block/template/chapter.rb
+++ b/app/models/communication/block/template/chapter.rb
@@ -1,11 +1,9 @@
 class Communication::Block::Template::Chapter < Communication::Block::Template::Base
+
   has_rich_text :text
   has_rich_text :notes
   has_image :image
   has_string :image_alt
   has_rich_text :image_credit
 
-  def build_git_dependencies
-    add_dependency image&.blob
-  end
 end
diff --git a/app/models/communication/block/template/gallery.rb b/app/models/communication/block/template/gallery.rb
index bf621dcf9..0fef23ebf 100644
--- a/app/models/communication/block/template/gallery.rb
+++ b/app/models/communication/block/template/gallery.rb
@@ -3,18 +3,4 @@ class Communication::Block::Template::Gallery < Communication::Block::Template::
   has_layouts [:grid, :carousel]
   has_elements Communication::Block::Template::Gallery::Image
 
-  def build_git_dependencies
-    add_dependency active_storage_blobs
-  end
-
-  # def images_with_alt
-  #   @images_with_alt ||= elements.map { |element|
-  #     extract_image_alt_and_credit element, 'file'
-  #   }.compact
-  # end
-
-  # def active_storage_blobs
-  #   @active_storage_blobs ||=  images_with_alt.map { |hash| hash.blob }
-  #                                             .compact
-  # end
 end
diff --git a/app/views/admin/communication/blocks/components/image/_static.html.erb b/app/views/admin/communication/blocks/components/image/_static.html.erb
index 8d2f396a9..af09d1a54 100644
--- a/app/views/admin/communication/blocks/components/image/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/image/_static.html.erb
@@ -1,10 +1,11 @@
 <%
 component = template.public_send "#{property}_component"
 blob = component.blob
-indentation = '      '
+depth ||= 3
+indentation = '  ' * depth
 %>
 <% if blob %>
-<%= indentation %>image:
+<%= indentation %><%= property %>:
 <%= indentation %>  id: "<%= blob.id %>"
 <%= indentation %>  file: "<%= blob.id %>"
 <% end %>
diff --git a/app/views/admin/communication/blocks/components/layouts/_edit.html.erb b/app/views/admin/communication/blocks/components/layout/_edit.html.erb
similarity index 100%
rename from app/views/admin/communication/blocks/components/layouts/_edit.html.erb
rename to app/views/admin/communication/blocks/components/layout/_edit.html.erb
diff --git a/app/views/admin/communication/blocks/components/layout/_static.html.erb b/app/views/admin/communication/blocks/components/layout/_static.html.erb
new file mode 100644
index 000000000..21c135f27
--- /dev/null
+++ b/app/views/admin/communication/blocks/components/layout/_static.html.erb
@@ -0,0 +1 @@
+      layout: <%= @block.template.layout %>
diff --git a/app/views/admin/communication/blocks/components/rich_text/_static.html.erb b/app/views/admin/communication/blocks/components/rich_text/_static.html.erb
index baf754588..7c487d790 100644
--- a/app/views/admin/communication/blocks/components/rich_text/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/rich_text/_static.html.erb
@@ -1,6 +1,7 @@
 <%
-value = @block.template.public_send property
-indentation = '      '
+value = template.public_send property
+depth ||= 3
+indentation = '  ' * depth
 %>
 <%= indentation %><%= property %>: >-
 <%= indentation %>  <%= prepare_html_for_static value, @university %>
diff --git a/app/views/admin/communication/blocks/components/string/_static.html.erb b/app/views/admin/communication/blocks/components/string/_static.html.erb
index 40e2b9d7e..72a8e7fc8 100644
--- a/app/views/admin/communication/blocks/components/string/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/string/_static.html.erb
@@ -1,6 +1,7 @@
 <%
-indentation = '      '
-value = @block.template.public_send property
+value = template.public_send property
+depth ||= 3
+indentation = '  ' * depth
 %>
 <%= indentation %><%= property %>: >-
-<%= indentation %>  <%= prepare_text_for_static value, 4 %>
+<%= indentation %>  <%= prepare_text_for_static value, depth + 1 %>
diff --git a/app/views/admin/communication/blocks/components/text/_static.html.erb b/app/views/admin/communication/blocks/components/text/_static.html.erb
index 40e2b9d7e..72a8e7fc8 100644
--- a/app/views/admin/communication/blocks/components/text/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/text/_static.html.erb
@@ -1,6 +1,7 @@
 <%
-indentation = '      '
-value = @block.template.public_send property
+value = template.public_send property
+depth ||= 3
+indentation = '  ' * depth
 %>
 <%= indentation %><%= property %>: >-
-<%= indentation %>  <%= prepare_text_for_static value, 4 %>
+<%= indentation %>  <%= prepare_text_for_static value, depth + 1 %>
diff --git a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
index 8ae23ce25..97fd09e1b 100644
--- a/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/gallery/_edit.html.erb
@@ -1,8 +1,7 @@
 <%
 element = @block.template.default_element
 %>
-<%= render 'admin/communication/blocks/components/layouts/edit',
-            horizontal: true %>
+<%= block_component_edit :layout, horizontal: true %>
 
 <%= render  'admin/communication/blocks/components/add_element/edit',
             label: t('.add_image') %>
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 0c2839e58..06a195a30 100644
--- a/app/views/admin/communication/blocks/templates/gallery/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/gallery/_static.html.erb
@@ -1,6 +1,12 @@
-      layout: <%= block.template.layout %>
+<%= block_component_static :layout %>
       images:
 <% block.template.elements.each do |element| %>
+<% next unless element.blob %>
+        -
+<%= block_component_static :image, template: element, depth: 5 %>
+<%= block_component_static :alt, template: element, depth: 6 %>
+<%= block_component_static :credit, template: element, depth: 6 %>
+<%= block_component_static :text, template: element, depth: 6 %>
         - id: "<%= element.blob.id %>"
           file: "<%= element.blob.id %>"
           alt: >
-- 
GitLab