From 04691c68ed084428e80302dad25ee88dc2f7fea0 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Thu, 12 May 2022 15:51:51 +0200
Subject: [PATCH] blocks

---
 app/controllers/server/blocks_controller.rb | 22 +++++++++++++++++++++
 app/views/server/blocks/index.html.erb      | 18 +++++++++++++++++
 app/views/server/blocks/show.html.erb       | 20 +++++++++++++++++++
 config/routes.rb                            |  6 +-----
 config/routes/server.rb                     |  6 ++++++
 config/server_navigation.rb                 | 18 ++++++++++++++---
 6 files changed, 82 insertions(+), 8 deletions(-)
 create mode 100644 app/controllers/server/blocks_controller.rb
 create mode 100644 app/views/server/blocks/index.html.erb
 create mode 100644 app/views/server/blocks/show.html.erb
 create mode 100644 config/routes/server.rb

diff --git a/app/controllers/server/blocks_controller.rb b/app/controllers/server/blocks_controller.rb
new file mode 100644
index 000000000..08be78e16
--- /dev/null
+++ b/app/controllers/server/blocks_controller.rb
@@ -0,0 +1,22 @@
+class Server::BlocksController < Server::ApplicationController
+  def index
+    @templates = Communication::Block.template_kinds.keys
+    breadcrumb
+  end
+
+  def show
+    @template = params[:id]
+    @blocks = Communication::Block.send(@template)
+    breadcrumb
+    add_breadcrumb t("enums.communication.block.template_kind.#{@template}")
+  end
+
+  protected
+
+  def breadcrumb
+    super
+    add_breadcrumb Communication::Block.model_name.human(count: 2), server_blocks_path
+  end
+
+
+end
diff --git a/app/views/server/blocks/index.html.erb b/app/views/server/blocks/index.html.erb
new file mode 100644
index 000000000..c19fa8b04
--- /dev/null
+++ b/app/views/server/blocks/index.html.erb
@@ -0,0 +1,18 @@
+<% content_for :title, Communication::Block.model_name.human(count: 2) %>
+
+<table class="<%= table_classes %>">
+  <thead>
+    <tr>
+      <th><%= Communication::Block.human_attribute_name('name') %></th>
+      <th><%= Communication::Block.human_attribute_name('quantity') %></th>
+    </tr>
+  </thead>
+  <tbody>
+    <% @templates.each do |template| %>
+      <tr>
+        <td><%= link_to t("enums.communication.block.template_kind.#{template}"), server_block_path(template) %></td>
+        <td><%= Communication::Block.send(template).count %></td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
diff --git a/app/views/server/blocks/show.html.erb b/app/views/server/blocks/show.html.erb
new file mode 100644
index 000000000..d1427ef97
--- /dev/null
+++ b/app/views/server/blocks/show.html.erb
@@ -0,0 +1,20 @@
+<% content_for :title, Communication::Block.model_name.human(count: 2) %>
+
+<table class="<%= table_classes %>">
+  <thead>
+    <tr>
+      <th><%= Communication::Block.human_attribute_name('name') %></th>
+      <th><%= Communication::Block.human_attribute_name('about') %></th>
+      <th><%= Communication::Block.human_attribute_name('university') %></th>
+    </tr>
+  </thead>
+  <tbody>
+    <% @blocks.each do |block| %>
+      <tr>
+        <td><%= link_to block, edit_admin_communication_block_url(block), target: :_blank %></td>
+        <td><%= block.about %></td>
+        <td><%= block.about.university %></td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
diff --git a/config/routes.rb b/config/routes.rb
index 5e9f0af11..2e983de6e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -28,15 +28,11 @@ Rails.application.routes.draw do
     root to: 'dashboard#index'
   end
 
-  namespace :server do
-    resources :universities
-    resources :languages
-    root to: 'dashboard#index'
-  end
 
   get '/media/:signed_id/:filename_with_transformations' => 'media#show', as: :medium
 
   draw 'api'
   draw 'extranet'
+  draw 'server'
   # Root is in extranet
 end
diff --git a/config/routes/server.rb b/config/routes/server.rb
new file mode 100644
index 000000000..adb099dfd
--- /dev/null
+++ b/config/routes/server.rb
@@ -0,0 +1,6 @@
+namespace :server do
+  resources :universities, :languages
+  get 'blocks' => 'blocks#index', as: :blocks
+  get 'blocks/:id' => 'blocks#show', as: :block
+  root to: 'dashboard#index'
+end
diff --git a/config/server_navigation.rb b/config/server_navigation.rb
index 4881a4db1..06113b460 100644
--- a/config/server_navigation.rb
+++ b/config/server_navigation.rb
@@ -4,8 +4,20 @@ SimpleNavigation::Configuration.run do |navigation|
   navigation.highlight_on_subpath = true
   navigation.selected_class = 'active'
   navigation.items do |primary|
-    primary.item :dashboard, t('dashboard'), server_root_path, { icon: 'tachometer-alt', highlights_on: %r{server$} }
-    primary.item :universities, University.model_name.human(count: 2), server_universities_path, { icon: 'university' } if can?(:read, University)
-    primary.item :languages, Language.model_name.human(count: 2), server_languages_path, { icon: 'flag' } if can?(:read, Language)
+    primary.item  :dashboard,
+                  t('dashboard'),
+                  server_root_path,
+                  { icon: 'tachometer-alt', highlights_on: %r{server$} }
+    primary.item  :universities,
+                  University.model_name.human(count: 2),
+                  server_universities_path, { icon: 'university' } if can?(:read, University)
+    primary.item  :languages,
+                  Language.model_name.human(count: 2),
+                  server_languages_path,
+                  { icon: 'flag' } if can?(:read, Language)
+    primary.item  :blocks,
+                  Communication::Block.model_name.human(count: 2),
+                  server_blocks_path,
+                  { icon: 'puzzle-piece' } if can?(:read, Communication::Block)
   end
 end
-- 
GitLab