2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'app/validators/multimodel_uniqueness_validator.rb', line 2
def validate_each(record, attribute, value)
return unless value
checkvalue = (options[:punctuation_sensitive] == false) ? value.gsub(/[^[:alnum:]]/, "") : value
(options[:check] || {}).each_pair do |model_name, attr|
model = model_name.to_s.classify.constantize
query = if options[:case_sensitive] == false
model.arel_table[attr].lower.eq(checkvalue.downcase)
else
DatabaseDetector.is_mysql? ? model.arel_table[attr].smatches(checkvalue) : model.arel_table[attr].eq(checkvalue)
end
query = query.and(model.arel_table[:id].not_eq(record.id)) if record.instance_of?(model)
record.errors.add(attribute, :taken) if model.unscoped.where(query).exists? end
end
|