You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

38 lines
820 B

  1. class Category < ApplicationRecord
  2. has_many :documents
  3. has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id'
  4. belongs_to :parent, class_name: 'Category', optional: true
  5. belongs_to :user
  6. before_destroy :check_for_subcategories, :check_for_documents
  7. validates :name, presence: true
  8. def get_fqcn
  9. fqcn = name
  10. cat_parent = parent
  11. while cat_parent != nil do
  12. fqcn = cat_parent.name + "/" + fqcn
  13. cat_parent = cat_parent.parent
  14. end
  15. fqcn
  16. end
  17. private
  18. def check_for_subcategories
  19. unless subcategories.empty?
  20. errors.add(:base, 'Subcategories present')
  21. throw :abort
  22. end
  23. end
  24. def check_for_documents
  25. unless documents.empty?
  26. errors.add(:base, 'Documents present')
  27. throw :abort
  28. end
  29. end
  30. end