From 81d5cfba4fd4f83e925cf0ae47c12637b3b14186 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Tue, 4 Jan 2022 14:48:15 +0100
Subject: [PATCH] fix

---
 app/helpers/admin/application_helper.rb                   | 8 ++++++++
 app/models/communication/website.rb                       | 8 --------
 app/models/communication/website/page.rb                  | 4 ----
 app/models/concerns/with_tree.rb                          | 2 +-
 .../admin/communication/website/pages/_form.html.erb      | 2 +-
 5 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb
index cf9e2a493..c2d92946f 100644
--- a/app/helpers/admin/application_helper.rb
+++ b/app/helpers/admin/application_helper.rb
@@ -72,6 +72,14 @@ module Admin::ApplicationHelper
     sanitize text
   end
 
+  def collection_tree(list, except)
+    collection = []
+    list.root.ordered.each do |object|
+      collection.concat(object.self_and_children(0))
+    end
+    collection.reject { |o| o[:id] == except.id }
+  end
+
   private
 
   def polymorphic_url_param(object_or_class, **options)
diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb
index 86dc76900..b8aba04c7 100644
--- a/app/models/communication/website.rb
+++ b/app/models/communication/website.rb
@@ -41,14 +41,6 @@ class Communication::Website < ApplicationRecord
     "#{name}"
   end
 
-  def list_of_pages
-    all_pages = []
-    pages.root.ordered.each do |page|
-      all_pages.concat(page.self_and_children(0))
-    end
-    all_pages
-  end
-
   def list_of_categories
     all_categories = []
     categories.root.ordered.each do |category|
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 6d328380c..631d3b0ed 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -79,10 +79,6 @@ class Communication::Website::Page < ApplicationRecord
     descendents + siblings
   end
 
-  def list_of_other_pages
-    website.list_of_pages.reject! { |p| p[:id] == id }
-  end
-
   def to_s
     "#{ title }"
   end
diff --git a/app/models/concerns/with_tree.rb b/app/models/concerns/with_tree.rb
index 4020468f6..8bfa2d557 100644
--- a/app/models/concerns/with_tree.rb
+++ b/app/models/concerns/with_tree.rb
@@ -24,7 +24,7 @@ module WithTree
     end
 
     def siblings
-      self.class.where(parent: parent).where.not(id: id)
+      self.class.where(parent: parent, university: university, website: website).where.not(id: id)
     end
 
     def self_and_children(level)
diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb
index a750e89d7..4cb44c14b 100644
--- a/app/views/admin/communication/website/pages/_form.html.erb
+++ b/app/views/admin/communication/website/pages/_form.html.erb
@@ -30,7 +30,7 @@
                       } %>
           <%= f.input :published %>
           <%= f.association :parent,
-                            collection: page.persisted? ? page.list_of_other_pages : page.website.list_of_pages,
+                            collection: collection_tree(page.website.pages, page),
                             label_method: ->(p) { sanitize p[:label] },
                             value_method: ->(p) { p[:id] } %>
           <ul>
-- 
GitLab