Class: HerExtension::Validations::RemoteUniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/her_extension/validations/remote_uniqueness_validation.rb

Overview

Validate the uniqueness of a field by performing a remote API call

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/her_extension/validations/remote_uniqueness_validation.rb', line 9

def validate_each(record,attribute,value)
  list = record.class.where({ attribute => value }).limit(1)
  
  if list.reject { |e| e.id == record.id }.any?
    error_options = options.except(:case_sensitive, :scope, :conditions)
    error_options[:value] = value
    record.errors.add(attribute, :taken, error_options)
  end
end