From 9e686fb7394e80a46fbe679dd51442302734f706 Mon Sep 17 00:00:00 2001
From: ArtOfCode- <hello@artofcode.co.uk>
Date: Tue, 30 Jun 2020 15:53:56 +0100
Subject: [PATCH] Disallow self-parenting

---
 app/models/tag.rb | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/app/models/tag.rb b/app/models/tag.rb
index e002bf627..d9daa475d 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -8,6 +8,7 @@ class Tag < ApplicationRecord
 
   validates :excerpt, length: { maximum: 600 }, allow_blank: true
   validates :wiki_markdown, length: { maximum: 30000 }, allow_blank: true
+  validate :parent_not_self
   validate :parent_not_own_child
 
   def self.search(term)
@@ -23,6 +24,13 @@ class Tag < ApplicationRecord
 
   private
 
+  def parent_not_self
+    return unless parent_id.present?
+    if parent_id == id
+      errors.add(:base, 'A tag cannot be its own parent.')
+    end
+  end
+
   def parent_not_own_child
     return unless parent_id.present?
     if all_children.include? parent_id
-- 
GitLab