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
Files
2
@@ -4,9 +4,27 @@ module Notes
class NoteMetadata < ApplicationRecord
self.table_name = :note_metadata
EMAIL_PARTICIPANT_LENGTH = 255
belongs_to :note, inverse_of: :note_metadata
validates :email_participant, length: { maximum: 255 }
alias_attribute :external_author, :email_participant
before_save :ensure_email_participant_length
private
def ensure_email_participant_length
return unless email_participant.present?
return unless email_participant.length > EMAIL_PARTICIPANT_LENGTH
email_prefix, email_suffix = email_participant.split('@')
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
end
end
Loading