From e3d8c17ed9f815ffa1fa20c510f4bab06fa09b47 Mon Sep 17 00:00:00 2001
From: Olivia Simonet <olivia.simonet@mmibordeaux.com>
Date: Tue, 14 Jun 2022 11:07:56 +0200
Subject: [PATCH] Refactor block file

---
 .../communication/block/template/file.rb      |  2 ++
 .../communication/block/template/file/file.rb |  5 ++++
 .../block/template/organization_chart.rb      | 26 ++++++++++++++++++-
 .../person.rb                                 |  2 +-
 .../blocks/templates/files/_edit.html.erb     | 24 ++++-------------
 .../blocks/templates/files/_preview.html.erb  |  6 ++---
 config/locales/communication/en.yml           |  9 ++++---
 config/locales/communication/fr.yml           |  9 ++++---
 8 files changed, 53 insertions(+), 30 deletions(-)
 create mode 100644 app/models/communication/block/template/file/file.rb
 rename app/models/communication/block/template/{Organization_chart => organization_chart}/person.rb (85%)

diff --git a/app/models/communication/block/template/file.rb b/app/models/communication/block/template/file.rb
index b1172690a..f998126bc 100644
--- a/app/models/communication/block/template/file.rb
+++ b/app/models/communication/block/template/file.rb
@@ -1,4 +1,5 @@
 class Communication::Block::Template::File < Communication::Block::Template::Base
+
   def build_git_dependencies
     files.each do |file|
       add_dependency file.blob
@@ -19,4 +20,5 @@ class Communication::Block::Template::File < Communication::Block::Template::Bas
       blob: find_blob(element, 'file')
     }.to_dot
   end
+
 end
diff --git a/app/models/communication/block/template/file/file.rb b/app/models/communication/block/template/file/file.rb
new file mode 100644
index 000000000..107bbc403
--- /dev/null
+++ b/app/models/communication/block/template/file/file.rb
@@ -0,0 +1,5 @@
+class Communication::Block::Template::File::File < Communication::Block::Template::Base
+
+  has_string :title
+
+end
diff --git a/app/models/communication/block/template/organization_chart.rb b/app/models/communication/block/template/organization_chart.rb
index 2788206cd..8b06c00ad 100644
--- a/app/models/communication/block/template/organization_chart.rb
+++ b/app/models/communication/block/template/organization_chart.rb
@@ -1,5 +1,29 @@
 class Communication::Block::Template::OrganizationChart < Communication::Block::Template::Base
 
-  has_elements Communication::Block::Template::OrganizationChart::Person
+  def build_git_dependencies
+    add_dependency persons
+    persons.each do |person|
+      add_dependency person.active_storage_blobs
+    end
+  end
 
+  def persons_with_role
+    @persons_with_role ||= elements.map { |element| person_with_role(element) }
+                                   .compact
+  end
+
+  protected
+
+  def persons
+    @persons ||= persons_with_role.map { |hash| hash[:person] }
+  end
+
+  def person_with_role(element)
+    person = block.university.people.find_by id: element['id']
+    return if person.nil?
+    {
+      person: person,
+      role: element['role']
+    }.to_dot
+  end
 end
diff --git a/app/models/communication/block/template/Organization_chart/person.rb b/app/models/communication/block/template/organization_chart/person.rb
similarity index 85%
rename from app/models/communication/block/template/Organization_chart/person.rb
rename to app/models/communication/block/template/organization_chart/person.rb
index fcfdad2ae..687112b22 100644
--- a/app/models/communication/block/template/Organization_chart/person.rb
+++ b/app/models/communication/block/template/organization_chart/person.rb
@@ -1,6 +1,6 @@
 class Communication::Block::Template::OrganizationChart::Person < Communication::Block::Template::Base
 
-  has_array :person
+  has_select :person
   has_string :role
 
 end
diff --git a/app/views/admin/communication/blocks/templates/files/_edit.html.erb b/app/views/admin/communication/blocks/templates/files/_edit.html.erb
index 69cf7d16c..4153e497d 100644
--- a/app/views/admin/communication/blocks/templates/files/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/files/_edit.html.erb
@@ -1,7 +1,6 @@
-<a  class="<%= button_classes('mb-4') %>"
-    v-on:click="data.elements.push({title: '', file: {}})">
-    <%= t '.add_file' %>
-</a>
+<% element = @block.template.default_element %>
+
+<%= block_component_add_element t('.add_file') %>
 
 <draggable :list="data.elements" handle=".dragHandle" class="row">
   <div v-for="(element, index) in data.elements" class="col-md-4">
@@ -21,18 +20,8 @@
       <div class="card-body">
         <div class="d-flex">
           <div class="flex-fill">
-            <label    class="form-label"
-                      :for="'element-' + index + '-title'"><%= t '.title_label' %></label>
-            <input class="form-control mb-3"
-                      v-model="element.title"
-                      placeholder="<%= t '.title_placeholder' %>"
-                      type="text"
-                      :id="'element-' + index + '-title'" />
+            <%= block_component_edit :title, template: element %>
 
-            <label  class="form-label"
-                    :for="'element-' + index + '-file'">
-                <%= t '.file_label' %>
-            </label>
             <div v-if="element.file.id">
               <p><b>{{ element.file.filename }}</b></p>
               <a  class="btn btn-sm btn-danger mt-2"
@@ -42,10 +31,7 @@
               </a>
             </div>
             <div v-if="!element.file.id">
-              <input  class="form-control mb-4"
-                      type="file"
-                      @change="onFileImageChange( $event, element, 'file' )"
-                      :id="'element-' + index + '-file'">
+              <%= block_component_edit :file, template: element %>
             </div>
           </div>
         </div>
diff --git a/app/views/admin/communication/blocks/templates/files/_preview.html.erb b/app/views/admin/communication/blocks/templates/files/_preview.html.erb
index 48e6bad1f..51e247520 100644
--- a/app/views/admin/communication/blocks/templates/files/_preview.html.erb
+++ b/app/views/admin/communication/blocks/templates/files/_preview.html.erb
@@ -1,7 +1,7 @@
 <ul>
-    <% @block.template.files.each do |file| %>
+<% @block.template.elements.each do |element| %>
         <% file.title ||= file.blob.filename if file.blob %>
-        <% if file.title 
+        <% if file.title
             title = file.title
           elsif file.blob
             title = file.blob.filename
@@ -16,4 +16,4 @@
           <% end %>
         </li>
     <% end %>
-</ul>
\ No newline at end of file
+</ul>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index d45e57730..dd0198880 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -215,9 +215,12 @@ en:
             description: A list of downloadable files, mentioning their file size.
             edit:
               add_file: Add a file
-              file_label: File
-              title_label: Title
-              title_placeholder: Enter title here
+              element:
+                file:
+                  label: File
+                title:
+                  label: Title
+                  placeholder: Enter title here
               remove_file: Remove a file
           gallery:
             description: A gallery of images, with an accessible description, a text and a credit for each image.
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 73b7cd21f..7fd177731 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -221,9 +221,12 @@ fr:
             description: Une liste de fichiers téléchargeables, présentés avec leur poids.
             edit:
               add_file: Ajouter un fichier
-              file_label: Fichier
-              title_label: Titre
-              title_placeholder: Entrer le titre du fichier
+              element:
+                file:
+                  label: Fichier
+                title:
+                  label: Titre
+                  placeholder: Entrer le titre du fichier
               remove_file: Supprimer le fichier
           gallery:
             description: Une galerie d'images, avec pour chaque image une description accessible, un texte et un crédit.
-- 
GitLab