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
|
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
################
|
|
|
|
# Associations #
|
|
|
|
################
|
|
|
|
|
2019-01-30 22:04:42 -05:00
|
|
|
belongs_to :person, optional: true
|
|
|
|
|
2018-12-01 05:15:27 -05:00
|
|
|
has_many_attached :images
|
2018-11-29 22:02:04 -05:00
|
|
|
|
2018-12-02 15:29:39 -05:00
|
|
|
has_many :passport_maps, dependent: :restrict_with_exception
|
2018-12-02 09:16:16 -05:00
|
|
|
|
2018-11-30 03:28:40 -05:00
|
|
|
has_many :passport_confirmations, dependent: :restrict_with_exception
|
|
|
|
|
2018-12-02 17:20:16 -05:00
|
|
|
accepts_nested_attributes_for :passport_maps, reject_if: :blank_passport_map?
|
2018-12-02 09:41:48 -05:00
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###############
|
|
|
|
# Validations #
|
|
|
|
###############
|
|
|
|
|
2018-12-01 12:18:37 -05:00
|
|
|
validates :confirmed,
|
|
|
|
inclusion: { in: [false], unless: :enough_confirmations? }
|
|
|
|
|
2019-03-25 10:07:58 -04:00
|
|
|
validates :images, passport_image: true
|
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
###########
|
|
|
|
# Methods #
|
|
|
|
###########
|
|
|
|
|
2018-12-02 15:29:39 -05:00
|
|
|
def passport_map
|
|
|
|
passport_maps.order(created_at: :asc).last
|
|
|
|
end
|
|
|
|
|
2018-12-01 05:23:58 -05:00
|
|
|
def image
|
2018-12-02 15:29:39 -05:00
|
|
|
images.order(created_at: :asc).last
|
2018-12-01 05:23:58 -05:00
|
|
|
end
|
|
|
|
|
2018-12-02 15:51:10 -05:00
|
|
|
def can_have_confirmations?
|
|
|
|
passport_map && image
|
|
|
|
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-12-02 17:20:16 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def blank_passport_map?(passport_map_attributes)
|
|
|
|
passport_map_attributes.all? do |key, value|
|
|
|
|
next true if key.start_with? 'date_'
|
|
|
|
|
|
|
|
value.blank?
|
|
|
|
end
|
|
|
|
end
|
2018-11-29 20:19:38 -05:00
|
|
|
end
|