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.
32 lines
901 B
Ruby
32 lines
901 B
Ruby
module Translatable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
before_action :set_translation_locale
|
|
before_action :delete_translations, only: [:update]
|
|
end
|
|
|
|
private
|
|
|
|
def translation_params
|
|
Budget::Investment::Milestone.globalize_attribute_names.
|
|
select { |k, v| params[:budget_investment_milestone].include?(k.to_sym) && params[:budget_investment_milestone][k].present? }
|
|
end
|
|
|
|
def set_translation_locale
|
|
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
|