Class: Quiver::Validators::Unique

Inherits:
Base
  • Object
show all
Defined in:
lib/quiver/validators/unique.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Quiver::Validators::Base

Instance Method Details

#validateObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/quiver/validators/unique.rb', line 6

def validate
  errors = Quiver::ErrorCollection.new

  result = mapper.send(:query,
    filter: {
      name => {'eq' => value}
    }
  )

  if result.object.any?
    unless result.object.size == 1 &&
      result.object.first.send(adapter.class.primary_key_name) == model.send(adapter.class.primary_key_name)

      errors << Quiver::Model::ValidationError.new(name, must_be_unique)
    end
  end

  errors
end