Class: NoCyclesValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/no_cycles_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
# File 'app/validators/no_cycles_validator.rb', line 2

def validate(record)
  id = record.id
  current = record
  while ! current.parent.nil? && !current.id.nil? && !id.nil? do
    if current.parent.id == id
      record.errors[:nocycle] << 'Creating cyclic ingredient hierarchies is not allowed!'
      break
    end
    current = current.parent
  end
end