Как переназначить ошибку active record в файле локализации?

Предполагается, что на проекте используем paperclip для загрузки фоток на сайт и I18n для интернационализации. В системе N моделей и K языков для локализации.

Далее допустим есть такой концерн:
app/models/concerns/imagable.rb
module Imagable
  extend ActiveSupport::Concern
  included do
   has_attached_file :image #в базе данных есть поле image, которое подвергается валидации
... many great methods are here
  end
end


В моделях model_1, model_2,... model_N
class Model_i < ApplicationRecord
include Imagable
end


В файлах языков config/locales: lang_1, lang_2, ... lang_K
lang_j:
  active_record:
    model_1:
      attributes:
        image_content_type:
          invalid: "image content is invalid" 
    model_2:
      attributes:
        image_content_type:
          invalid: "image content is invalid"
...
    model_N:
      attributes:
        image_content_type:
          invalid: "image content is invalid"


При N и K 10 и 3 ручной работы как-то уже дофига.
Хочу что -то типа:
module Imagable
  extend ActiveSupport::Concern
  included do
   ...
    active_record.error_messages.config.reassign field: :image, message: 'invalid_image_content_common'
  end
end


lang_j:
  active_record:
    invalid_image_content_common: "image content is invalid"
    model_1:
      attributes:
...


Я не знаю этой фишки и есть ли она? Но согласитесь, было бы удобно
  • Вопрос задан
  • 16 просмотров
Решения вопроса 1
@sunnmas Автор вопроса
Ruby
config/initializers/attachment_content_type_validator.rb
class Paperclip::Validators::AttachmentContentTypeValidator
  def mark_invalid(record, attribute, types)
      record.errors.add(attribute, :invalid, message: I18n.t('paperclip.file_content_type_invalid'))
  end
end


lang_j:
  paperclip:
    file_content_type_invalid: "File content invalid"
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы