Sha256: f9387f8abcf243030478f735c1ca700e162ce5a73b8c930e15116dea0c25f0a7
Contents?: true
Size: 1.75 KB
Versions: 1
Compression:
Stored size: 1.75 KB
Contents
class HaystackTag < ApplicationRecord belongs_to :parent_tag, class_name: "NeedleInAHaystack::HaystackTag", optional: true has_many :children, class_name: "NeedleInAHaystack::HaystackTag", foreign_key: "parent_tag_id", dependent: :destroy, inverse_of: :parent_tag has_many :haystack_taggings, dependent: :destroy has_many :taggables, through: :haystack_taggings if NeedleInAHaystack.respond_to?(:configuration) && NeedleInAHaystack.configuration&.taggable_models NeedleInAHaystack.configuration.taggable_models.each do |model| has_many "taggable_#{model.name.underscore.pluralize}".to_sym, through: :haystack_taggings, source: :taggable, source_type: model.name end end validates :name, presence: true validates :description, presence: true validate :prevent_circular_reference def ancestors ancestors = [] current_tag = self while current_tag.parent_tag ancestors << current_tag.parent_tag current_tag = current_tag.parent_tag end ancestors end def self.find_by_path(path) keys = path.split(".") current = nil keys.each do |key| current = current ? current.child_tags.find_by(name: key) : find_by(name: key) return nil unless current end current end def descendants children = child_tags children + children.flat_map(&:descendants) end def siblings parent_tag ? parent_tag.child_tags.where.not(id: id) : self.class.where(parent_tag_id: nil).where.not(id: id) end def root? parent_tag.nil? end def leaf? child_tags.empty? end def depth ancestors.size end def prevent_circular_reference return unless parent_tag == self || ancestors.include?(self) errors.add(:parent_tag, "kan geen circulaire referentie bevatten") end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
needle_in_a_haystack-1.0.1 | lib/needle_in_a_haystack/haystack_tag.rb |