From 445991d80bc1db04379da24ce37a5e3b7c03770d Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 22 Mar 2023 11:45:13 +0100
Subject: [PATCH] wip

---
 .../admin/communication/blocks_controller.rb  |  2 +
 app/models/communication/block.rb             |  2 +-
 .../communication/block/component/base.rb     |  2 +-
 app/models/communication/block/heading.rb     |  3 +-
 .../communication/block/template/base.rb      |  2 +-
 app/models/communication/website/page.rb      |  2 +-
 app/models/communication/website/post.rb      |  5 ++
 .../{accessible.rb => with_accessibility.rb}  |  2 +-
 .../admin/application/a11y/_widget.html.erb   | 16 +++--
 .../communication/blocks/_block.html.erb      |  2 +-
 .../communication/blocks/_index_edit.html.erb | 38 ++++++-----
 .../blocks/headings/_form.html.erb            | 14 ++--
 .../blocks/headings/_heading_static.html.erb  |  4 ++
 .../websites/menus/show.html.erb              | 23 ++-----
 .../websites/posts/show.html.erb              | 64 +++++++------------
 .../websites/posts/show/_metadata.html.erb    | 15 +++++
 16 files changed, 99 insertions(+), 97 deletions(-)
 rename app/models/concerns/{accessible.rb => with_accessibility.rb} (97%)
 create mode 100644 app/views/admin/communication/websites/posts/show/_metadata.html.erb

diff --git a/app/controllers/admin/communication/blocks_controller.rb b/app/controllers/admin/communication/blocks_controller.rb
index 511a159e2..3356a1afc 100644
--- a/app/controllers/admin/communication/blocks_controller.rb
+++ b/app/controllers/admin/communication/blocks_controller.rb
@@ -42,6 +42,8 @@ class Admin::Communication::BlocksController < Admin::Communication::Application
 
   def create
     @block.university = current_university
+    headings = @block.about.headings
+    @block.heading = headings.last if headings.any?
     if @block.save
       # No need to sync as content is empty
       redirect_to [:edit, :admin, @block],
diff --git a/app/models/communication/block.rb b/app/models/communication/block.rb
index 04ab7f2db..8711957ea 100644
--- a/app/models/communication/block.rb
+++ b/app/models/communication/block.rb
@@ -27,8 +27,8 @@
 #  fk_rails_90ac986fab  (heading_id => communication_block_headings.id)
 #
 class Communication::Block < ApplicationRecord
-  include Accessible
   include Sanitizable
+  include WithAccessibility
   include WithUniversity
   include WithPosition
 
diff --git a/app/models/communication/block/component/base.rb b/app/models/communication/block/component/base.rb
index b0e3accc8..ebb20af14 100644
--- a/app/models/communication/block/component/base.rb
+++ b/app/models/communication/block/component/base.rb
@@ -1,5 +1,5 @@
 class Communication::Block::Component::Base
-  include Accessible
+  include WithAccessibility
 
   attr_reader :property, :template
 
diff --git a/app/models/communication/block/heading.rb b/app/models/communication/block/heading.rb
index b15589e15..90d5e37b4 100644
--- a/app/models/communication/block/heading.rb
+++ b/app/models/communication/block/heading.rb
@@ -41,7 +41,8 @@ class Communication::Block::Heading < ApplicationRecord
   DEFAULT_LEVEL = 2
   
   scope :root, -> { where(level: DEFAULT_LEVEL) }
-  default_scope { order(:position) }
+  scope :ordered, -> { order(:position) }
+  default_scope { ordered }
 
   before_validation :compute_level
 
diff --git a/app/models/communication/block/template/base.rb b/app/models/communication/block/template/base.rb
index fbc8a7f2a..390127573 100644
--- a/app/models/communication/block/template/base.rb
+++ b/app/models/communication/block/template/base.rb
@@ -1,5 +1,5 @@
 class Communication::Block::Template::Base
-  include Accessible
+  include WithAccessibility
 
   class_attribute :components_descriptions,
                   :layouts,
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index da268152d..c2a1c3d50 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -46,8 +46,8 @@
 class Communication::Website::Page < ApplicationRecord
   self.ignored_columns = %w(path)
 
-  include Accessible
   include Sanitizable
+  include WithAccessibility
   include WithBlobs
   include WithBlocks
   include WithDuplication
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 5264c87a5..e4c7fafea 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -39,6 +39,7 @@
 #
 class Communication::Website::Post < ApplicationRecord
   include Sanitizable
+  include WithAccessibility
   include WithBlobs
   include WithBlocks
   include WithDuplication
@@ -156,6 +157,10 @@ class Communication::Website::Post < ApplicationRecord
 
   protected
 
+  def check_accessibility
+    accessibility_merge_array blocks
+  end
+
   def slug_unavailable?(slug)
     self.class.unscoped
               .where(communication_website_id: self.communication_website_id, language_id: language_id, slug: slug)
diff --git a/app/models/concerns/accessible.rb b/app/models/concerns/with_accessibility.rb
similarity index 97%
rename from app/models/concerns/accessible.rb
rename to app/models/concerns/with_accessibility.rb
index de07704aa..0e24f40f3 100644
--- a/app/models/concerns/accessible.rb
+++ b/app/models/concerns/with_accessibility.rb
@@ -1,4 +1,4 @@
-module Accessible
+module WithAccessibility
   extend ActiveSupport::Concern
 
   def accessible?
diff --git a/app/views/admin/application/a11y/_widget.html.erb b/app/views/admin/application/a11y/_widget.html.erb
index b023bd180..9291b0910 100644
--- a/app/views/admin/application/a11y/_widget.html.erb
+++ b/app/views/admin/application/a11y/_widget.html.erb
@@ -1,14 +1,15 @@
 <%
 horizontal ||= false
-color = about.accessible? ? 'text-success' : 'text-danger'
-action = "<i class=\"#{ Icon::A11Y } fa-2x float-end #{ color}\"></i>"
 %>
-<%= osuny_panel t('accessibility.label'), action: action do %>
+<%= osuny_panel t('accessibility.label') do %>
   <% if horizontal %>
-    <div class="row"><div class="offset-lg-4 col-lg-8">
+    <div class="row"><div class="offset-lg-4 col-lg-8 pt-n4">
   <% end %>
   <% if about.accessibility_errors.any? %>
-    <%= osuny_label t('accessibility.errors', count: about.accessibility_errors.count), classes: 'text-danger' %>
+    <p class="text-danger">
+      <i class="<%= Icon::A11Y %>"></i>
+      <%= t('accessibility.errors', count: about.accessibility_errors.count) %>
+    </p>
     <ol class="list-unstyled">
       <% about.accessibility_errors.each do |key| %>
         <li>
@@ -18,7 +19,10 @@ action = "<i class=\"#{ Icon::A11Y } fa-2x float-end #{ color}\"></i>"
       <% end %>
     </ol>
   <% else %>
-    <p><%= t 'accessibility.errors', count: 0 %></p>
+    <p>
+      <i class="<%= Icon::A11Y %> text-success"></i>
+      <%= t 'accessibility.errors', count: 0 %>
+    </p>
   <% end %>
   <% if about.accessibility_warnings.any? %>
     <%= osuny_label t('accessibility.warnings', count: about.accessibility_warnings.count) %>
diff --git a/app/views/admin/communication/blocks/_block.html.erb b/app/views/admin/communication/blocks/_block.html.erb
index ba4c34b9d..978b3f303 100644
--- a/app/views/admin/communication/blocks/_block.html.erb
+++ b/app/views/admin/communication/blocks/_block.html.erb
@@ -1,4 +1,4 @@
-<div class="row" data-id="block_<%= block.id %>">
+<div data-id="block_<%= block.id %>" class="row">
   <div class="offset-lg-4 col-lg-8">
     <article class="blocks__list__element mt-5 <%= 'draft' unless block.published? %>">
       <div class="mb-1">
diff --git a/app/views/admin/communication/blocks/_index_edit.html.erb b/app/views/admin/communication/blocks/_index_edit.html.erb
index 1ff42a3dd..73a81938a 100644
--- a/app/views/admin/communication/blocks/_index_edit.html.erb
+++ b/app/views/admin/communication/blocks/_index_edit.html.erb
@@ -1,22 +1,20 @@
-<%= osuny_panel Communication::Block.model_name.human(count: 2) do %>
-  <div class="blocks" data-sortable data-sort-url="<%= reorder_admin_communication_blocks_path %>">
-    <% about.blocks.with_no_heading.ordered.each do |block| %>
-      <%= render 'admin/communication/blocks/block', block: block %>
-    <% end %>
-    <% about.headings.root.each do |heading| %>
-      <%= render 'admin/communication/blocks/headings/heading', heading: heading %>
-    <% end %>
+<div class="blocks" data-sortable data-sort-url="<%= reorder_admin_communication_blocks_path %>">
+  <% about.blocks.with_no_heading.ordered.each do |block| %>
+    <%= render 'admin/communication/blocks/block', block: block %>
+  <% end %>
+  <% about.headings.root.each do |heading| %>
+    <%= render 'admin/communication/blocks/headings/heading', heading: heading %>
+  <% end %>
+</div>
+<div class="row mt-5">
+  <div class="col-lg-4">
+    <%= link_to t('admin.communication.blocks.headings.add'),
+                new_admin_communication_heading_path(about_id: about.id, about_type: about.class.name),
+                class: 'py-5 px-2 d-block bg-light text-center h4' if can? :create, Communication::Block::Heading%>
   </div>
-  <div class="row mt-5">
-    <div class="col-lg-4">
-      <%= link_to t('admin.communication.blocks.headings.add'),
-                  new_admin_communication_heading_path(about_id: about.id, about_type: about.class.name),
-                  class: 'py-5 px-2 d-block bg-light text-center h4' if can? :create, Communication::Block::Heading%>
-    </div>
-    <div class="col-lg-8">
-      <%= link_to t('admin.communication.blocks.add'),
-                  new_admin_communication_block_path(about_id: about.id, about_type: about.class.name),
-                  class: 'py-5 px-2 d-block bg-light text-center h4' if can? :create, Communication::Block%>
-    </div>
+  <div class="col-lg-8">
+    <%= link_to t('admin.communication.blocks.add'),
+                new_admin_communication_block_path(about_id: about.id, about_type: about.class.name),
+                class: 'py-5 px-2 d-block bg-light text-center h4' if can? :create, Communication::Block%>
   </div>
-<% end %>
+</div>
diff --git a/app/views/admin/communication/blocks/headings/_form.html.erb b/app/views/admin/communication/blocks/headings/_form.html.erb
index 1af6668be..b3838a45a 100644
--- a/app/views/admin/communication/blocks/headings/_form.html.erb
+++ b/app/views/admin/communication/blocks/headings/_form.html.erb
@@ -8,12 +8,14 @@ url = heading.persisted?  ? admin_communication_heading_path(heading)
   <%= f.input :title %>
   <%= f.input :about_type, as: :hidden %>
   <%= f.input :about_id, as: :hidden %>
-  <% content_for :action_bar_left do %>
-    <%= link_to t('delete'),
-                admin_communication_heading_path(heading), 
-                method: :delete,
-                data: { confirm: t('please_confirm') },
-                class: button_classes_danger if can? :destroy, heading %>
+  <% if heading.persisted?%>
+    <% content_for :action_bar_left do %>
+      <%= link_to t('delete'),
+                  admin_communication_heading_path(heading), 
+                  method: :delete,
+                  data: { confirm: t('please_confirm') },
+                  class: button_classes_danger if can? :destroy, heading %>
+    <% end %>
   <% end %>
   <% content_for :action_bar_right do %>
     <%= submit f %>
diff --git a/app/views/admin/communication/blocks/headings/_heading_static.html.erb b/app/views/admin/communication/blocks/headings/_heading_static.html.erb
index 676e10313..a03e792a2 100644
--- a/app/views/admin/communication/blocks/headings/_heading_static.html.erb
+++ b/app/views/admin/communication/blocks/headings/_heading_static.html.erb
@@ -6,3 +6,7 @@
 <% @heading.blocks.published.ordered.each do |block| @block = block %>
 <%= render 'admin/communication/blocks/block_static' %>
 <% end %>
+<% children = @heading.children %>
+<% children.ordered.each do |heading| @heading = heading %>
+<%= render 'admin/communication/blocks/headings/heading_static' %>
+<% end %>
diff --git a/app/views/admin/communication/websites/menus/show.html.erb b/app/views/admin/communication/websites/menus/show.html.erb
index dc23ad953..249e1ec2a 100644
--- a/app/views/admin/communication/websites/menus/show.html.erb
+++ b/app/views/admin/communication/websites/menus/show.html.erb
@@ -1,23 +1,14 @@
 <% content_for :title, @menu %>
 
 <%= render 'admin/communication/websites/sidebar' do %>
-  <div class="row">
-    <div class="col-md-8">
-      <h2 class="h3">
-        <%= "#{Communication::Website::Menu::Item.model_name.human(count: 2)} (#{@root_items.size})" %>
-      </h2>
+  <%
+  action = ''
+  action += link_to t('create'),
+                    new_admin_communication_website_menu_item_path(website_id: @website, menu_id: @menu.id),
+                    class: button_classes if can?(:create, Communication::Website::Menu::Item) %>
+  <%= osuny_panel Communication::Website::Menu::Item.model_name.human(count: 2), action: action do %>
       <%= render 'admin/communication/websites/menus/items/list', items: @root_items if @items.any? %>
-      <%= link_to t('create'),
-                  new_admin_communication_website_menu_item_path(website_id: @website, menu_id: @menu.id),
-                  class: button_classes if can?(:create, Communication::Website::Menu::Item) %>
-    </div>
-    <div class="col-md-4">
-      <%= render 'admin/application/i18n/widget', about: @menu %>
-      <%= osuny_panel t('metadata') do %>
-        <%= osuny_label Communication::Website::Menu.human_attribute_name('identifier') %>
-        <p><%= @menu.identifier %></p>
-      <% end %>
-  </div>
+  <% end %>
 <% end %>
 
 <% content_for :action_bar_left do %>
diff --git a/app/views/admin/communication/websites/posts/show.html.erb b/app/views/admin/communication/websites/posts/show.html.erb
index c560d5e92..619fa1dfb 100644
--- a/app/views/admin/communication/websites/posts/show.html.erb
+++ b/app/views/admin/communication/websites/posts/show.html.erb
@@ -2,53 +2,29 @@
 
 <%= render 'admin/communication/websites/sidebar' do %>
   <div class="row">
-    <div class="col-xl-8">
-      <%= render 'admin/application/summary/show', about: @post %>
-      <%= render 'admin/communication/blocks/index_edit', about: @post %>
-    </div>
-    <div class="col-xl-4">
-      <%= render 'admin/application/i18n/widget', about: @post %>
-      <%
-      action = ''
-      action += link_to t('open'),
-                        @post.url,
-                        target: :_blank,
-                        class: 'btn btn-light btn-xs' if @post.url
-      %>
-      <%= osuny_panel t('metadata'), action: action do %>
-        <div class="row pure__row--small">
-          <div class="col-6">
-            <%= osuny_label Communication::Website::Post.human_attribute_name('published') %>
-            <p>
-              <%= t @post.published %><% if @post.published & @post.published_at %>,
-                <%= l @post.published_at.to_date, format: :long if @post.published_at %>
-              <% end %>
-            </p>
-          </div>
-          <div class="col-6">
-            <%= osuny_label Communication::Website::Post.human_attribute_name('pinned') %>
-            <p><%= t @post.pinned %></p>
-          </div>
-        </div>
-        <% if @post.author %>
-          <%= osuny_label Communication::Website::Post.human_attribute_name('author') %>
-          <p><%= link_to_if can?(:read, @post.author), @post.author, admin_communication_website_author_path(@post.author) %></p>
-        <% end %>
-        <% if @post.categories.any? %>
-          <%= osuny_label Communication::Website::Post.human_attribute_name('categories') %>
-          <ul class="list-unstyled">
-            <% @post.categories.each do |category| %>
-              <li><%= link_to_if can?(:read, category), category, [:admin, category] %></li>
-            <% end %>
-          </ul>
-        <% end %>
-        <%= osuny_label Communication::Website::Post.human_attribute_name('slug') %>
-        <p><%= @post.slug %></p>
+    <div class="col-lg-7">
+      <%= osuny_panel Communication::Website::Post.human_attribute_name(:title) do %>
+        <p class="lead"><%= @post.title %></p>
       <% end %>
+    </div>
+    <div class="offset-lg-1 col-lg-4">
       <%= render 'admin/application/featured_image/show', about: @post %>
+    </div>
+  </div>
+  <hr class="my-5">
+  <%= render 'admin/application/a11y/widget', about: @post, horizontal: true %>
+  <hr class="my-5">
+  <div class="row">
+    <div class="col-lg-4">
+      <%= render 'admin/communication/websites/posts/show/metadata' %>
+      <%= render 'admin/application/i18n/widget', about: @post %>
+    </div>
+    <div class="col-lg-8">
+      <%= render 'admin/application/summary/show', about: @post %>
       <%= render 'admin/application/meta_description/show', about: @post %>
     </div>
   </div>
+  <%= render 'admin/communication/blocks/index_edit', about: @post %>
 <% end %>
 
 <% content_for :action_bar_left do %>
@@ -58,6 +34,10 @@
 <% end %>
 
 <% content_for :action_bar_right do %>
+  <%= link_to t('open'),
+              @post.url,
+              target: :_blank,
+              class: 'btn btn-light btn-xs' if @post.url %>
   <%= preview_link %>
   <%= edit_link @post %>
 <% end %>
diff --git a/app/views/admin/communication/websites/posts/show/_metadata.html.erb b/app/views/admin/communication/websites/posts/show/_metadata.html.erb
new file mode 100644
index 000000000..6e13f96c9
--- /dev/null
+++ b/app/views/admin/communication/websites/posts/show/_metadata.html.erb
@@ -0,0 +1,15 @@
+<%= osuny_label t('metadata') %><br>
+<% if @post.published %>
+  Publié le <%= l @post.published_at.to_date %>
+  <% if @post.pinned %>
+    et mis en avant
+  <% end %>
+<% else %>
+  Brouillon
+<% end %>
+<% if @post.author %>
+  <br>par <%= @post.author %>
+<% end %>
+<% if @post.categories.any? %>
+  <br>dans <%= @post.categories.collect(&:to_s).join(', ') %>
+<% end %>
-- 
GitLab