Add persisted but marked for destruction translations to logic

Now we take into consideration locales persisted but marked for
destruction to complete some logic and to be able to show best
translations on different situations.
This commit is contained in:
Senén Rodero Rodríguez
2019-06-07 16:50:50 +02:00
parent d3422acbb7
commit 041abe9044
2 changed files with 50 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
module GlobalizeHelper module GlobalizeHelper
def options_for_select_language(resource) def options_for_select_language(resource)
options_for_select(available_locales(resource), first_available_locale(resource)) options_for_select(available_locales(resource), selected_locale(resource))
end end
def available_locales(resource) def available_locales(resource)
@@ -13,20 +13,22 @@ module GlobalizeHelper
def enabled_locale?(resource, locale) def enabled_locale?(resource, locale)
return site_customization_enable_translation?(locale) if resource.blank? return site_customization_enable_translation?(locale) if resource.blank?
if resource.translations.empty? if resource.locales_not_marked_for_destruction.any?
locale == I18n.locale
else
resource.locales_not_marked_for_destruction.include?(locale) resource.locales_not_marked_for_destruction.include?(locale)
elsif resource.locales_persisted_and_marked_for_destruction.any?
locale == first_marked_for_destruction_translation(resource)
else
locale == I18n.locale
end end
end end
def first_available_locale(resource) def selected_locale(resource)
return first_i18n_content_translation_locale if resource.blank? return first_i18n_content_translation_locale if resource.blank?
if translations_for_locale?(resource, I18n.locale) if resource.locales_not_marked_for_destruction.any?
I18n.locale first_translation(resource)
elsif resource.translations.any? elsif resource.locales_persisted_and_marked_for_destruction.any?
resource.translations.first.locale first_marked_for_destruction_translation(resource)
else else
I18n.locale I18n.locale
end end
@@ -41,9 +43,24 @@ module GlobalizeHelper
end end
end end
def translations_for_locale?(resource, locale) def first_translation(resource)
resource.present? && resource.translations.any? && if resource.locales_not_marked_for_destruction.include? I18n.locale
resource.locales_not_marked_for_destruction.include?(locale) I18n.locale
else
resource.locales_not_marked_for_destruction.first
end
end
def first_marked_for_destruction_translation(resource)
if resource.locales_persisted_and_marked_for_destruction.include? I18n.locale
I18n.locale
else
resource.locales_persisted_and_marked_for_destruction.first
end
end
def translations_for_locale?(resource)
resource.locales_not_marked_for_destruction.any?
end end
def selected_languages_description(resource) def selected_languages_description(resource)
@@ -52,7 +69,7 @@ module GlobalizeHelper
def active_languages_count(resource) def active_languages_count(resource)
if resource.blank? if resource.blank?
languages_count no_resource_languages_count
elsif resource.locales_not_marked_for_destruction.size > 0 elsif resource.locales_not_marked_for_destruction.size > 0
resource.locales_not_marked_for_destruction.size resource.locales_not_marked_for_destruction.size
else else
@@ -60,7 +77,7 @@ module GlobalizeHelper
end end
end end
def languages_count def no_resource_languages_count
count = I18nContentTranslation.existing_languages.count count = I18nContentTranslation.existing_languages.count
count > 0 ? count : 1 count > 0 ? count : 1
end end
@@ -70,11 +87,14 @@ module GlobalizeHelper
end end
def display_translation?(resource, locale) def display_translation?(resource, locale)
if !resource || resource.translations.empty? || return locale == I18n.locale if resource.blank?
resource.locales_not_marked_for_destruction.include?(I18n.locale)
locale == I18n.locale if resource.locales_not_marked_for_destruction.any?
locale == first_translation(resource)
elsif resource.locales_persisted_and_marked_for_destruction.any?
locale == first_marked_for_destruction_translation(resource)
else else
locale == resource.translations.first.locale locale == I18n.locale
end end
end end
@@ -83,7 +103,7 @@ module GlobalizeHelper
end end
def display_destroy_locale_link?(resource, locale) def display_destroy_locale_link?(resource, locale)
first_available_locale(resource) == locale selected_locale(resource) == locale
end end
def options_for_add_language def options_for_add_language

View File

@@ -6,9 +6,6 @@ module Globalizable
globalize_accessors globalize_accessors
accepts_nested_attributes_for :translations, allow_destroy: true accepts_nested_attributes_for :translations, allow_destroy: true
def locales_not_marked_for_destruction
translations.reject(&:_destroy).map(&:locale)
end
validate :check_translations_number, on: :update, if: :translations_required? validate :check_translations_number, on: :update, if: :translations_required?
after_validation :copy_error_to_current_translation, on: :update after_validation :copy_error_to_current_translation, on: :update
@@ -16,6 +13,17 @@ module Globalizable
self.read_attribute(:description).try :html_safe self.read_attribute(:description).try :html_safe
end end
def locales_not_marked_for_destruction
translations.reject(&:marked_for_destruction?).map(&:locale)
end
def locales_marked_for_destruction
I18n.available_locales - locales_not_marked_for_destruction
end
def locales_persisted_and_marked_for_destruction
translations.select{|t| t.persisted? && t.marked_for_destruction? }.map(&:locale)
end
def translations_required? def translations_required?
translated_attribute_names.any?{|attr| required_attribute?(attr)} translated_attribute_names.any?{|attr| required_attribute?(attr)}