diff --git a/app/models/remote_translation.rb b/app/models/remote_translation.rb index e9ef75a1c..85fc77bdd 100644 --- a/app/models/remote_translation.rb +++ b/app/models/remote_translation.rb @@ -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 diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index aae364b39..d30a8785b 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -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}" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index 5a3ed55cf..ceef5bb12 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -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}" diff --git a/spec/features/remote_translations_spec.rb b/spec/features/remote_translations_spec.rb index c144a406a..7f4e1bb92 100644 --- a/spec/features/remote_translations_spec.rb +++ b/spec/features/remote_translations_spec.rb @@ -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 diff --git a/spec/models/remote_translation_spec.rb b/spec/models/remote_translation_spec.rb index 0c6723a0e..399952939 100644 --- a/spec/models/remote_translation_spec.rb +++ b/spec/models/remote_translation_spec.rb @@ -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) diff --git a/spec/shared/features/remotely_translatable.rb b/spec/shared/features/remotely_translatable.rb index d7acff59f..478c9a049 100644 --- a/spec/shared/features/remotely_translatable.rb +++ b/spec/shared/features/remotely_translatable.rb @@ -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