Simplify code checking whether to enable a locale

This commit is contained in:
Javi Martín
2018-10-18 00:31:51 +02:00
parent f5bb3c64a1
commit 93a7cb6c0f
2 changed files with 9 additions and 4 deletions

View File

@@ -32,10 +32,11 @@ module GlobalizeHelper
end
def enable_locale?(resource, locale)
# Use `map` instead of `pluck` in order to keep the `params` sent
# by the browser when there's invalid data
(resource.translations.blank? && locale == I18n.locale) ||
resource.translations.reject(&:_destroy).map(&:locale).include?(locale)
if resource.translations.any?
resource.locales_not_marked_for_destruction.include?(locale)
else
locale == I18n.locale
end
end
def highlight_class(resource, locale)

View File

@@ -4,6 +4,10 @@ module Globalizable
included do
globalize_accessors
accepts_nested_attributes_for :translations, allow_destroy: true
def locales_not_marked_for_destruction
translations.reject(&:_destroy).map(&:locale)
end
end
class_methods do