1
0
Fork 0
This repository has been archived on 2023-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/passport.rb

43 lines
918 B
Ruby

# frozen_string_literal: true
class Passport < ApplicationRecord
REQUIRED_CONFIRMATIONS = 3
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
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