2018-11-29 20:19:38 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Passport < ApplicationRecord
|
2018-11-30 04:23:46 -05:00
|
|
|
REQUIRED_CONFIRMATIONS = 3
|
|
|
|
|
2018-11-29 21:46:38 -05:00
|
|
|
enum sex: %i[male female]
|
2018-11-29 20:19:38 -05:00
|
|
|
|
2018-12-01 05:15:27 -05:00
|
|
|
has_many_attached :images
|
2018-11-29 22:02:04 -05:00
|
|
|
|
2018-11-30 03:28:40 -05:00
|
|
|
has_many :passport_confirmations, dependent: :restrict_with_exception
|
|
|
|
|
2018-12-01 12:18:37 -05:00
|
|
|
validates :confirmed,
|
|
|
|
inclusion: { in: [false], unless: :enough_confirmations? }
|
|
|
|
|
2018-11-29 20:19:38 -05:00
|
|
|
validates :surname, presence: true
|
|
|
|
validates :given_name, presence: true
|
|
|
|
validates :sex, presence: true
|
|
|
|
validates :date_of_birth, presence: true
|
|
|
|
validates :place_of_birth, presence: true
|
|
|
|
validates :series, presence: true
|
|
|
|
validates :number, presence: true
|
|
|
|
validates :issued_by, presence: true
|
|
|
|
validates :unit_code, presence: true
|
|
|
|
validates :date_of_issue, presence: true
|
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.patronymic = nil if patronymic.blank?
|
|
|
|
end
|
2018-11-30 04:23:46 -05:00
|
|
|
|
2018-12-01 05:23:58 -05:00
|
|
|
def image
|
|
|
|
images.order(created_at: :desc).last
|
|
|
|
end
|
|
|
|
|
2018-11-30 19:40:34 -05:00
|
|
|
def enough_confirmations?
|
|
|
|
passport_confirmations.count >= REQUIRED_CONFIRMATIONS
|
2018-11-30 04:23:46 -05:00
|
|
|
end
|
2018-11-29 20:19:38 -05:00
|
|
|
end
|