Fix crash translating an already translated text

The page was crashing when at least part of the content of the page had
been translated between the request showing the remote translations
button and the moment people pressed the button.
This commit is contained in:
Javi Martín
2023-01-30 17:07:57 +01:00
parent fe3c9d47fa
commit 2b9f9ed557
2 changed files with 34 additions and 2 deletions

View File

@@ -31,11 +31,11 @@ class RemoteTranslation < ApplicationRecord
def self.create_all(remote_translations_params) def self.create_all(remote_translations_params)
remote_translations_params.map do |remote_translation_params| remote_translations_params.map do |remote_translation_params|
new(remote_translation_params) new(remote_translation_params)
end.reject(&:enqueued?).each(&:save!) end.reject(&:already_translated?).reject(&:enqueued?).each(&:save!)
end end
def already_translated_resource def already_translated_resource
if remote_translatable&.translations&.where(locale: locale).present? if already_translated?
errors.add(:locale, :already_translated) errors.add(:locale, :already_translated)
end end
end end
@@ -45,4 +45,8 @@ class RemoteTranslation < ApplicationRecord
locale: locale, locale: locale,
error_message: nil).any? error_message: nil).any?
end end
def already_translated?
remote_translatable&.translations&.where(locale: locale).present?
end
end end

View File

@@ -1,3 +1,5 @@
require "sessions_helper"
shared_examples "remotely_translatable" do |factory_name, path_name, path_arguments| shared_examples "remotely_translatable" do |factory_name, path_name, path_arguments|
let(:arguments) do let(:arguments) do
path_arguments.transform_values { |path_to_value| resource.send(path_to_value) } path_arguments.transform_values { |path_to_value| resource.send(path_to_value) }
@@ -192,6 +194,32 @@ shared_examples "remotely_translatable" do |factory_name, path_name, path_argume
expect(RemoteTranslation.count).to eq(0) expect(RemoteTranslation.count).to eq(0)
expect(resource.translations.count).to eq(2) expect(resource.translations.count).to eq(2)
end end
scenario "request a translation of an already translated text" do
microsoft_translate_client_response = generate_response(resource)
expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(microsoft_translate_client_response)
in_browser(:one) do
visit path
select "Español", from: "Language:"
expect(page).to have_button "Traducir página"
end
in_browser(:two) do
visit path
select "Español", from: "Language:"
click_button "Traducir página"
expect(page).to have_content "Se han solicitado correctamente las traducciones"
end
in_browser(:one) do
click_button "Traducir página"
expect(page).not_to have_button "Traducir página"
end
end
end end
end end
end end