4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/paranoia_uniqueness_validator/validations/uniqueness_without_deleted.rb', line 4
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
table = finder_class.arel_table
if ActiveRecord.version.to_s >= '4.2.0'
value = map_enum_attribute(finder_class, attribute, value)
else
value = deserialize_attribute(record, attribute, value)
end
relation = build_relation(finder_class, table, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted?
default_sentinel_value = Object.const_defined?('Paranoia') ? Paranoia.default_sentinel_value : nil
relation = relation.and(table[:deleted_at].eq(default_sentinel_value))
relation = scope_relation(record, table, relation)
relation = finder_class.unscoped.where(relation)
relation = relation.merge(options[:conditions]) if options[:conditions]
if relation.exists?
error_options = options.except(:case_sensitive, :scope, :conditions)
error_options[:value] = value
record.errors.add(attribute, :taken, error_options)
end
end
|