From 8fb3ae44e53850400613875e9e05408721b60c09 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <arnaud.levy@noesya.coop>
Date: Tue, 19 Nov 2024 09:23:03 +0100
Subject: [PATCH] =?UTF-8?q?R=C3=A9paration=20de=20l'arbre=20(#2430)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Fix #2429

* better
---
 app/models/concerns/with_tree.rb | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/app/models/concerns/with_tree.rb b/app/models/concerns/with_tree.rb
index 0101f1404..3ca0f40ee 100644
--- a/app/models/concerns/with_tree.rb
+++ b/app/models/concerns/with_tree.rb
@@ -14,8 +14,7 @@ module WithTree
   end
 
   def ancestors
-    has_parent? ? parent.ancestors.push(parent)
-                : []
+    has_parent? ? parent.ancestors.push(parent) : []
   end
 
   def ancestors_and_self
@@ -23,8 +22,7 @@ module WithTree
   end
 
   def descendants
-    has_children? ? children.ordered(original_localization.language).map { |child| [child, child.descendants].flatten }.flatten
-                  : []
+    has_children? ? descendants_flattened : []
   end
 
   def descendants_and_self
@@ -33,22 +31,31 @@ module WithTree
 
   def siblings
     self.class.unscoped
-              .where(
-                parent: parent,
-                university: university
-              )
+              .where(parent: parent, university: university)
               .where.not(id: id)
-              .ordered(original_localization.language)
+              .ordered(original_language)
   end
 
   def self_and_children(level)
     elements = []
     label = "&nbsp;&nbsp;&nbsp;" * level + self.to_s
     elements << { label: label, id: self.id, parent_id: self.parent_id }
-    children.ordered(original_localization.language).each do |child|
+    children.ordered(original_language).each do |child|
       elements.concat(child.self_and_children(level + 1))
     end
     elements
   end
+  
+  protected
+
+  def original_language
+    self.respond_to?(:original_localization) ? original_localization.language : nil
+  end
+
+  def descendants_flattened
+    children.ordered(original_language).map { |child| 
+      [child, child.descendants]
+    }.flatten
+  end
 
 end
-- 
GitLab