From 703d18011fd3de302f43012b68ed9d641342e675 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Mon, 13 Dec 2021 12:17:02 +0100
Subject: [PATCH] wip menus

---
 app/models/communication/website.rb           | 25 +++++++++++++++----
 app/models/communication/website/menu/item.rb |  2 +-
 .../website/menu/items/_form.html.erb         |  5 +++-
 .../website/menu/items/kind_switch.js.erb     | 24 +++++++++++++++---
 .../website/menu/items/show.html.erb          |  2 +-
 5 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index eddce3c46..705422260 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -62,7 +62,7 @@ class Communication::Website < ApplicationRecord
 
   after_create :create_home
   after_save :publish_about_object, if: :saved_change_to_about_id?
-  after_save_commit :set_programs_categories!, if: -> (website) { website.about_type == 'Education::School' }
+  after_save_commit :set_programs_categories!, if: -> (website) { website.about_school? }
 
   scope :ordered, -> { order(:name) }
 
@@ -74,12 +74,15 @@ class Communication::Website < ApplicationRecord
     "#{name}"
   end
 
+  def programs
+    about_school? ? about.programs : Education::Program.none
+  end
+
   def import!
-    unless imported?
-      self.imported_website = Communication::Website::Imported::Website.where(website: self, university: university)
-                                                                        .first_or_create
+    imported_website = Communication::Website::Imported::Website.where(
+      website: self, university: university
+    ).first_or_create unless imported?
 
-    end
     imported_website.run!
     imported_website
   end
@@ -104,6 +107,14 @@ class Communication::Website < ApplicationRecord
     all_categories
   end
 
+  def list_of_programs
+    all_programs = []
+    programs.root.ordered.each do |program|
+      all_programs.concat(program.self_and_children(0))
+    end
+    all_programs
+  end
+
   protected
 
   def create_home
@@ -113,4 +124,8 @@ class Communication::Website < ApplicationRecord
   def github
     @github ||= Github.with_website self
   end
+
+  def about_school?
+    about_type == 'Education::School'
+  end
 end
diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb
index 9cb253e41..243c6e1a4 100644
--- a/app/models/communication/website/menu/item.rb
+++ b/app/models/communication/website/menu/item.rb
@@ -81,7 +81,7 @@ class Communication::Website::Menu::Item < ApplicationRecord
   end
 
   def has_about?
-    kind_page? || kind_program? || kind_news_category? || kind_news_article
+    kind_page? || kind_program? || kind_news_category? || kind_news_article?
   end
 
   protected
diff --git a/app/views/admin/communication/website/menu/items/_form.html.erb b/app/views/admin/communication/website/menu/items/_form.html.erb
index 2beebc68f..973295938 100644
--- a/app/views/admin/communication/website/menu/items/_form.html.erb
+++ b/app/views/admin/communication/website/menu/items/_form.html.erb
@@ -24,11 +24,14 @@
                 if item.kind_page?
                   about_collection = @website.list_of_pages
                 elsif item.kind_program?
+                  about_collection = @website.list_of_programs
                 elsif item.kind_news_category?
+                  about_collection = @website.list_of_categories
                 elsif item.kind_news_article?
+                  about_collection = @website.posts.ordered.map { |e| { label: e.to_s, id: e.id } }
                 end
               else
-                about_collection = nil
+                about_collection = []
               end
             %>
             <%= f.input :about_type, as: :hidden, input_html: { class: 'js-about-type' } %>
diff --git a/app/views/admin/communication/website/menu/items/kind_switch.js.erb b/app/views/admin/communication/website/menu/items/kind_switch.js.erb
index 71103f364..cb32d1252 100644
--- a/app/views/admin/communication/website/menu/items/kind_switch.js.erb
+++ b/app/views/admin/communication/website/menu/items/kind_switch.js.erb
@@ -21,11 +21,29 @@ function hideAbout() {
     %>
     displayAbout('Communication::Website::Page', "<%= j(raw(options.join(''))) %>");
 <% elsif @kind == 'program' %>
-    displayAbout();
+    <%
+    options = ['<option value="" label=" "></option>']
+    @website.list_of_programs.each do |page|
+        options << "<option value=\"#{page[:id]}\">#{page[:label]}</option>"
+    end
+    %>
+    displayAbout('Education::Program', "<%= j(raw(options.join(''))) %>");
 <% elsif @kind == 'news_category' %>
-    displayAbout();
+    <%
+        options = ['<option value="" label=" "></option>']
+        @website.list_of_categories.each do |category|
+            options << "<option value=\"#{category[:id]}\">#{category[:label]}</option>"
+        end
+    %>
+    displayAbout('Communication::Website::Category', "<%= j(raw(options.join(''))) %>");
 <% elsif @kind == 'news_article' %>
-    displayAbout();
+    <%
+        options = ['<option value="" label=" "></option>']
+        @website.posts.ordered.each do |post|
+            options << "<option value=\"#{post.id}\">#{post.to_s}</option>"
+        end
+    %>
+    displayAbout('Communication::Website::Post', "<%= j(raw(options.join(''))) %>");
 <% else %>
     $('.js-url-field').addClass('d-none');
     hideAbout();
diff --git a/app/views/admin/communication/website/menu/items/show.html.erb b/app/views/admin/communication/website/menu/items/show.html.erb
index d07ab910e..312dd7bb5 100644
--- a/app/views/admin/communication/website/menu/items/show.html.erb
+++ b/app/views/admin/communication/website/menu/items/show.html.erb
@@ -26,7 +26,7 @@
           <% if @item.kind_url? %>
             <h3 class="h5"><%= Communication::Website::Menu::Item.human_attribute_name('url') %></h3>
             <p><%= link_to @item.url, @item.url, target: '_blank' unless @item.url.blank? %></p>
-          <% elsif @item.kind_page? %>
+          <% elsif @item.has_about? %>
             <h3 class="h5"><%= Communication::Website::Menu::Item.human_attribute_name('about') %></h3>
             <p><%= link_to_if can?(:read, @item.about), @item.about, [:admin, @item.about] %></p>
           <% end %>
-- 
GitLab