Add feature to delete a translation

To delete a translation, a link has been added. This
link works for the selected language. It hides all the
things related to a language (the tab and the text_area)
and empties the text area, so that the value is blank
in the param hash. A variable called `delete_translations[]`
is changed.

e.g. If admin wants to remove English language,
delete_translations[:en] will be 1; if not, it will be 0.

When the milestone is updated, there is a before_action
callback that cleans the selected languages for deletion
(looking the delete_translations[] variable).
Because of the deleted translations are blank in param hash,
them won't be saved in DB.
This commit is contained in:
iagirre
2018-04-12 09:58:02 +02:00
parent f7486b9238
commit b318c2be46
7 changed files with 55 additions and 8 deletions

View File

@@ -9,17 +9,28 @@ App.Globalize =
display_translations: (locale) ->
$(".js-globalize-attribute").each ->
console.log("In standard")
console.log($(this))
if $(this).data("locale") == locale
$(this).show()
else
$(this).hide()
$('.delete-language').hide()
$('#delete-' + locale).show()
highlight_locale: (element) ->
$('.js-globalize-locale-link').removeClass('highlight');
element.addClass('highlight');
remove_language: (locale) ->
$(".js-globalize-attribute[data-locale=" + locale + "]").val('')
$(".js-globalize-attribute[data-locale=" + locale + "]").hide()
$(".js-globalize-locale-link[data-locale=" + locale + "]").hide()
$("#delete-" + locale).hide()
next = $(".js-globalize-locale-link:visible").first()
App.Globalize.highlight_locale(next)
$(".js-globalize-attribute[data-locale=" + next.data("locale") + "]").show()
$("#delete-" + next.data("locale")).show()
$("#delete_translations_" + locale).val(1)
initialize: ->
$('.js-globalize-locale').on 'change', ->
App.Globalize.display_translations($(this).val())
@@ -29,3 +40,9 @@ App.Globalize =
locale = $(this).data("locale")
App.Globalize.display_translations(locale)
App.Globalize.highlight_locale($(this))
$('.delete-language').on 'click', ->
locale = $(this).data("locale")
$(this).hide()
App.Globalize.remove_language(locale)

View File

@@ -3,6 +3,7 @@ module Translatable
included do
before_action :set_translation_locale
before_action :delete_translations, only: [:update]
end
private
@@ -16,4 +17,15 @@ module Translatable
Globalize.locale = I18n.locale
end
def delete_translations
locales = Budget::Investment::Milestone.globalize_locales.
select { |k, v| params[:delete_translations].include?(k.to_sym) && params[:delete_translations][k] == "1" }
milestone = Budget::Investment::Milestone.find(params[:id])
locales.each do |l|
Globalize.with_locale(l) do
milestone.translation.destroy
end
end
end
end

View File

@@ -30,4 +30,8 @@ module GlobalizeHelper
globalize_locale == locale ? "highlight" : ""
end
def show_delete?(locale)
I18n.locale == locale ? '' : 'display: none'
end
end

View File

@@ -9,6 +9,7 @@
<%= f.label :description, t("admin.milestones.new.description") %>
<% @milestone.globalize_locales.each do |locale| %>
<%= hidden_field_tag "delete_translations[#{locale}]", 0 %>
<% Globalize.with_locale(locale) do %>
<%= f.text_area "description_#{locale}", rows: 5,
class: "js-globalize-attribute",

View File

@@ -5,10 +5,15 @@
class: "js-globalize-locale-link",
data: { locale: locale },
remote: true %>
<%= link_to t("admin.milestones.form.remove_language"), "#",
id: "delete-#{locale}",
style: show_delete?(locale),
class: 'float-right delete-language',
data: { locale: locale } %>
</span>
<% end %>
<%= select_tag :translation_locale,
options_for_locale_select,
prompt: "Añadir idioma",
prompt: t("admin.milestones.form.add_language"),
class: "js-globalize-locale" %>

View File

@@ -254,9 +254,13 @@ en:
image: "Image"
show_image: "Show image"
documents: "Documents"
form:
add_language: Add language
remove_language: Remove language
new:
creating: Create milestone
date: Date
description: Description
edit:
title: Edit milestone
create:

View File

@@ -254,9 +254,13 @@ es:
image: "Imagen"
show_image: "Mostrar imagen"
documents: "Documentos"
form:
add_language: Añadir idioma
remove_language: Eliminar idioma
new:
creating: Crear hito
date: Fecha
description: Descripción
edit:
title: Editar hito
create: