Add RemoteTranslation validations
- Validate that locale is a valid locale for RemoteTranslation Client. - RemoteTranslation can only be created for resources that do not have the requested language translated
This commit is contained in:
@@ -4,7 +4,8 @@ class RemoteTranslation < ApplicationRecord
|
||||
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
|
||||
@@ -17,4 +18,10 @@ class RemoteTranslation < ApplicationRecord
|
||||
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
|
||||
|
||||
@@ -548,6 +548,10 @@ en:
|
||||
attributes:
|
||||
valuation:
|
||||
cannot_comment_valuation: "You cannot comment a valuation"
|
||||
remote_translation:
|
||||
attributes:
|
||||
locale:
|
||||
already_translated: Already translated resource
|
||||
messages:
|
||||
translations_too_short: Is mandatory to provide one translation at least
|
||||
record_invalid: "Validation failed: %{errors}"
|
||||
|
||||
@@ -550,6 +550,10 @@ es:
|
||||
attributes:
|
||||
valuation:
|
||||
cannot_comment_valuation: "No puedes comentar una evaluación"
|
||||
remote_translation:
|
||||
attributes:
|
||||
locale:
|
||||
already_translated: Recurso ya traducido
|
||||
messages:
|
||||
translations_too_short: El obligatorio proporcionar una traducción como mínimo
|
||||
record_invalid: "Error de validación: %{errors}"
|
||||
|
||||
@@ -5,7 +5,7 @@ describe "Remote Translations" do
|
||||
Setting["feature.remote_translations"] = true
|
||||
create(:proposal)
|
||||
available_locales_response = %w[de en es fr pt zh-Hans]
|
||||
expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).
|
||||
expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).at_most(2).times.
|
||||
and_return(available_locales_response)
|
||||
allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123")
|
||||
end
|
||||
|
||||
@@ -27,6 +27,16 @@ describe RemoteTranslation do
|
||||
expect(remote_translation).not_to be_valid
|
||||
end
|
||||
|
||||
it "is not valid without an available_locales" do
|
||||
remote_translation.locale = "unavailable_locale"
|
||||
expect(remote_translation).not_to be_valid
|
||||
end
|
||||
|
||||
it "is not valid when exists a translation for locale" do
|
||||
remote_translation.locale = :en
|
||||
expect(remote_translation).not_to be_valid
|
||||
end
|
||||
|
||||
describe "#enqueue_remote_translation", :delay_jobs do
|
||||
it "after create enqueue Delayed Job" do
|
||||
expect { remote_translation.save }.to change { Delayed::Job.count }.by(1)
|
||||
|
||||
@@ -10,7 +10,7 @@ shared_examples "remotely_translatable" do |factory_name, path_name, path_argume
|
||||
before do
|
||||
Setting["feature.remote_translations"] = true
|
||||
available_locales_response = %w[de en es fr pt zh-Hans]
|
||||
expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).at_most(3).times.
|
||||
expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).at_most(4).times.
|
||||
and_return(available_locales_response)
|
||||
allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user