Sha256: 3aa070ec73bcff7802bcced19a38ae5593674c3fdf126c75ce48b3bd6e005c7a
Contents?: true
Size: 1.76 KB
Versions: 2
Compression:
Stored size: 1.76 KB
Contents
# frozen_string_literal: true module MrCommon # When MrCommon.enable_pre_registration is true newly created Registrations # will be checked against existing PreRegistrations. If a PreRegistration is # found the Registration will be confirmed automatically. # # A PreRegistration requires a first+last name OR an email to be valid. class PreRegistration < ApplicationRecord validates :email, presence: true, format: MrCommon::Pattern.email, if: :should_validate_email? validates :first_name, presence: true, if: :should_validate_name? validates :last_name, presence: true, if: :should_validate_name? validates :normalized_name, presence: true, if: :should_validate_name? before_validation :normalize_email before_validation :set_normalized_name def self.exists_for?(registration) email = registration.email&.downcase name = normalized_name_from(registration).presence exists_for_email = email.present? ? exists?(email: email) : false exists_for_name = name.present? ? exists?(normalized_name: name) : false exists_for_email || exists_for_name end private def self.normalized_name_from(registration_or_self) first = registration_or_self.first_name.downcase.gsub(/\W+/, "-") last = registration_or_self.last_name.downcase.gsub(/\W+/, "-") spacer = first.present? && last.present? ? "-" : "" "#{first}#{spacer}#{last}" end def should_validate_email? first_name.blank? && last_name.blank? end def should_validate_name? email.blank? end def set_normalized_name self.normalized_name = PreRegistration.normalized_name_from(self) end def normalize_email self.email = email.downcase end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mr_common-2.1.0 | app/models/mr_common/pre_registration.rb |
mr_common-2.0.0 | app/models/mr_common/pre_registration.rb |