demarches-normaliennes/app/models/concerns/email_sanitizable_concern.rb
2024-08-22 09:26:48 +02:00

38 lines
1.2 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
module EmailSanitizableConcern
extend ActiveSupport::Concern
def sanitize_email(attribute)
value_to_sanitize = self.send(attribute)
if value_to_sanitize.present?
self[attribute] = EmailSanitizer.sanitize(value_to_sanitize)
end
end
def generate_emails_suggestions_message(suggestions)
return if suggestions.empty?
typo_list = suggestions.map(&:first).join(', ')
verification_link = view_context.link_to("vérifier lorthographe", "#maybe_typos_errors")
"Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{typo_list}. Veuillez #{verification_link} des invitations."
end
def check_if_typo(emails)
emails = emails.map { EmailSanitizer.sanitize(_1) }
@maybe_typos, no_suggestions = emails
.map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] }
.partition { _1[1].present? }
emails = no_suggestions.map(&:first)
emails << EmailSanitizer.sanitize(params['final_email']) if params['final_email'].present?
emails
end
class EmailSanitizer
def self.sanitize(value)
value.gsub(/[[:space:]]/, ' ').strip.downcase
end
end
end