Class: UniqRunValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/run_cl/uniq_run_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
# File 'lib/run_cl/uniq_run_validator.rb', line 2

def validate_each(record, attribute, value)
  run = Run.remove_format value

  matchers = []
  if record.new_record?
    matchers = record.class.where(attribute => run)
  else
    matchers = record.class.where('? = ? AND id != ?', attribute, run, record.id)
  end

  if options[:scope]
    matchers = matchers.where(options[:scope] => record.send(options[:scope]))
  end

  record.errors[attribute.to_sym] << (options[:message] || I18n.t(:uniq_run, scope: [:errors, :messages])) if matchers.any?
end