diff --git a/app/models/remote_translation.rb b/app/models/remote_translation.rb index 07be39193..51851267d 100644 --- a/app/models/remote_translation.rb +++ b/app/models/remote_translation.rb @@ -9,6 +9,7 @@ class RemoteTranslation < ApplicationRecord after_create :enqueue_remote_translation def enqueue_remote_translation + RemoteTranslationsCaller.new(self).delay.call end def self.remote_translation_enqueued?(remote_translation) diff --git a/lib/remote_translations_caller.rb b/lib/remote_translations_caller.rb new file mode 100644 index 000000000..8cc2da961 --- /dev/null +++ b/lib/remote_translations_caller.rb @@ -0,0 +1,49 @@ +class RemoteTranslationsCaller + attr_reader :remote_translation + + def initialize(remote_translation) + @remote_translation = remote_translation + end + + def call + update_resource + destroy_remote_translation + end + + private + + def update_resource + Globalize.with_locale(locale) do + resource.translated_attribute_names.each_with_index do |field, index| + resource.send(:"#{field}=", translations[index]) + end + end + resource.save + end + + def destroy_remote_translation + if resource.valid? + remote_translation.destroy + else + remote_translation.update(error_message: resource.errors.messages) + end + end + + def resource + @resource ||= remote_translation.remote_translatable + end + + def translations + @translations ||= MicrosoftTranslateClient.new.call(fields_values, locale) + end + + def fields_values + resource.translated_attribute_names.map do |field| + resource.send(field) + end + end + + def locale + remote_translation.locale + end +end diff --git a/spec/lib/remote_translations_caller_spec.rb b/spec/lib/remote_translations_caller_spec.rb new file mode 100644 index 000000000..796fac6d2 --- /dev/null +++ b/spec/lib/remote_translations_caller_spec.rb @@ -0,0 +1,206 @@ +require 'rails_helper' + +describe RemoteTranslationsCaller do + + before do + RemoteTranslation.skip_callback(:create, :after, :enqueue_remote_translation) + end + + describe '#call' do + + context 'Debates' do + + let(:debate) { create(:debate) } + let(:remote_translation) { create(:remote_translation, + remote_translatable: debate, locale: :es) } + let(:remote_translation_caller) { described_class.new(remote_translation) } + + it 'returns the resource with new translation persisted' do + microsoft_translate_client_response = ["Título traducido", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(debate.translations.count).to eq(2) + end + + it "when new translation locale is distinct to default_locale skip length validations" do + microsoft_translate_client_response = ["TT", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to eq nil + expect(debate.translations.count).to eq(2) + expect(debate.valid?).to eq true + end + + it "when new translation locale is distinct to default_locale not skip presence validations" do + microsoft_translate_client_response = ["", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to include("can't be blank") + expect(debate.translations.count).to eq(1) + expect(debate.valid?).to eq false + end + + it "destroy remote translation instance" do + microsoft_translate_client_response = ["Título traducido", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(RemoteTranslation.count).to eq(0) + end + end + + context 'Proposals' do + + let!(:proposal) { create(:proposal) } + let(:remote_translation) { create(:remote_translation, + remote_translatable: proposal, locale: :es) } + let(:remote_translation_caller) { described_class.new(remote_translation) } + + it 'returns the resource with new translation persisted' do + microsoft_translate_client_response = ["Título traducido", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(proposal.translations.count).to eq(2) + end + + it "when new translation locale is distinct to default_locale skip lenght validations" do + microsoft_translate_client_response = ["TT", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to eq nil + expect(proposal.translations.count).to eq(2) + expect(proposal.valid?).to eq true + end + + it "when new translation locale is distinct to default_locale do not skip presence validations" do + microsoft_translate_client_response = ["", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to include("can't be blank") + expect(proposal.translations.count).to eq(1) + expect(proposal.valid?).to eq false + end + + it "destroy remote translation instance" do + microsoft_translate_client_response = ["Título traducido", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(RemoteTranslation.count).to eq(0) + end + end + + context 'Budget Investments' do + + let(:budget_investment) { create(:budget_investment) } + let(:remote_translation) { create(:remote_translation, + remote_translatable: budget_investment, + locale: :es) } + let(:remote_translation_caller) { described_class.new(remote_translation) } + + it 'returns the resource with new translation persisted' do + microsoft_translate_client_response = ["Título traducido", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(budget_investment.translations.count).to eq(2) + end + + it "when new translation locale is distinct to default_locale skip lenght validations" do + microsoft_translate_client_response = ["TT", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to eq nil + expect(budget_investment.translations.count).to eq(2) + expect(budget_investment.valid?).to eq true + end + + it "when new translation locale is distinct to default_locale not skip presence validations" do + microsoft_translate_client_response = ["", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to include("can't be blank") + expect(budget_investment.translations.count).to eq(1) + expect(budget_investment.valid?).to eq false + end + + it "destroy remote translation instance" do + microsoft_translate_client_response = ["Título traducido", "Descripción traducida"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(RemoteTranslation.count).to eq(0) + end + end + + context 'Comments' do + + let(:comment) { create(:comment) } + let(:remote_translation) { create(:remote_translation, + remote_translatable: comment, locale: :es) } + let(:remote_translation_caller) { described_class.new(remote_translation) } + + it 'returns the resource with new translation persisted' do + microsoft_translate_client_response = ["Body traducido"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(comment.translations.count).to eq(2) + end + + it "when new translation locale is distinct to default_locale skip lenght validations" do + microsoft_translate_client_response = ["BT"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to eq nil + expect(comment.translations.count).to eq(2) + expect(comment.valid?).to eq true + end + + it "when new translation locale is distinct to default_locale not skip presence validations" do + microsoft_translate_client_response = [""] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(remote_translation.error_message).to include("can't be blank") + expect(comment.translations.count).to eq(1) + expect(comment.valid?).to eq false + end + + it "destroy remote translation instance" do + microsoft_translate_client_response = ["Body traducido"] + expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(microsoft_translate_client_response) + + remote_translation_caller.call + + expect(RemoteTranslation.count).to eq(0) + end + end + + end + +end diff --git a/spec/models/remote_translation_spec.rb b/spec/models/remote_translation_spec.rb index e1a8e3616..c2707fd0b 100644 --- a/spec/models/remote_translation_spec.rb +++ b/spec/models/remote_translation_spec.rb @@ -27,6 +27,17 @@ describe RemoteTranslation do remote_translation.remote_translatable_type = nil expect(remote_translation).not_to be_valid end + + describe '#enqueue_remote_translation' do + + it 'after create enqueue Delayed Job' do + Delayed::Worker.delay_jobs = true + + expect { remote_translation.save }.to change { Delayed::Job.count }.by(1) + + Delayed::Worker.delay_jobs = false + end + end end