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:
taitus
2020-01-23 15:55:24 +01:00
parent 086e38c969
commit d853366d38
6 changed files with 28 additions and 3 deletions

View File

@@ -4,7 +4,8 @@ class RemoteTranslation < ApplicationRecord
validates :remote_translatable_id, presence: true validates :remote_translatable_id, presence: true
validates :remote_translatable_type, presence: true validates :remote_translatable_type, presence: true
validates :locale, presence: true validates :locale, presence: true
validates :locale, inclusion: { in: RemoteTranslations::Microsoft::AvailableLocales.available_locales }
validate :already_translated_resource
after_create :enqueue_remote_translation after_create :enqueue_remote_translation
def enqueue_remote_translation def enqueue_remote_translation
@@ -17,4 +18,10 @@ class RemoteTranslation < ApplicationRecord
locale: remote_translation["locale"], locale: remote_translation["locale"],
error_message: nil).any? error_message: nil).any?
end end
def already_translated_resource
if remote_translatable&.translations&.where(locale: locale).present?
errors.add(:locale, :already_translated)
end
end
end end

View File

@@ -548,6 +548,10 @@ en:
attributes: attributes:
valuation: valuation:
cannot_comment_valuation: "You cannot comment a valuation" cannot_comment_valuation: "You cannot comment a valuation"
remote_translation:
attributes:
locale:
already_translated: Already translated resource
messages: messages:
translations_too_short: Is mandatory to provide one translation at least translations_too_short: Is mandatory to provide one translation at least
record_invalid: "Validation failed: %{errors}" record_invalid: "Validation failed: %{errors}"

View File

@@ -550,6 +550,10 @@ es:
attributes: attributes:
valuation: valuation:
cannot_comment_valuation: "No puedes comentar una evaluación" cannot_comment_valuation: "No puedes comentar una evaluación"
remote_translation:
attributes:
locale:
already_translated: Recurso ya traducido
messages: messages:
translations_too_short: El obligatorio proporcionar una traducción como mínimo translations_too_short: El obligatorio proporcionar una traducción como mínimo
record_invalid: "Error de validación: %{errors}" record_invalid: "Error de validación: %{errors}"

View File

@@ -5,7 +5,7 @@ describe "Remote Translations" do
Setting["feature.remote_translations"] = true Setting["feature.remote_translations"] = true
create(:proposal) create(:proposal)
available_locales_response = %w[de en es fr pt zh-Hans] 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) and_return(available_locales_response)
allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123") allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123")
end end

View File

@@ -27,6 +27,16 @@ describe RemoteTranslation do
expect(remote_translation).not_to be_valid expect(remote_translation).not_to be_valid
end 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 describe "#enqueue_remote_translation", :delay_jobs do
it "after create enqueue Delayed Job" do it "after create enqueue Delayed Job" do
expect { remote_translation.save }.to change { Delayed::Job.count }.by(1) expect { remote_translation.save }.to change { Delayed::Job.count }.by(1)

View File

@@ -10,7 +10,7 @@ shared_examples "remotely_translatable" do |factory_name, path_name, path_argume
before do before do
Setting["feature.remote_translations"] = true Setting["feature.remote_translations"] = true
available_locales_response = %w[de en es fr pt zh-Hans] 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) and_return(available_locales_response)
allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123") allow(Rails.application.secrets).to receive(:microsoft_api_key).and_return("123")
end end