note_length_validator.rb 667 B

12345678910111213141516171819202122
  1. # frozen_string_literal: true
  2. class NoteLengthValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. record.errors.add(attribute, :too_long, message: I18n.t('statuses.over_character_limit', max: options[:maximum]), count: options[:maximum]) if too_long?(value)
  5. end
  6. private
  7. def too_long?(value)
  8. countable_text(value).mb_chars.grapheme_length > options[:maximum]
  9. end
  10. def countable_text(value)
  11. return '' if value.nil?
  12. value.dup.tap do |new_text|
  13. new_text.gsub!(FetchLinkCardService::URL_PATTERN, StatusLengthValidator::URL_PLACEHOLDER)
  14. new_text.gsub!(Account::MENTION_RE, '@\2')
  15. end
  16. end
  17. end