Class: UniquenessMultipleValidator

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

Overview

Validator check is unique in multiple columns

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
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
# File 'app/validators/uniqueness_multiple_validator.rb', line 3

def validate_each(record, attribute, value)
  # fields need to validate
  fields = [attribute, *options[:scope]].uniq

  fields_value = []
  query_str = []
  fields.each do |field|
    field_value = record.send(field).try(:strip)
    fields_value << field_value if field_value.present?
    query_str << "#{field} IN (:value)"
  end

  return if fields_value.blank?

  shop_url = record.class.where(query_str.join(' OR '), value: fields_value)

  # except current record if it's update
  shop_url = shop_url.where.not(id: record.id) unless record.new_record?

  # check errors
  unless shop_url.size.zero?
    shop_fields = shop_url.limit(1).pluck(*fields).first.select(&:present?).map(&:strip)
    fields.each do |field|
      record.errors.add(field, :uniqueness_multiple) if shop_fields.include? record.send(field).try(:strip)
    end
  end
end