Class: Categoryz3::Validators::ParentCategoryValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/categoryz3/validators/parent_category_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/categoryz3/validators/parent_category_validator.rb', line 4

def validate(record)
  if record.parent_id && record.parent.path.map{ |category| category.id }.include?(record.id)
    record.errors[:parent] << "Parent cycle dependency"
  end
  if record.id && record.parent_id == record.id
    record.errors[:parent] << "Can't be parent of itself!"
  end
end