From af4bf828b691a484d56b6309935a6a56e6da0bd2 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Fri, 17 Jun 2022 15:28:33 +0200
Subject: [PATCH] organization chart

---
 .../communication/block/component/base.rb     |  4 +++
 .../communication/block/component/category.rb | 16 +++++++-----
 .../communication/block/component/person.rb   |  7 +++++
 .../communication/block/component/post.rb     | 16 ++++++++----
 .../communication/block/template/base.rb      |  4 +++
 .../block/template/organization_chart.rb      | 26 +------------------
 .../template/organization_chart/person.rb     |  7 +++--
 .../communication/block/template/post.rb      |  5 ++--
 .../components/category/_static.html.erb      |  1 +
 .../blocks/components/person/_edit.html.erb   | 19 ++++++++++++++
 .../components/person/_preview.html.erb       |  1 +
 .../blocks/components/person/_static.html.erb |  1 +
 .../blocks/components/post/_static.html.erb   |  1 +
 .../organization_chart/_edit.html.erb         | 16 +++---------
 .../organization_chart/_preview.html.erb      |  8 +++---
 .../organization_chart/_static.html.erb       | 10 +++----
 .../blocks/templates/posts/_static.html.erb   |  2 +-
 config/locales/communication/fr.yml           |  2 +-
 18 files changed, 81 insertions(+), 65 deletions(-)
 create mode 100644 app/models/communication/block/component/person.rb
 create mode 100644 app/views/admin/communication/blocks/components/person/_edit.html.erb
 create mode 100644 app/views/admin/communication/blocks/components/person/_preview.html.erb
 create mode 100644 app/views/admin/communication/blocks/components/person/_static.html.erb

diff --git a/app/models/communication/block/component/base.rb b/app/models/communication/block/component/base.rb
index 4c65e7f07..798e61e84 100644
--- a/app/models/communication/block/component/base.rb
+++ b/app/models/communication/block/component/base.rb
@@ -32,4 +32,8 @@ class Communication::Block::Component::Base
   def active_storage_blobs
     []
   end
+
+  def website
+    template.block.about&.website
+  end
 end
diff --git a/app/models/communication/block/component/category.rb b/app/models/communication/block/component/category.rb
index 1336a6d4c..e11cd06a5 100644
--- a/app/models/communication/block/component/category.rb
+++ b/app/models/communication/block/component/category.rb
@@ -1,12 +1,16 @@
 class Communication::Block::Component::Category < Communication::Block::Component::Base
 
   def category
-    return unless template.block.about&.website
-    template.block
-            .about
-            .website
-            .categories
-            .find_by(id: data)
+    return unless website
+    website.categories.find_by(id: data)
   end
 
+  def git_dependencies
+    active_storage_blobs +
+    [category]
+  end
+
+  def active_storage_blobs
+    category&.active_storage_blobs || []
+  end
 end
diff --git a/app/models/communication/block/component/person.rb b/app/models/communication/block/component/person.rb
new file mode 100644
index 000000000..f15ddb44e
--- /dev/null
+++ b/app/models/communication/block/component/person.rb
@@ -0,0 +1,7 @@
+class Communication::Block::Component::Person < Communication::Block::Component::Base
+
+  def person
+    template.block.university.people.find_by(id: data)
+  end
+
+end
diff --git a/app/models/communication/block/component/post.rb b/app/models/communication/block/component/post.rb
index 964d25384..0a653a7ea 100644
--- a/app/models/communication/block/component/post.rb
+++ b/app/models/communication/block/component/post.rb
@@ -1,11 +1,17 @@
 class Communication::Block::Component::Post < Communication::Block::Component::Base
 
   def post
-    return if data.blank?
-    block.about&.website
-                .posts
-                .published
-                .find_by(id: data)
+    return unless website
+    website.posts.published.find_by(id: data)
+  end
+
+  def git_dependencies
+    active_storage_blobs +
+    [post, post.author, post.author.author]
+  end
+
+  def active_storage_blobs
+    post&.author&.active_storage_blobs || []
   end
 
 end
diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb
index d830701e2..1cd430c1d 100644
--- a/app/models/communication/block/template/base.rb
+++ b/app/models/communication/block/template/base.rb
@@ -96,6 +96,7 @@ class Communication::Block::Template::Base
       elements.each do |element|
         add_dependency element.git_dependencies
       end
+      add_custom_git_dependencies
       @git_dependencies.uniq!
     end
     @git_dependencies
@@ -166,6 +167,9 @@ class Communication::Block::Template::Base
     !self.class.element_class.nil?
   end
 
+  def add_custom_git_dependencies
+  end
+
   def add_dependency(dependency)
     if dependency.is_a? Array
       @git_dependencies += dependency
diff --git a/app/models/communication/block/template/organization_chart.rb b/app/models/communication/block/template/organization_chart.rb
index 8b06c00ad..2788206cd 100644
--- a/app/models/communication/block/template/organization_chart.rb
+++ b/app/models/communication/block/template/organization_chart.rb
@@ -1,29 +1,5 @@
 class Communication::Block::Template::OrganizationChart < Communication::Block::Template::Base
 
-  def build_git_dependencies
-    add_dependency persons
-    persons.each do |person|
-      add_dependency person.active_storage_blobs
-    end
-  end
+  has_elements Communication::Block::Template::OrganizationChart::Person
 
-  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
index 687112b22..265a20f80 100644
--- a/app/models/communication/block/template/organization_chart/person.rb
+++ b/app/models/communication/block/template/organization_chart/person.rb
@@ -1,6 +1,9 @@
 class Communication::Block::Template::OrganizationChart::Person < Communication::Block::Template::Base
 
-  has_select :person
-  has_string :role
+  has_component :id, :person
+  has_component :role, :string
 
+  def person
+    id_component.person
+  end
 end
diff --git a/app/models/communication/block/template/post.rb b/app/models/communication/block/template/post.rb
index 32d5f9b96..4b916b574 100644
--- a/app/models/communication/block/template/post.rb
+++ b/app/models/communication/block/template/post.rb
@@ -5,10 +5,9 @@ class Communication::Block::Template::Post < Communication::Block::Template::Bas
   has_component :posts_quantity, :number, options: 3
   has_component :category_id, :category
 
-  def build_git_dependencies
-    add_dependency category unless category.nil?
-    add_dependency selected_posts
+  def add_custom_git_dependencies
     selected_posts.each do |post|
+      add_dependency post
       add_dependency post.active_storage_blobs
       if post.author.present?
         add_dependency [post.author, post.author.author]
diff --git a/app/views/admin/communication/blocks/components/category/_static.html.erb b/app/views/admin/communication/blocks/components/category/_static.html.erb
index e69de29bb..a871dde97 100644
--- a/app/views/admin/communication/blocks/components/category/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/category/_static.html.erb
@@ -0,0 +1 @@
+<%= indentation %><%= '- ' if list %><%= property %>: <%= value %>
diff --git a/app/views/admin/communication/blocks/components/person/_edit.html.erb b/app/views/admin/communication/blocks/components/person/_edit.html.erb
new file mode 100644
index 000000000..679a74a43
--- /dev/null
+++ b/app/views/admin/communication/blocks/components/person/_edit.html.erb
@@ -0,0 +1,19 @@
+<%
+people = current_university.people.ordered
+%>
+<label  class="form-label"
+        :for="<%= dom_id.html_safe %>">
+  <%= label %>
+</label>
+<select :id="<%= dom_id.html_safe %>"
+        class="form-select select mb-3"
+        v-model="<%= model %>.<%= property %>">
+  <% if placeholder %>
+    <option value="" disabled><%= placeholder %></option>
+  <% end %>
+  <% people.each do |person| %>
+    <option value="<%= person.id %>">
+      <%= person.last_name %>, <%= person.first_name %>
+    </option>
+  <% end %>
+</select>
diff --git a/app/views/admin/communication/blocks/components/person/_preview.html.erb b/app/views/admin/communication/blocks/components/person/_preview.html.erb
new file mode 100644
index 000000000..c6c01864d
--- /dev/null
+++ b/app/views/admin/communication/blocks/components/person/_preview.html.erb
@@ -0,0 +1 @@
+<%= component.person.to_s %>
diff --git a/app/views/admin/communication/blocks/components/person/_static.html.erb b/app/views/admin/communication/blocks/components/person/_static.html.erb
new file mode 100644
index 000000000..a871dde97
--- /dev/null
+++ b/app/views/admin/communication/blocks/components/person/_static.html.erb
@@ -0,0 +1 @@
+<%= indentation %><%= '- ' if list %><%= property %>: <%= value %>
diff --git a/app/views/admin/communication/blocks/components/post/_static.html.erb b/app/views/admin/communication/blocks/components/post/_static.html.erb
index e69de29bb..a871dde97 100644
--- a/app/views/admin/communication/blocks/components/post/_static.html.erb
+++ b/app/views/admin/communication/blocks/components/post/_static.html.erb
@@ -0,0 +1 @@
+<%= indentation %><%= '- ' if list %><%= property %>: <%= value %>
diff --git a/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb b/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb
index 92589f172..a4925d724 100644
--- a/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb
+++ b/app/views/admin/communication/blocks/templates/organization_chart/_edit.html.erb
@@ -1,4 +1,3 @@
-<% element = @block.template.default_element %>
 <%= block_component_add_element t('.add_person') %>
 
 <draggable :list="data.elements" class="list-group" handle=".dragHandle">
@@ -10,21 +9,12 @@
         </a>
       </div>
       <div class="flex-fill">
-        <div class="row">
+        <div class="row mb-n2">
           <div class="col-md-6">
-            <label  class="form-label visually-hidden"
-                    :for="'person-' + index + '-name'"><%= t '.person_label' %></label>
-            <select :id="'person-' + index + '-name'"
-                    class="form-select select"
-                    v-model="person.id">
-              <option value="" disabled><%= t '.person_placeholder' %></option>
-              <% current_university.people.ordered.each_with_index do |person, index| %>
-              <option value="<%= person.id %>"><%= person.last_name %>, <%= person.first_name %></option>
-              <% end %>
-            </select>
+            <%= block_component_edit :id, template: @element %>
           </div>
           <div class="col-md-6">
-            <%= block_component_edit  :role, template: element %>
+            <%= block_component_edit :role, template: @element %>
           </div>
         </div>
       </div>
diff --git a/app/views/admin/communication/blocks/templates/organization_chart/_preview.html.erb b/app/views/admin/communication/blocks/templates/organization_chart/_preview.html.erb
index 1ba8ba5a0..08c110324 100644
--- a/app/views/admin/communication/blocks/templates/organization_chart/_preview.html.erb
+++ b/app/views/admin/communication/blocks/templates/organization_chart/_preview.html.erb
@@ -1,5 +1,7 @@
-<% @block.template.persons_with_role.each do |person_with_role|
-      person = person_with_role.person %>
+<% @block.template.elements.each do |element|
+      person = element.person
+      next if person.nil?
+      %>
   <article class="card">
     <div class="card-body">
       <% if person.best_picture.attached? %>
@@ -9,7 +11,7 @@
         </div>
       <% end %>
       <h3 class="card-title h5"><%= link_to person, [:admin, person] %></h3>
-      <p class="mb-0"><%= person_with_role.role %></p>
+      <p class="mb-0"><%= element.role %></p>
     </div>
   </article>
 <% end %>
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 95c784ff6..6ed1e765f 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,7 +1,5 @@
-<%
-block.template.persons_with_role.each do |person_with_role|
-  person = person_with_role.person %>
-      - slug: "<%= person.slug %>"
-        role: >-
-          <%= prepare_text_for_static person_with_role.role, 5 %>
+<% block.template.elements.each do |element| %>
+<% next unless element.person %>
+<%= block_component_static :id, template: element, depth: 4, list: true %>
+<%= block_component_static :role, template: element, depth: 5 %>
 <% end %>
diff --git a/app/views/admin/communication/blocks/templates/posts/_static.html.erb b/app/views/admin/communication/blocks/templates/posts/_static.html.erb
index 752302945..69055c198 100644
--- a/app/views/admin/communication/blocks/templates/posts/_static.html.erb
+++ b/app/views/admin/communication/blocks/templates/posts/_static.html.erb
@@ -3,5 +3,5 @@
 <% end %>
       posts:
 <% block.template.selected_posts.each do |post| %>
-        - <%= post.static_path %>
+        - "<%= post.static_path %>"
 <% end %>
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 30132f5fb..30aa26e4f 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -276,7 +276,7 @@ fr:
             edit:
               add_person: Ajouter une personne
               element:
-                person:
+                id:
                   label: Personne
                   placeholder: Choisir la personne
                 role:
-- 
GitLab