From 9f878f219cedbbc5284829244cbb6b37b68baf0d Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Tue, 7 Oct 2014 17:21:45 +0300 Subject: [PATCH] Validate identity code on user --- app/models/user.rb | 10 ++++++++++ config/locales/en.yml | 1 + 2 files changed, 11 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 53b4fd40d..ae2ef1a7c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,6 +16,8 @@ class User < ActiveRecord::Base validates :email, presence: true, if: -> { country.iso != 'EE' } validates :registrar, presence: true, if: -> { !admin } + validate :validate_identity_code + before_save -> { self.registrar = nil if admin? } attr_accessor :registrar_typeahead @@ -27,4 +29,12 @@ class User < ActiveRecord::Base def registrar_typeahead @registrar_typeahead || registrar || nil end + + private + + def validate_identity_code + return unless identity_code.present? + code = Isikukood.new(identity_code) + errors.add(:identity_code, :invalid) unless code.valid? + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 2d5145d8c..d159704f2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -170,6 +170,7 @@ en: password: blank: 'Password is missing' identity_code: + invalid: 'Identity code is invalid' blank: 'Identity code is missing' taken: 'Identity code already exists' email: