Create RemoteTranslations Controller

- Create RemoteTranslations Controller to receive resources without
  translations and create RemoteTranslation instances when theirs
  translations are not enqueued.
- Create remote_translation_enqueued? class method on RemoteTranslation
  model to check if exists same remote translations without errors
  pending to translate.
This commit is contained in:
taitus
2019-01-25 15:22:33 +01:00
committed by voodoorai2000
parent 04810f5080
commit 744a3d48fd
6 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
class RemoteTranslationsController < ApplicationController
skip_authorization_check
respond_to :html, :js
before_action :set_remote_translations, only: :create
def create
@remote_translations.each do |remote_translation|
RemoteTranslation.create(remote_translation) unless translations_enqueued?(remote_translation)
end
redirect_to request.referer, notice: t('remote_translations.create.enqueue_remote_translation')
end
private
def remote_translations_params
params.permit(:remote_translations)
end
def set_remote_translations
decoded_remote_translations = ActiveSupport::JSON.decode(remote_translations_params["remote_translations"])
@remote_translations = decoded_remote_translations.map{ |remote_translation|
remote_translation.slice("remote_translatable_id","remote_translatable_type","locale")
}
end
def translations_enqueued?(remote_translation)
RemoteTranslation.remote_translation_enqueued?(remote_translation)
end
end

View File

@@ -11,4 +11,10 @@ class RemoteTranslation < ApplicationRecord
def enqueue_remote_translation
end
def self.remote_translation_enqueued?(remote_translation)
where(remote_translatable_id: remote_translation["remote_translatable_id"],
remote_translatable_type: remote_translation["remote_translatable_type"],
locale: remote_translation["locale"],
error_message: nil).any?
end
end

View File

@@ -1033,3 +1033,6 @@ en:
title: Prioritization type
borda: Borda votation
dowdall: Dowdall votation
remote_translations:
create:
enqueue_remote_translation: Translations have been correctly requested.

View File

@@ -1030,3 +1030,6 @@ es:
title: Tipo de priorizacion
borda: Votación con recuento Borda
dowdall: Votación con recuento Dowdall
remote_translations:
create:
enqueue_remote_translation: Se han solicitado correctamente las traducciones.

View File

@@ -42,6 +42,7 @@ Rails.application.routes.draw do
resources :images, only: [:destroy]
resources :documents, only: [:destroy]
resources :follows, only: [:create, :destroy]
resources :remote_translations, only: [:create]
# More info pages
get "help", to: "pages#show", id: "help/index", as: "help"

View File

@@ -0,0 +1,48 @@
require "rails_helper"
describe RemoteTranslationsController do
describe "POST create" do
before do
@debate = create(:debate)
@remote_translations_params = [{ remote_translatable_id: @debate.id.to_s,
remote_translatable_type: @debate.class.to_s,
locale: :es }].to_json
allow(controller.request).to receive(:referer).and_return("any_path")
Delayed::Worker.delay_jobs = true
end
after do
Delayed::Worker.delay_jobs = false
end
it "create correctly remote translation" do
post :create, remote_translations: @remote_translations_params
expect(RemoteTranslation.count).to eq(1)
end
it "create remote translation when same remote translation with error_message is enqueued" do
create(:remote_translation, remote_translatable: @debate, locale: :es, error_message: "Has errors")
post :create, remote_translations: @remote_translations_params
expect(RemoteTranslation.count).to eq(2)
end
it "not create remote translation when same remote translation is enqueued" do
create(:remote_translation, remote_translatable: @debate, locale: :es)
post :create, remote_translations: @remote_translations_params
expect(RemoteTranslation.count).to eq(1)
end
it "redirect_to request referer after create" do
post :create, remote_translations: @remote_translations_params
expect(subject).to redirect_to("any_path")
end
end
end