Files
nairobi/app/models/remote_translation.rb
Javi Martín e7fcca9b47 Check remote translations locales at runtime
We were evaluating its value when the server starts. On production
enviroments, that could mean weeks or months before the available
locales are checked again, and so it would be possible to use a list
which is no longer in sync with the list provided by microsoft.
2020-04-28 15:45:32 +02:00

28 lines
1017 B
Ruby

class RemoteTranslation < ApplicationRecord
belongs_to :remote_translatable, polymorphic: true
validates :remote_translatable_id, presence: true
validates :remote_translatable_type, presence: true
validates :locale, presence: true
validates :locale, inclusion: { in: ->(_) { RemoteTranslations::Microsoft::AvailableLocales.available_locales }}
validate :already_translated_resource
after_create :enqueue_remote_translation
def enqueue_remote_translation
RemoteTranslations::Caller.new(self).delay.call
end
def self.remote_translation_enqueued?(remote_translation)
where(remote_translatable_id: remote_translation["remote_translatable_id"],
remote_translatable_type: remote_translation["remote_translatable_type"],
locale: remote_translation["locale"],
error_message: nil).any?
end
def already_translated_resource
if remote_translatable&.translations&.where(locale: locale).present?
errors.add(:locale, :already_translated)
end
end
end