internetee/registry

View on GitHub
app/validators/domain_nameserver_validator.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
class DomainNameserverValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    min, max = options[:min].call, options[:max].call
    values = value.reject(&:marked_for_destruction?)

    return true if !Domain.nameserver_required? && values.empty?

    return if values.size.between?(min, max)
    association = options[:association] || attribute
    record.errors.add(association, :out_of_range, **{ min: min, max: max })
  end
end