Skip to content
Snippets Groups Projects

Add before save callback to truncate email if > 255 chars

Merged Rajendra Kadam requested to merge 409653-update-email-truncation into master
All threads resolved!
2 files
+ 25
5
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -7,11 +7,10 @@ class NoteMetadata < ApplicationRecord
EMAIL_PARTICIPANT_LENGTH = 255
belongs_to :note, inverse_of: :note_metadata
validates :email_participant, length: { maximum: EMAIL_PARTICIPANT_LENGTH }
alias_attribute :external_author, :email_participant
before_validation :ensure_email_participant_length
before_save :ensure_email_participant_length, if: ->(note_metadata) { email_participant && email_participant.length > EMAIL_PARTICIPANT_LENGTH }
private
@@ -20,7 +19,9 @@ def ensure_email_participant_length
email_prefix, email_suffix = email_participant.split('@')
allowed_prefix_length = EMAIL_PARTICIPANT_LENGTH - email_suffix.length - 1 # 1 is for '@'
email_suffix_length = email_suffix ? email_suffix.length : 0
allowed_prefix_length = EMAIL_PARTICIPANT_LENGTH - email_suffix_length - 1 # 1 is for '@'
self.email_participant = "#{email_prefix[0...allowed_prefix_length]}@#{email_suffix}"
end
Loading