Create Remote Translations Caller
This module is used in a callback model and in charge of
- extracting resources associated from RemoteTranslation and preparing
its fields to be sent to the MicrosoftTranslateClient thought DelayedJobs
- receive the response from MicrosoftTranslateClient and update resource
with his translates
Co-authored-by: javierm <elretirao@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
49
lib/remote_translations_caller.rb
Normal file
49
lib/remote_translations_caller.rb
Normal file
@@ -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
|
||||
206
spec/lib/remote_translations_caller_spec.rb
Normal file
206
spec/lib/remote_translations_caller_spec.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user