From b397de38d80a808e132bf94dc66f045f87e3c629 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 31 Oct 2023 11:43:30 +0100
Subject: [PATCH] clean

---
 .../websites/agenda/events_controller.rb          |  6 +++++-
 .../websites/application_controller.rb            |  4 ++++
 .../communication/websites/posts_controller.rb    |  4 ----
 app/models/communication/website/agenda/event.rb  |  6 ++++++
 app/models/communication/website/category.rb      |  5 +++++
 .../websites/agenda/events/_form.html.erb         | 10 ++++++++++
 .../websites/agenda/events/_list.html.erb         |  9 +++++----
 .../websites/agenda/events/index.html.erb         |  3 +++
 .../websites/agenda/events/show.html.erb          |  7 +------
 .../agenda/events/show/_metadata.html.erb         | 15 +++++++++++++++
 .../websites/agenda/events/static.html.erb        |  1 +
 .../websites/categories/_inline.html.erb          |  5 +++++
 .../websites/categories/_panel.html.erb           | 10 ++++++++++
 .../websites/categories/_static_about.html.erb    |  6 ++++++
 .../communication/websites/posts/_list.html.erb   |  8 +-------
 .../communication/websites/posts/index.html.erb   | 11 +----------
 .../communication/websites/posts/static.html.erb  |  7 +------
 config/locales/communication/en.yml               |  2 ++
 config/locales/communication/fr.yml               |  2 ++
 .../20231031102713_join_categories_to_events.rb   | 11 +++++++++++
 db/schema.rb                                      |  9 ++++++++-
 21 files changed, 102 insertions(+), 39 deletions(-)
 create mode 100644 app/views/admin/communication/websites/agenda/events/show/_metadata.html.erb
 create mode 100644 app/views/admin/communication/websites/categories/_inline.html.erb
 create mode 100644 app/views/admin/communication/websites/categories/_panel.html.erb
 create mode 100644 app/views/admin/communication/websites/categories/_static_about.html.erb
 create mode 100644 db/migrate/20231031102713_join_categories_to_events.rb

diff --git a/app/controllers/admin/communication/websites/agenda/events_controller.rb b/app/controllers/admin/communication/websites/agenda/events_controller.rb
index a78ff1a91..04326f917 100644
--- a/app/controllers/admin/communication/websites/agenda/events_controller.rb
+++ b/app/controllers/admin/communication/websites/agenda/events_controller.rb
@@ -2,8 +2,11 @@ class Admin::Communication::Websites::Agenda::EventsController < Admin::Communic
   load_and_authorize_resource class: Communication::Website::Agenda::Event, 
                               through: :website
 
+  before_action :load_categories, only: [:new, :edit]
+
   def index
     @events = apply_scopes(@events).for_language(current_website_language).ordered.page params[:page]
+    @root_categories = @website.categories.for_language(current_website_language).root.ordered
     breadcrumb
   end
 
@@ -80,7 +83,8 @@ class Admin::Communication::Websites::Agenda::EventsController < Admin::Communic
     .permit(
       :title, :meta_description, :summary, :published, :slug,
       :featured_image, :featured_image_delete, :featured_image_infos, :featured_image_alt, :featured_image_credit,
-      :from_day, :from_hour, :to_day, :to_hour
+      :from_day, :from_hour, :to_day, :to_hour, 
+      category_ids: []
     )
     .merge(
       university_id: current_university.id,
diff --git a/app/controllers/admin/communication/websites/application_controller.rb b/app/controllers/admin/communication/websites/application_controller.rb
index f5111244d..44cb83f3e 100644
--- a/app/controllers/admin/communication/websites/application_controller.rb
+++ b/app/controllers/admin/communication/websites/application_controller.rb
@@ -25,4 +25,8 @@ class Admin::Communication::Websites::ApplicationController < Admin::Communicati
     end
     options
   end
+
+  def load_categories
+    @categories = @website.categories.for_language(current_website_language)
+  end
 end
diff --git a/app/controllers/admin/communication/websites/posts_controller.rb b/app/controllers/admin/communication/websites/posts_controller.rb
index e16f48cec..ba642280d 100644
--- a/app/controllers/admin/communication/websites/posts_controller.rb
+++ b/app/controllers/admin/communication/websites/posts_controller.rb
@@ -129,8 +129,4 @@ class Admin::Communication::Websites::PostsController < Admin::Communication::We
   def load_filters
     @filters = ::Filters::Admin::Communication::Website::Posts.new(current_user, @website).list
   end
-
-  def load_categories
-    @categories = @website.categories.for_language(current_website_language)
-  end
 end
diff --git a/app/models/communication/website/agenda/event.rb b/app/models/communication/website/agenda/event.rb
index 67c710dae..c96700f8f 100644
--- a/app/models/communication/website/agenda/event.rb
+++ b/app/models/communication/website/agenda/event.rb
@@ -58,6 +58,12 @@ class Communication::Website::Agenda::Event < ApplicationRecord
               class_name: 'Communication::Website::Agenda::Event',
               optional: true
 
+  has_and_belongs_to_many :categories,
+                          class_name: 'Communication::Website::Category',
+                          join_table: :communication_website_agenda_events_categories,
+                          foreign_key: :communication_website_agenda_event_id,
+                          association_foreign_key: :communication_website_category_id
+
   scope :ordered_desc, -> { order(from_day: :desc, from_hour: :desc) }
   scope :ordered_asc, -> { order(:from_day, :from_hour) }
   scope :ordered, -> { ordered_asc }
diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 17afdc07c..64e565c1a 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -70,6 +70,11 @@ class Communication::Website::Category < ApplicationRecord
                           join_table: :communication_website_categories_posts,
                           foreign_key: :communication_website_category_id,
                           association_foreign_key: :communication_website_post_id
+  has_and_belongs_to_many :events,
+                          class_name: 'Communication::Website::Agenda::Event',
+                          join_table: :communication_website_agenda_events_categories,
+                          foreign_key: :communication_website_category_id,
+                          association_foreign_key: :communication_website_agenda_event_id
 
   validates :name, presence: true
 
diff --git a/app/views/admin/communication/websites/agenda/events/_form.html.erb b/app/views/admin/communication/websites/agenda/events/_form.html.erb
index 576ba9c86..380a0448c 100644
--- a/app/views/admin/communication/websites/agenda/events/_form.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/_form.html.erb
@@ -26,7 +26,17 @@
         </div>
       </div>
       <% end %>
+      <% if @categories.any? %>
+        <%= osuny_panel t('activerecord.attributes.communication/website/post.categories') do %>
+          <%= f.association :categories,
+                            label_text: false,
+                            as: :check_boxes,
+                            collection: collection_tree_for_checkboxes(@categories) %>
+        <% end %>
+      <% end %>
+
       <%= render 'admin/application/meta_description/form', f: f, about: event %>
+      
     </div>
     <div class="col-md-4">
       <%= osuny_panel t('metadata') do %>
diff --git a/app/views/admin/communication/websites/agenda/events/_list.html.erb b/app/views/admin/communication/websites/agenda/events/_list.html.erb
index 98cdcba21..038ea46d1 100644
--- a/app/views/admin/communication/websites/agenda/events/_list.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/_list.html.erb
@@ -7,18 +7,19 @@
         <tr>
           <th class="ps-0" style="min-width: 250px"><%= Communication::Website::Agenda::Event.human_attribute_name('title') %></th>
           <th><%= Communication::Website::Agenda::Event.human_attribute_name('dates') %></th>
+          <th><%= Communication::Website::Agenda::Event.human_attribute_name('categories') %></th>
           <th><%= Communication::Website::Agenda::Event.human_attribute_name('featured_image') %></th>
         </tr>
       </thead>
       <tbody>
         <% events.each do |event| %>
-          <tr class="<% 'draft' unless event.published? %>">
-            <td class="ps-0">
+          <tr class="<%= 'draft' unless event.published? %>">
+            <td>
               <%= link_to event,
-                          admin_communication_website_agenda_event_path(website_id: event.website.id, id: event.id),
-                          class: "#{'draft' unless event.published?}" %>
+                          admin_communication_website_agenda_event_path(website_id: event.website.id, id: event.id) %>
             </td>
             <td class="small"><%= render 'admin/communication/websites/agenda/events/dates', event: event %></td>
+            <td class="small"><%= render 'admin/communication/websites/categories/inline', about: event %></td>
             <td>
               <%= kamifusen_tag event.featured_image, width: 100 if event.featured_image.attached? %>
             </td>
diff --git a/app/views/admin/communication/websites/agenda/events/index.html.erb b/app/views/admin/communication/websites/agenda/events/index.html.erb
index 92fd99626..9f75cfd77 100644
--- a/app/views/admin/communication/websites/agenda/events/index.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/index.html.erb
@@ -15,4 +15,7 @@
     <%= render 'admin/communication/websites/agenda/events/list', events: @events %>
     <%= paginate @events, theme: 'bootstrap-5' %>
   <% end %>
+  
+  <%= render 'admin/communication/websites/categories/panel', root_categories: @root_categories %>
 <% end %>
+
diff --git a/app/views/admin/communication/websites/agenda/events/show.html.erb b/app/views/admin/communication/websites/agenda/events/show.html.erb
index 3bd2fded9..b613966a7 100644
--- a/app/views/admin/communication/websites/agenda/events/show.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/show.html.erb
@@ -17,12 +17,7 @@
   <hr class="mb-5">
   <div class="row">
     <div class="col-lg-4">
-      <div class="mb-4">
-        <%= osuny_label t('metadata') %><br>
-        <% # TODO i18n après Violenn %>
-        <%= @event.published ? 'Publié' : 'Brouillon' %>
-        <%= render 'admin/application/i18n/inline', about: @event %>
-      </div>
+      <%= render 'admin/communication/websites/agenda/events/show/metadata' %>
     </div>
     <div class="col-lg-8">
       <%= render 'admin/application/summary/show', about: @event, small: true %>
diff --git a/app/views/admin/communication/websites/agenda/events/show/_metadata.html.erb b/app/views/admin/communication/websites/agenda/events/show/_metadata.html.erb
new file mode 100644
index 000000000..f597b0a7f
--- /dev/null
+++ b/app/views/admin/communication/websites/agenda/events/show/_metadata.html.erb
@@ -0,0 +1,15 @@
+<div class="mb-4">
+  <%= osuny_label t('metadata') %><br>
+  
+  <%= @event.published  ? Communication::Website::Agenda::Event.human_attribute_name(:published)
+                        : t('admin.communication.website.agenda.events.draft') %>
+
+  <%= render 'admin/application/i18n/inline', about: @event %>
+
+  <% if @event.categories.any? %>
+    <%= t(
+          'admin.communication.website.posts.in', 
+          categories: @event.categories.collect(&:to_s).join(', ')
+        ) %>
+  <% end %>
+</div>
diff --git a/app/views/admin/communication/websites/agenda/events/static.html.erb b/app/views/admin/communication/websites/agenda/events/static.html.erb
index 9e83a432b..137d6f1c0 100644
--- a/app/views/admin/communication/websites/agenda/events/static.html.erb
+++ b/app/views/admin/communication/websites/agenda/events/static.html.erb
@@ -11,5 +11,6 @@ weight: <%= @about.distance_in_days %>
 <%= render 'admin/application/featured_image/static' %>
 <%= render 'admin/application/meta_description/static' %>
 <%= render 'admin/application/summary/static' %>
+<%= render 'admin/communication/websites/categories/static_about' %>
 <%= render 'admin/communication/blocks/content/static', about: @about %>
 ---
diff --git a/app/views/admin/communication/websites/categories/_inline.html.erb b/app/views/admin/communication/websites/categories/_inline.html.erb
new file mode 100644
index 000000000..2594e1625
--- /dev/null
+++ b/app/views/admin/communication/websites/categories/_inline.html.erb
@@ -0,0 +1,5 @@
+<ul class="list-unstyled mb-0">
+  <% about.categories.each do |category| %>
+    <li><%= category %></li>
+  <% end %>
+</ul>
diff --git a/app/views/admin/communication/websites/categories/_panel.html.erb b/app/views/admin/communication/websites/categories/_panel.html.erb
new file mode 100644
index 000000000..a289c6377
--- /dev/null
+++ b/app/views/admin/communication/websites/categories/_panel.html.erb
@@ -0,0 +1,10 @@
+<% if can?(:create, Communication::Website::Category) || (root_categories.any? && can?(:edit, root_categories.first))  %>
+  <% action = create_link Communication::Website::Category %>
+  <%= osuny_panel Communication::Website::Category.model_name.human(count: 2), action: action do %>
+    <ul class="list-unstyled treeview treeview--sortable js-treeview js-treeview-sortable js-treeview-sortable-container"
+        data-id=""
+        data-sort-url="<%= reorder_admin_communication_website_categories_path %>">
+      <%= render 'admin/communication/websites/categories/treebranch', categories: root_categories %>
+    </ul>
+  <% end %>
+<% end %>
diff --git a/app/views/admin/communication/websites/categories/_static_about.html.erb b/app/views/admin/communication/websites/categories/_static_about.html.erb
new file mode 100644
index 000000000..1f6b4ec4c
--- /dev/null
+++ b/app/views/admin/communication/websites/categories/_static_about.html.erb
@@ -0,0 +1,6 @@
+<% if @about.categories.any? %>
+categories:
+  <% @about.categories.each do |category| %>
+  - "<%= category.slug_with_ancestors_slugs %>"
+  <% end %>
+<% end %>
\ No newline at end of file
diff --git a/app/views/admin/communication/websites/posts/_list.html.erb b/app/views/admin/communication/websites/posts/_list.html.erb
index 5f04b74e9..1c99eaa85 100644
--- a/app/views/admin/communication/websites/posts/_list.html.erb
+++ b/app/views/admin/communication/websites/posts/_list.html.erb
@@ -42,13 +42,7 @@ selectable ||= false
               <% if !hide_author && post.author %>
                 <%= post.author %><br>
               <% end %>
-              <% unless hide_category %>
-                <ul class="list-unstyled mb-0">
-                  <% post.categories.each do |category| %>
-                    <li><%= category %></li>
-                  <% end %>
-                </ul>
-              <% end %>
+              <%= render 'admin/communication/websites/categories/inline', about: post unless hide_category %>
               <% if post.pinned %>
                 <%= Communication::Website::Post.human_attribute_name('pinned') %>
               <% end %>
diff --git a/app/views/admin/communication/websites/posts/index.html.erb b/app/views/admin/communication/websites/posts/index.html.erb
index acf2195ff..18e9992fb 100644
--- a/app/views/admin/communication/websites/posts/index.html.erb
+++ b/app/views/admin/communication/websites/posts/index.html.erb
@@ -39,16 +39,7 @@
       <% end %>
     </div>
   <% end %>
-  <% if can?(:create, Communication::Website::Category) || (@root_categories.any? && can?(:edit, @root_categories.first))  %>
-      <% action = create_link Communication::Website::Category %>
-      <%= osuny_panel Communication::Website::Category.model_name.human(count: 2), action: action do %>
-        <ul class="list-unstyled treeview treeview--sortable js-treeview js-treeview-sortable js-treeview-sortable-container"
-            data-id=""
-            data-sort-url="<%= reorder_admin_communication_website_categories_path %>">
-          <%= render 'admin/communication/websites/categories/treebranch', categories: @root_categories %>
-        </ul>
-      <% end %>
-  <% end %>
+  <%= render 'admin/communication/websites/categories/panel', root_categories: @root_categories %>
   <% if @authors.any? %>
     <%= osuny_panel t('communication.authors', count: 2) do %>
       <%= render 'admin/communication/websites/authors/list', authors: @authors %>
diff --git a/app/views/admin/communication/websites/posts/static.html.erb b/app/views/admin/communication/websites/posts/static.html.erb
index 0c17c1a6a..52d468a4a 100644
--- a/app/views/admin/communication/websites/posts/static.html.erb
+++ b/app/views/admin/communication/websites/posts/static.html.erb
@@ -13,12 +13,7 @@ weight: 1
 authors:
   - "<%= @about.translated_author.slug %>"
 <% end %>
-<% if @about.categories.any? %>
-categories:
-  <% @about.categories.each do |category| %>
-  - "<%= category.slug_with_ancestors_slugs %>"
-  <% end %>
-<% end %>
+<%= render 'admin/communication/websites/categories/static_about' %>
 <%= render 'admin/application/i18n/static' %>
 <%= render 'admin/application/featured_image/static' %>
 <%= render 'admin/application/meta_description/static' %>
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index db409f658..91b22d9e9 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -165,6 +165,7 @@ en:
         url: URL
         university: University
       communication/website/agenda/event:
+        categories: Categories
         dates: Dates
         featured_image: Featured image
         from_day: From day
@@ -256,6 +257,7 @@ en:
       website:
         agenda:
           events:
+            draft: Draft
             none: No event yet
         default_image: "The default image will be used on pages and posts that do not have an image, if the theme is set to display images in all cases. It is also the default sharing image."
         editorial: 
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index fb95a9c82..21c29c120 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -165,6 +165,7 @@ fr:
         university: Université
         url: URL
       communication/website/agenda/event:
+        categories: Catégories
         dates: Dates
         featured_image: Image à la une
         from_day: Jour de début
@@ -257,6 +258,7 @@ fr:
         agenda:
           events:
             none: Pas encore d'événements
+            draft: Brouillon
         default_image: "L'image par défaut sera utilisée dans les pages et actualités qui n'ont pas d'image, si le thème est paramétré pour afficher des images dans tous les cas. C'est aussi l'image de partage par défaut."
         editorial: 
           label: Éditorial
diff --git a/db/migrate/20231031102713_join_categories_to_events.rb b/db/migrate/20231031102713_join_categories_to_events.rb
new file mode 100644
index 000000000..20f7786c5
--- /dev/null
+++ b/db/migrate/20231031102713_join_categories_to_events.rb
@@ -0,0 +1,11 @@
+class JoinCategoriesToEvents < ActiveRecord::Migration[7.1]
+  def change
+
+  create_table "communication_website_agenda_events_categories", id: false, force: :cascade do |t|
+    t.uuid "communication_website_agenda_event_id", null: false
+    t.uuid "communication_website_category_id", null: false
+    t.index ["communication_website_category_id", "communication_website_agenda_event_id"], name: "category_event"
+    t.index ["communication_website_agenda_event_id", "communication_website_category_id"], name: "event_category"
+  end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 659e0ae39..2eebca15a 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema[7.1].define(version: 2023_10_18_182341) do
+ActiveRecord::Schema[7.1].define(version: 2023_10_31_102713) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -260,6 +260,13 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_18_182341) do
     t.index ["university_id"], name: "index_communication_website_agenda_events_on_university_id"
   end
 
+  create_table "communication_website_agenda_events_categories", id: false, force: :cascade do |t|
+    t.uuid "communication_website_agenda_event_id", null: false
+    t.uuid "communication_website_category_id", null: false
+    t.index ["communication_website_agenda_event_id", "communication_website_category_id"], name: "event_category"
+    t.index ["communication_website_category_id", "communication_website_agenda_event_id"], name: "category_event"
+  end
+
   create_table "communication_website_categories", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
     t.uuid "university_id", null: false
     t.uuid "communication_website_id", null: false
-- 
GitLab