From 2b9f9ed557dfa8bf184535bf74047464c9d4aa49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 30 Jan 2023 17:07:57 +0100 Subject: [PATCH] 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. --- app/models/remote_translation.rb | 8 ++++-- spec/shared/system/remotely_translatable.rb | 28 +++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/models/remote_translation.rb b/app/models/remote_translation.rb index b186a9d3a..1ef0f893d 100644 --- a/app/models/remote_translation.rb +++ b/app/models/remote_translation.rb @@ -31,11 +31,11 @@ class RemoteTranslation < ApplicationRecord def self.create_all(remote_translations_params) remote_translations_params.map do |remote_translation_params| new(remote_translation_params) - end.reject(&:enqueued?).each(&:save!) + end.reject(&:already_translated?).reject(&:enqueued?).each(&:save!) end def already_translated_resource - if remote_translatable&.translations&.where(locale: locale).present? + if already_translated? errors.add(:locale, :already_translated) end end @@ -45,4 +45,8 @@ class RemoteTranslation < ApplicationRecord locale: locale, error_message: nil).any? end + + def already_translated? + remote_translatable&.translations&.where(locale: locale).present? + end end diff --git a/spec/shared/system/remotely_translatable.rb b/spec/shared/system/remotely_translatable.rb index c38678e46..1204b8f64 100644 --- a/spec/shared/system/remotely_translatable.rb +++ b/spec/shared/system/remotely_translatable.rb @@ -1,3 +1,5 @@ +require "sessions_helper" + shared_examples "remotely_translatable" do |factory_name, path_name, path_arguments| let(:arguments) do 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(resource.translations.count).to eq(2) 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