Class: NotNaughty::UniquenessValidation

Inherits:
Validation
  • Object
show all
Defined in:
lib/not_naughty/validations/sequel/uniqueness_validation.rb

Constant Summary

Constants inherited from Validation

Validation::BASEDIR, Validation::PATTERN

Instance Attribute Summary

Attributes inherited from Validation

#attributes

Instance Method Summary collapse

Methods inherited from Validation

#call_with_conditions, #call_without_conditions, inherited, load, new

Constructor Details

#initialize(opts, attributes) ⇒ UniquenessValidation

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/not_naughty/validations/sequel/uniqueness_validation.rb', line 5

def initialize(opts, attributes) #:nodoc:
  __message = opts[:message] || '#{"%s".humanize} is not unique.'
  
  if opts[:allow_blank] or opts[:allow_nil]
    __allow = if opts[:allow_blank] then :blank? else :nil? end
    super opts, attributes do |obj, attr, val|
      obj.errors.add attr, __message unless
        v.send! __allow or
        obj.model.find(attr => val).limit(1).length.zero?
    end
  else
    super opts, attributes do |obj, attr, val|
      obj.errors.add attr, __message unless
        obj.model.find(attr => val).limit(1).length.zero?
    end
  end
end