From d49a5393f18b52af8f308ecd8f14709d51233f7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Fri, 14 Jan 2022 13:43:50 +0100
Subject: [PATCH] menu

---
 app/models/communication/website/menu/item.rb | 25 +++++++++++--------
 .../website/menus/static.html.erb             |  1 +
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb
index e923cf923..707c9f7bf 100644
--- a/app/models/communication/website/menu/item.rb
+++ b/app/models/communication/website/menu/item.rb
@@ -73,31 +73,34 @@ class Communication::Website::Menu::Item < ApplicationRecord
   end
 
   def static_target
-    target = ''
+    target = nil
     case self.kind
     when 'url'
       target = url
+    when 'page', 'news_article'
+      target = about.path if about&.published_at
     when 'programs'
-      target = "/#{website.programs_github_directory}"
+      target = "/#{website.programs_github_directory}" if website.programs.any?
     when 'program'
-      target = "/#{website.programs_github_directory}#{about.path}"
+      target = "/#{website.programs_github_directory}#{about.path}" if website.about_school?
     when 'news'
-      target = "/#{website.posts_github_directory}"
+      target = "/#{website.posts_github_directory}" if website.posts.published.any?
     when 'staff'
-      target = "/#{website.staff_github_directory}"
+      target = "/#{website.staff_github_directory}" if website.people.any?
     when 'research_volumes'
-      target = "/#{website.research_volumes_github_directory}"
+      target = "/#{website.research_volumes_github_directory}" if website.research_volumes.published.any?
     when 'research_volume'
-      target = "/#{website.research_volumes_github_directory}#{about.path}"
+      target = "/#{website.research_volumes_github_directory}#{about.path}" if about&.published_at
     when 'research_articles'
-      target = "/#{website.research_articles_github_directory}"
+      target = "/#{website.research_articles_github_directory}" if website.research_articles.published.any?
     when 'research_article'
-      target = "/#{website.research_articles_github_directory}#{about.path}"
+      target = "/#{website.research_articles_github_directory}#{about.path}" if about&.published_at
     when 'blank'
       target = ''
     else
       target = about&.path
     end
+    return nil if target.nil?
     target.end_with?('/') ? target
                           : "#{target}/"
   end
@@ -112,12 +115,12 @@ class Communication::Website::Menu::Item < ApplicationRecord
   end
 
   def to_static_hash
-    return {} if kind_news_article? && !about.published
+    return nil if static_target.nil?
     {
       'title' => title,
       'target' => static_target,
       'kind' => kind,
-      'children' => children.ordered.map(&:to_static_hash)
+      'children' => children.ordered.map(&:to_static_hash).compact
     }
   end
 
diff --git a/app/views/admin/communication/website/menus/static.html.erb b/app/views/admin/communication/website/menus/static.html.erb
index 035a94b4a..8e9ef0627 100644
--- a/app/views/admin/communication/website/menus/static.html.erb
+++ b/app/views/admin/communication/website/menus/static.html.erb
@@ -2,4 +2,5 @@
              .root
              .ordered
              .map(&:to_static_hash)
+             .compact
              .to_yaml %>
-- 
GitLab