Class: MultimodelUniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/multimodel_uniqueness_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



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|
    # Get class constant
    model = model_name.to_s.classify.constantize
    # Work out field to query
    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)
    # Run the check
    record.errors.add(attribute, :taken) if model.unscoped.where(query).exists? # rubocop:disable Pundit/AvoidUnscoped
  end
end