summaryrefslogtreecommitdiff
path: root/app/models/node.rb
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-07 02:46:37 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-07 02:46:37 +0200
commit7b6af89509e8439fe2474e623ee97e4db67ab011 (patch)
tree47aa227de58d566727e8afdc44d7abc0934f22a6 /app/models/node.rb
parent527376039c527eb8f559c5e6da76429bc3f3ee4f (diff)
Replace awesome_nested_set with a plain parent_id-based NestedTree concern
Root-caused this session: appending a child to any node never widened that parent's own rgt boundary, on the pinned revision (Gemfile tracked main directly, chasing a too-conservative gemspec constraint - not, as first assumed, a deliberate pin to avoid a known bug). Reproduced cleanly on a single ordinary create with no concurrency and no bulk operation involved, confirmed via the gem's own SetValidator, then confirmed as the root cause of nodes_controller_test.rb's 3 long-standing "pre-existing" failures - not three separate mysteries, one bug. admin_controller's sitemap needed its own real conversion, not just a drop-in: awesome_nested_set's lft column implicitly provided correct depth-first tree order for free, which the old code combined with a separate class-level each_with_level iterator. Both replaced by one method, self_and_descendants_ordered_with_level, computing an ordered [node, level] list in a single query-then-walk pass - checked against the actual view template first (admin/index.html.erb) rather than assumed, since it relies on list order alone to render correct visual nesting. lft/rgt/depth columns intentionally left in schema, unused - dropping them is a separate, deliberately deferred migration once this is proven running for a while, not bundled with the behavior change.
Diffstat (limited to 'app/models/node.rb')
-rw-r--r--app/models/node.rb25
1 files changed, 6 insertions, 19 deletions
diff --git a/app/models/node.rb b/app/models/node.rb
index 2177f15..fc23dc1 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -1,6 +1,6 @@
1class Node < ApplicationRecord 1class Node < ApplicationRecord
2 # Mixins and Plugins 2 # Mixins and Plugins
3 acts_as_nested_set 3 include NestedTree
4 4
5 # Associations 5 # Associations
6 has_many :pages, -> { order("revision ASC") }, :dependent => :destroy 6 has_many :pages, -> { order("revision ASC") }, :dependent => :destroy
@@ -21,16 +21,6 @@ class Node < ApplicationRecord
21 validates_uniqueness_of :slug, :scope => :parent_id, :unless => -> { parent_id.nil? } 21 validates_uniqueness_of :slug, :scope => :parent_id, :unless => -> { parent_id.nil? }
22 validates_presence_of :parent_id, :unless => -> { Node.root.nil? } 22 validates_presence_of :parent_id, :unless => -> { Node.root.nil? }
23 23
24 validate :borders # This should never ever happen.
25
26 # Index for Fulltext Search
27 # define_index do
28 # indexes head.translations.title
29 # indexes slug
30 # indexes unique_name
31 # indexes head.translations.body
32 # end
33
34 # Class methods 24 # Class methods
35 25
36 # Returns a page for a given node. If no revision is supplied, it returns 26 # Returns a page for a given node. If no revision is supplied, it returns
@@ -254,10 +244,13 @@ class Node < ApplicationRecord
254 # Watch out recursion ahead! update_unique_name itself triggers this 244 # Watch out recursion ahead! update_unique_name itself triggers this
255 # after_save callback which invokes update_unique_name on its children. 245 # after_save callback which invokes update_unique_name on its children.
256 # Hopefully until no childrens occur 246 # Hopefully until no childrens occur
247 #
248 # Queries parent_id directly rather than the NestedTree#children
249 # association out of habit from the old awesome_nested_set-avoidance
250 # workaround - no longer strictly necessary now that children is
251 # equally safe, but left as-is since it already works correctly.
257 def update_unique_names_of_children 252 def update_unique_names_of_children
258 unless root? 253 unless root?
259 # Use parent_id-based traversal instead of lft/rgt descendants
260 # due to awesome_nested_set not refreshing parent lft/rgt in memory
261 Node.where(:parent_id => self.id).each do |child| 254 Node.where(:parent_id => self.id).each do |child|
262 child.reload 255 child.reload
263 child.update_unique_name 256 child.update_unique_name
@@ -265,10 +258,4 @@ class Node < ApplicationRecord
265 end 258 end
266 end 259 end
267 end 260 end
268
269 def borders
270 if lft && rgt && (lft > rgt)
271 errors.add("Fuck!. lft should never be smaller than rgt")
272 end
273 end
274end 261end