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:
@@ -1,7 +1,7 @@
|
||||
module GlobalizeHelper
|
||||
|
||||
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
|
||||
|
||||
def available_locales(resource)
|
||||
@@ -13,20 +13,22 @@ module GlobalizeHelper
|
||||
def enabled_locale?(resource, locale)
|
||||
return site_customization_enable_translation?(locale) if resource.blank?
|
||||
|
||||
if resource.translations.empty?
|
||||
locale == I18n.locale
|
||||
else
|
||||
if resource.locales_not_marked_for_destruction.any?
|
||||
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
|
||||
|
||||
def first_available_locale(resource)
|
||||
def selected_locale(resource)
|
||||
return first_i18n_content_translation_locale if resource.blank?
|
||||
|
||||
if translations_for_locale?(resource, I18n.locale)
|
||||
I18n.locale
|
||||
elsif resource.translations.any?
|
||||
resource.translations.first.locale
|
||||
if resource.locales_not_marked_for_destruction.any?
|
||||
first_translation(resource)
|
||||
elsif resource.locales_persisted_and_marked_for_destruction.any?
|
||||
first_marked_for_destruction_translation(resource)
|
||||
else
|
||||
I18n.locale
|
||||
end
|
||||
@@ -41,9 +43,24 @@ module GlobalizeHelper
|
||||
end
|
||||
end
|
||||
|
||||
def translations_for_locale?(resource, locale)
|
||||
resource.present? && resource.translations.any? &&
|
||||
resource.locales_not_marked_for_destruction.include?(locale)
|
||||
def first_translation(resource)
|
||||
if resource.locales_not_marked_for_destruction.include? I18n.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
|
||||
|
||||
def selected_languages_description(resource)
|
||||
@@ -52,7 +69,7 @@ module GlobalizeHelper
|
||||
|
||||
def active_languages_count(resource)
|
||||
if resource.blank?
|
||||
languages_count
|
||||
no_resource_languages_count
|
||||
elsif resource.locales_not_marked_for_destruction.size > 0
|
||||
resource.locales_not_marked_for_destruction.size
|
||||
else
|
||||
@@ -60,7 +77,7 @@ module GlobalizeHelper
|
||||
end
|
||||
end
|
||||
|
||||
def languages_count
|
||||
def no_resource_languages_count
|
||||
count = I18nContentTranslation.existing_languages.count
|
||||
count > 0 ? count : 1
|
||||
end
|
||||
@@ -70,11 +87,14 @@ module GlobalizeHelper
|
||||
end
|
||||
|
||||
def display_translation?(resource, locale)
|
||||
if !resource || resource.translations.empty? ||
|
||||
resource.locales_not_marked_for_destruction.include?(I18n.locale)
|
||||
locale == I18n.locale
|
||||
return locale == I18n.locale if resource.blank?
|
||||
|
||||
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
|
||||
locale == resource.translations.first.locale
|
||||
locale == I18n.locale
|
||||
end
|
||||
end
|
||||
|
||||
@@ -83,7 +103,7 @@ module GlobalizeHelper
|
||||
end
|
||||
|
||||
def display_destroy_locale_link?(resource, locale)
|
||||
first_available_locale(resource) == locale
|
||||
selected_locale(resource) == locale
|
||||
end
|
||||
|
||||
def options_for_add_language
|
||||
|
||||
@@ -6,9 +6,6 @@ module Globalizable
|
||||
globalize_accessors
|
||||
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?
|
||||
after_validation :copy_error_to_current_translation, on: :update
|
||||
|
||||
@@ -16,6 +13,17 @@ module Globalizable
|
||||
self.read_attribute(:description).try :html_safe
|
||||
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?
|
||||
translated_attribute_names.any?{|attr| required_attribute?(attr)}
|
||||
|
||||
Reference in New Issue
Block a user