Class: RnValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/rn/validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rn/validator.rb', line 5

def validate_each(record, attribute, value)
  check_birthday = options.fetch(:birthday, false)
  birthday_accessor = options.fetch(:birthday_column, :birthday)

  begin
    number = Rn::RegistrationNumber.new(value)

    record.errors.add(attribute, :invalid_registration_number, options) unless number.valid?

    if check_birthday
      birthday = record.send(birthday_accessor)
      record.errors.add(attribute, :no_matching_birthday, options) if number.birthday != birthday
    end
  rescue ArgumentError => e
    record.errors.add(attribute, e.message, options)
  end
end