Class: ActsAsParanoid::Validations::UniquenessWithoutDeletedValidator::V5

Inherits:
ActiveRecord::Validations::UniquenessValidator
  • Object
show all
Defined in:
lib/acts_as_paranoid/validations.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_paranoid/validations.rb', line 20

def validate_each(record, attribute, value)
  finder_class = find_finder_class_for(record)
  table = finder_class.arel_table

  coder = record.class.attribute_types[attribute.to_s]
  value = coder.type_cast_for_schema value if value && coder

  relation = build_relation(finder_class, table, attribute, value)
  [Array(finder_class.primary_key), Array(record.send(:id))].transpose.each do |pk_key, pk_value|
    relation = relation.where(table[pk_key.to_sym].not_eq(pk_value))
  end if record.persisted?

  Array.wrap(options[:scope]).each do |scope_item|
    relation = relation.where(table[scope_item].eq(record.public_send(scope_item)))
  end

  if relation.where(finder_class.paranoid_default_scope).where(relation).exists?
    record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
  end
end