iachat/app/models/application_record.rb
Gabriel Jablonski 053e684261
feat(mailer): add i18n support for transactional emails with pt-BR (#255)
* feat(mailer): add i18n support for all transactional emails with pt-BR translations

- Create Liquid i18n filter (LiquidFilters::I18nFilter) exposing I18n.t() as `t` filter
  with interpolation support via positional args
- Replace hardcoded English strings in all 26 Liquid email templates with i18n keys
- Replace hardcoded English strings in all 5 Devise ERB email templates (OSS + enterprise)
  with t() calls, using raw output (<%==) to prevent HTML-encoding of apostrophes
- Translate all mailer subjects to use I18n.t() for locale-aware rendering
- Add comprehensive pt-BR translations for all transactional emails
- Add SSO-specific i18n keys for enterprise confirmation instructions
- Fix ApplicationRecord#to_drop to walk STI hierarchy (e.g. SuperAdmin -> UserDrop)

* fix: address CodeRabbit review feedback

- Use _html suffix for Devise translation keys to prevent XSS while
  keeping apostrophes unencoded (Rails escapes interpolations automatically)
- Replace hardcoded English in SLA templates with full_body i18n keys
- Fix _target="blank" typo to target="_blank" in attachment links
- Add i18n for tiktok_disconnect subject and create liquid template
- Extract brand_name to private method in AccountNotificationMailer

* fix: address round 2 review feedback

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 15:48:52 -03:00

56 lines
1.8 KiB
Ruby

class ApplicationRecord < ActiveRecord::Base
include Events::Types
self.abstract_class = true
before_validation :validates_column_content_length
# the models that exposed in email templates through liquid
def droppables
%w[Account Channel Conversation Inbox User Message]
end
# ModelDrop class should exist in app/drops
# Walks the STI hierarchy so subclasses (e.g. SuperAdmin < User) resolve to the matching Drop
def to_drop
drop_class = self.class.ancestors.find { |k| k.is_a?(Class) && droppables.include?(k.name) }
return unless drop_class
"#{drop_class.name}Drop".constantize.new(self)
end
private
# Generic validation for all columns of type string and text
# Validates the length of the column to prevent DOS via large payloads
# if a custom length validation is already present, skip the validation
def validates_column_content_length
self.class.columns.each do |column|
check_and_validate_content_length(column) if column_of_type_string_or_text?(column)
end
end
def column_of_type_string_or_text?(column)
%i[string text].include?(column.type)
end
def check_and_validate_content_length(column)
length_validator = self.class.validators_on(column.name).find { |v| v.kind == :length }
validate_content_length(column) if length_validator.blank?
end
def validate_content_length(column)
max_length = column.type == :text ? 20_000 : 255
return if self[column.name].nil? || self[column.name].length <= max_length
errors.add(column.name.to_sym, "is too long (maximum is #{max_length} characters)")
end
def normalize_empty_string_to_nil(attrs = [])
attrs.each do |attr|
self[attr] = nil if self[attr].blank?
end
end
end
ApplicationRecord.prepend_mod_with('ApplicationRecord')