diff --git a/app/models/tag.rb b/app/models/tag.rb index e002bf62780f935082102e24fdfaa689d1354021..d9daa475dc5697dac6abafe3cc9daae105ffd7ab 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