# frozen_string_literal: true
class Passport < ApplicationRecord
REQUIRED_CONFIRMATIONS = 3
belongs_to :person, optional: true
has_many_attached :images
has_many :passport_maps, dependent: :restrict_with_exception
has_many :passport_confirmations, dependent: :restrict_with_exception
accepts_nested_attributes_for :passport_maps, reject_if: :blank_passport_map?
validates :confirmed,
inclusion: { in: [false], unless: :enough_confirmations? }
def passport_map
passport_maps.order(created_at: :asc).last
end
def image
images.order(created_at: :asc).last
def can_have_confirmations?
passport_map && image
def enough_confirmations?
passport_confirmations.count >= REQUIRED_CONFIRMATIONS
private
def blank_passport_map?(passport_map_attributes)
passport_map_attributes.all? do |key, value|
next true if key.start_with? 'date_'
value.blank?