class ActiveModel::Validations::AllowedSubdomainValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/reserved_subdomains.rb, line 9
def validate_each(record, attribute, value)
  reserved = exists_in_reserved_list(value)
  regexp = matches_regexp_list(value)

  if value.present? && ( reserved || regexp)
    record.errors.add(attribute, :invalid)
  end
end

Private Instance Methods

exists_in_reserved_list(value) click to toggle source
# File lib/reserved_subdomains.rb, line 25
def exists_in_reserved_list(value)
  list = load_list('reserved_subdomains_list.yml')
  list.include?(value)
end
load_list(file) click to toggle source
# File lib/reserved_subdomains.rb, line 20
def load_list(file)
  shared_path = File.dirname(__FILE__)
  YAML.load_file(File.join(shared_path, file))
end
matches_regexp_list(value) click to toggle source
# File lib/reserved_subdomains.rb, line 30
def matches_regexp_list(value)
  list = load_list('regexp_list.yml')

  list.each do |regexp|
    return true if Regexp.new(regexp).match(value)
  end

  return false
end