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) -> display_translations: (locale) ->
$(".js-globalize-attribute").each -> $(".js-globalize-attribute").each ->
console.log("In standard")
console.log($(this))
if $(this).data("locale") == locale if $(this).data("locale") == locale
$(this).show() $(this).show()
else else
$(this).hide() $(this).hide()
$('.delete-language').hide()
$('#delete-' + locale).show()
highlight_locale: (element) -> highlight_locale: (element) ->
$('.js-globalize-locale-link').removeClass('highlight'); $('.js-globalize-locale-link').removeClass('highlight');
element.addClass('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: -> initialize: ->
$('.js-globalize-locale').on 'change', -> $('.js-globalize-locale').on 'change', ->
App.Globalize.display_translations($(this).val()) App.Globalize.display_translations($(this).val())
@@ -28,4 +39,10 @@ App.Globalize =
$('.js-globalize-locale-link').on 'click', -> $('.js-globalize-locale-link').on 'click', ->
locale = $(this).data("locale") locale = $(this).data("locale")
App.Globalize.display_translations(locale) App.Globalize.display_translations(locale)
App.Globalize.highlight_locale($(this)) 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 included do
before_action :set_translation_locale before_action :set_translation_locale
before_action :delete_translations, only: [:update]
end end
private private
@@ -16,4 +17,15 @@ module Translatable
Globalize.locale = I18n.locale Globalize.locale = I18n.locale
end end
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" : "" globalize_locale == locale ? "highlight" : ""
end end
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") %> <%= f.label :description, t("admin.milestones.new.description") %>
<% @milestone.globalize_locales.each do |locale| %> <% @milestone.globalize_locales.each do |locale| %>
<%= hidden_field_tag "delete_translations[#{locale}]", 0 %>
<% Globalize.with_locale(locale) do %> <% Globalize.with_locale(locale) do %>
<%= f.text_area "description_#{locale}", rows: 5, <%= f.text_area "description_#{locale}", rows: 5,
class: "js-globalize-attribute", class: "js-globalize-attribute",

View File

@@ -5,10 +5,15 @@
class: "js-globalize-locale-link", class: "js-globalize-locale-link",
data: { locale: locale }, data: { locale: locale },
remote: true %> 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> </span>
<% end %> <% end %>
<%= select_tag :translation_locale, <%= select_tag :translation_locale,
options_for_locale_select, options_for_locale_select,
prompt: "Añadir idioma", prompt: t("admin.milestones.form.add_language"),
class: "js-globalize-locale" %> class: "js-globalize-locale" %>

View File

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

View File

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