From ac4a8151791988bdbc96c0b6ac78fdf165d05610 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sat, 12 Feb 2022 08:37:46 +0100
Subject: [PATCH] better shows

---
 .../property/_summernote_embeds.html.erb      | 27 +++++++
 .../admin/application/property/_text.html.erb | 23 ++++++
 .../communication/website/pages/show.html.erb |  8 +-
 .../communication/website/posts/show.html.erb | 39 +---------
 .../admin/education/programs/show.html.erb    | 76 ++++++-------------
 5 files changed, 81 insertions(+), 92 deletions(-)
 create mode 100644 app/views/admin/application/property/_summernote_embeds.html.erb
 create mode 100644 app/views/admin/application/property/_text.html.erb

diff --git a/app/views/admin/application/property/_summernote_embeds.html.erb b/app/views/admin/application/property/_summernote_embeds.html.erb
new file mode 100644
index 000000000..4953d5ce5
--- /dev/null
+++ b/app/views/admin/application/property/_summernote_embeds.html.erb
@@ -0,0 +1,27 @@
+<h3 class="h5"><%= object.class.human_attribute_name(property) %></h3>
+<% if object.summernote_embeds.any? %>
+  <% object.summernote_embeds.each do |embed| %>
+    <div class="row mb-3">
+      <div class="col-md-2">
+        <div class="img-thumbnail text-center">
+          <% if embed.variable? %>
+            <%= kamifusen_tag embed, class: 'img-fluid' %>
+          <% else %>
+            <i class="fas fa-file p-3 fa-2x"></i>
+          <% end %>
+        </div>
+      </div>
+      <div class="col-md-10">
+        <p class="mb-1">
+          <%= embed.filename %>
+          <%= number_to_human_size embed.metadata['filesize'] %>
+        </p>
+        <%= link_to t('download'),
+                    embed.blob.url(disposition: 'attachment', filename: embed.filename),
+                    class: 'btn btn-xs btn-light' %>
+      </div>
+    </div>
+  <% end %>
+<% else %>
+  <p>0</p>
+<% end %>
diff --git a/app/views/admin/application/property/_text.html.erb b/app/views/admin/application/property/_text.html.erb
new file mode 100644
index 000000000..6b31f7e83
--- /dev/null
+++ b/app/views/admin/application/property/_text.html.erb
@@ -0,0 +1,23 @@
+<%
+value = object.send(property).to_s
+if object.respond_to? "best_#{property}"
+  value = object.public_send("best_#{property}").to_s
+  source = object.public_send("best_#{property}_source")
+end
+%>
+<h3 class="h5">
+  <%= object.class.human_attribute_name(property) %>
+  <% if source %>
+    <span class="small text-muted">
+      <%= t 'admin.inheritance.sentence_html',
+            link: link_to(source, [:admin, source]) %>
+    </span>
+  <% end %>
+</h3>
+<p>
+  <% if value.blank? %>
+    <i class="fa fa-exclamation-circle text-danger"></i>
+  <% else %>
+    <%= strip_tags(value).truncate 200 %>
+  <% end %>
+</p>
diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb
index 898684ec0..2f584359b 100644
--- a/app/views/admin/communication/website/pages/show.html.erb
+++ b/app/views/admin/communication/website/pages/show.html.erb
@@ -9,11 +9,9 @@
           <h2 class="card-title mb-0 h5"><%= t('content') %></h2>
         </div>
         <div class="card-body">
-          <h3 class="h5"><%= Communication::Website::Page.human_attribute_name('description') %></h3>
-          <%= sanitize @page.description %>
-
-          <h3 class="h5 mt-4"><%= Communication::Website::Page.human_attribute_name('text') %></h3>
-          <%= @page.text %>
+          <%= render 'admin/application/property/text', object: @page, property: :description %>
+          <%= render 'admin/application/property/text', object: @page, property: :text %>
+          <%= render 'admin/application/property/summernote_embeds', object: @page, property: :medias %>
         </div>
       </div>
     </div>
diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb
index 18f782eb9..e2fb1cdbf 100644
--- a/app/views/admin/communication/website/posts/show.html.erb
+++ b/app/views/admin/communication/website/posts/show.html.erb
@@ -8,43 +8,10 @@
           <h2 class="card-title mb-0 h5"><%= t('content') %></h2>
         </div>
         <div class="card-body">
-          <h3 class="h5"><%= Communication::Website::Post.human_attribute_name('description') %></h3>
-          <% if @post.description.blank? %>
-            <i class="fa fa-exclamation-circle text-danger"></i>
-          <% else %>
-            <%= sanitize @post.description %>
-          <% end %>
-
-          <h3 class="h5 mt-4"><%= Communication::Website::Post.human_attribute_name('text') %></h3>
-          <%= strip_tags(@post.text.to_s).truncate 300 %>
-
-          <h3 class="h5 mt-4"><%= Communication::Website::Post.human_attribute_name('medias') %></h3>
-
-          <% @post.summernote_embeds.each do |embed| %>
-            <div class="row mb-3">
-              <div class="col-md-2">
-                <div class="img-thumbnail text-center">
-                  <% if embed.variable? %>
-                    <%= kamifusen_tag embed, class: 'img-fluid' %>
-                  <% else %>
-                    <i class="fas fa-file p-3 fa-2x"></i>
-                  <% end %>
-                </div>
-              </div>
-              <div class="col-md-10">
-                <p class="mb-1">
-                  <%= embed.filename %>
-                  <%= number_to_human_size embed.metadata['filesize'] %>
-                </p>
-                <%= link_to t('download'),
-                            embed.blob.url(disposition: 'attachment', filename: embed.filename),
-                            class: 'btn btn-xs btn-light' %>
-              </div>
-            </div>
-          <% end %>
-
+          <%= render 'admin/application/property/text', object: @post, property: :description %>
+          <%= render 'admin/application/property/text', object: @post, property: :text %>
+          <%= render 'admin/application/property/summernote_embeds', object: @post, property: :medias %>
           <hr>
-
           <pre><%= @post.text_new.to_html %></pre>
         </div>
       </div>
diff --git a/app/views/admin/education/programs/show.html.erb b/app/views/admin/education/programs/show.html.erb
index d6ef2d1ab..09f89481c 100644
--- a/app/views/admin/education/programs/show.html.erb
+++ b/app/views/admin/education/programs/show.html.erb
@@ -2,7 +2,7 @@
 
 <div class="row">
 
-  <div class="col-md-4">
+  <div class="col-md-3">
     <div class="card flex-fill w-100">
       <div class="card-header">
         <h2 class="card-title mb-0 h5"><%= t('education.program.main_informations') %></h2>
@@ -39,15 +39,13 @@
       </div>
     <% end %>
   </div>
-  <div class="col-md-8">
+  <div class="col-md-4">
     <div class="card flex-fill w-100">
       <div class="card-header">
         <h5 class="card-title mb-0"><%= t('education.program.useful_informations') %></h5>
       </div>
       <div class="card-body">
-        <h3 class="h5 mt-4">
-          <%= Education::Program.human_attribute_name('description') %>
-        </h3>
+        <%= render 'admin/application/property/text', object: @program, property: :description %>
         <p><%= @program.description %></p>
         <% [
               :registration,
@@ -57,58 +55,34 @@
               :accessibility,
               :other
             ].each_with_index do |prop, index| %>
-          <%
-          best_prop_value = @program.public_send("best_#{prop}")
-          best_prop_source = @program.public_send("best_#{prop}_source")
-          %>
-          <% next if best_prop_value.blank? # No value at all%>
-          <h3 class="h5 mt-4">
-            <%= Education::Program.human_attribute_name(prop) %>
-            <% if best_prop_source %>
-              <span class="small text-muted">
-                <%= t 'admin.inheritance.sentence_html', link: link_to(best_prop_source, [:admin, best_prop_source]) %>
-              </span>
-            <% end %>
-          </h3>
-          <%= best_prop_value %>
+            <%= render 'admin/application/property/text', object: @program, property: prop %>
         <% end %>
       </div>
     </div>
   </div>
-</div>
+  <div class="col-md-5">
 
-<div class="card flex-fill w-100">
-  <div class="card-header">
-    <h5 class="card-title mb-0"><%= t('education.program.educational_informations') %></h5>
-  </div>
-  <div class="card-body">
-    <div class="row">
-      <div class="col-md-6">
-        <% i = 0 %>
-        <%  [
-              :objectives,
-              :content,
-              :prerequisites
-            ].each do |prop| %>
-          <% next if @program.public_send(prop).blank? %>
-          <h3 class="h5 <%= 'mt-4' if i > 0 %>"><%= Education::Program.human_attribute_name(prop) %></h3>
-          <%= @program.public_send prop %>
-          <% i += 1 %>
-        <% end %>
+    <div class="card flex-fill w-100">
+      <div class="card-header">
+        <h5 class="card-title mb-0"><%= t('education.program.educational_informations') %></h5>
       </div>
-      <div class="col-md-6">
-        <% i = 0 %>
-        <%  [
-              :pedagogy,
-              :evaluation,
-              :opportunities,
-              :results
-            ].each do |prop| %>
-          <% next if @program.public_send(prop).blank? %>
-          <h3 class="h5 <%= 'mt-4' if i > 0 %>"><%= Education::Program.human_attribute_name(prop) %></h3>
-          <%= @program.public_send prop %>
-          <% i += 1 %>
-        <% end %>
+      <div class="card-body">
+        <div class="row">
+          <div class="col-md-6">
+            <% i = 0 %>
+            <%  [
+                  :objectives,
+                  :content,
+                  :prerequisites,
+                  :pedagogy,
+                  :evaluation,
+                  :opportunities,
+                  :results
+                ].each do |prop| %>
+              <%= render 'admin/application/property/text', object: @program, property: prop %>
+            <% end %>
+          </div>
+        </div>
       </div>
     </div>
   </div>
-- 
GitLab