1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/passport.rb
2019-07-08 18:40:41 +05:00

61 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class Passport < ApplicationRecord
REQUIRED_CONFIRMATIONS = 3
################
# Associations #
################
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?
###############
# Validations #
###############
validates :confirmed,
inclusion: { in: [false], unless: :enough_confirmations? }
validates :images, passport_image: true
###########
# Methods #
###########
def passport_map
passport_maps.order(created_at: :asc).last
end
def image
images.order(created_at: :asc).last
end
def can_have_confirmations?
passport_map && image
end
def enough_confirmations?
passport_confirmations.count >= REQUIRED_CONFIRMATIONS
end
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
end