Simplify methods defining translation styles

This refactor is going to be useful when we change these rules within
the next few commits.
This commit is contained in:
Javi Martín
2018-10-11 01:17:27 +02:00
parent 139cf769c6
commit 124b8496de
6 changed files with 38 additions and 34 deletions

View File

@@ -11,15 +11,19 @@ module GlobalizeHelper
end end
def display_translation?(locale) def display_translation?(locale)
same_locale?(I18n.locale, locale) ? "" : "display: none;" locale == I18n.locale
end
def display_translation_style(locale)
"display: none;" unless display_translation?(locale)
end end
def translation_enabled_tag(locale, enabled) def translation_enabled_tag(locale, enabled)
hidden_field_tag("enabled_translations[#{locale}]", (enabled ? 1 : 0)) hidden_field_tag("enabled_translations[#{locale}]", (enabled ? 1 : 0))
end end
def css_to_display_translation?(resource, locale) def enable_translation_style(resource, locale)
enable_locale?(resource, locale) ? "" : "display: none;" "display: none;" unless enable_locale?(resource, locale)
end end
def enable_locale?(resource, locale) def enable_locale?(resource, locale)
@@ -28,12 +32,8 @@ module GlobalizeHelper
resource.translations.reject(&:_destroy).map(&:locale).include?(locale) || locale == I18n.locale resource.translations.reject(&:_destroy).map(&:locale).include?(locale) || locale == I18n.locale
end end
def highlight_current?(locale) def highlight_class(locale)
same_locale?(I18n.locale, locale) ? 'is-active' : '' "is-active" if display_translation?(locale)
end
def show_delete?(locale)
display_translation?(locale)
end end
def globalize(locale, &block) def globalize(locale, &block)

View File

@@ -3,7 +3,15 @@ module SiteCustomizationHelper
I18nContentTranslation.existing_languages.include?(locale) || locale == I18n.locale I18nContentTranslation.existing_languages.include?(locale) || locale == I18n.locale
end end
def site_customization_display_translation?(locale) def site_customization_display_translation_style(locale)
site_customization_enable_translation?(locale) ? "" : "display: none;" site_customization_enable_translation?(locale) ? "" : "display: none;"
end end
def merge_translatable_field_options(options, locale)
options.merge(
class: "#{options[:class]} js-globalize-attribute".strip,
style: "#{options[:style]} #{site_customization_display_translation_style(locale)}".strip,
data: (options[:data] || {}).merge(locale: locale)
)
end
end end

View File

@@ -5,14 +5,6 @@ module TranslatableFormHelper
end end
end end
def merge_translatable_field_options(options, locale)
options.merge(
class: "#{options[:class]} js-globalize-attribute".strip,
style: "#{options[:style]} #{display_translation?(locale)}".strip,
data: options.fetch(:data, {}).merge(locale: locale),
)
end
class TranslatableFormBuilder < FoundationRailsHelper::FormBuilder class TranslatableFormBuilder < FoundationRailsHelper::FormBuilder
def translatable_fields(&block) def translatable_fields(&block)
@object.globalize_locales.map do |locale| @object.globalize_locales.map do |locale|
@@ -62,7 +54,7 @@ module TranslatableFormHelper
content_tag :div, content_tag :div,
label_help_text_and_field, label_help_text_and_field,
class: "js-globalize-attribute", class: "js-globalize-attribute",
style: @template.display_translation?(locale), style: display_style,
data: { locale: locale } data: { locale: locale }
else else
label_help_text_and_field label_help_text_and_field
@@ -75,30 +67,34 @@ module TranslatableFormHelper
end end
def label(attribute, text = nil, options = {}) def label(attribute, text = nil, options = {})
label_options = options.merge( label_options = translations_options(options)
class: "#{options[:class]} js-globalize-attribute".strip,
style: "#{options[:style]} #{@template.display_translation?(locale)}".strip,
data: (options[:data] || {}) .merge(locale: locale)
)
hint = label_options.delete(:hint) hint = label_options.delete(:hint)
super(attribute, text, label_options) + help_text(hint) super(attribute, text, label_options) + help_text(hint)
end end
def display_style
@template.display_translation_style(locale)
end
private private
def help_text(text) def help_text(text)
if text if text
content_tag :span, text, content_tag :span, text,
class: "help-text js-globalize-attribute", class: "help-text js-globalize-attribute",
data: { locale: locale }, data: { locale: locale },
style: @template.display_translation?(locale) style: display_style
else else
"" ""
end end
end end
def translations_options(options) def translations_options(options)
@template.merge_translatable_field_options(options, locale) options.merge(
class: "#{options[:class]} js-globalize-attribute".strip,
style: "#{options[:style]} #{display_style}".strip,
data: (options[:data] || {}).merge(locale: locale)
)
end end
end end
end end

View File

@@ -35,7 +35,7 @@
<%= content_tag :div, <%= content_tag :div,
class: "markdown-editor clear js-globalize-attribute", class: "markdown-editor clear js-globalize-attribute",
data: { locale: translations_form.locale }, data: { locale: translations_form.locale },
style: display_translation?(translations_form.locale) do %> style: translations_form.display_style do %>
<div class="small-12 medium-8 column fullscreen-container"> <div class="small-12 medium-8 column fullscreen-container">
<div class="markdown-editor-header truncate"> <div class="markdown-editor-header truncate">

View File

@@ -1,7 +1,7 @@
<% I18n.available_locales.each do |locale| %> <% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#", <%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{locale}", id: "delete-#{locale}",
style: show_delete?(locale), style: display_translation_style(locale),
class: 'float-right delete js-delete-language', class: 'float-right delete js-delete-language',
data: { locale: locale } %> data: { locale: locale } %>
@@ -11,8 +11,8 @@
<% I18n.available_locales.each do |locale| %> <% I18n.available_locales.each do |locale| %>
<li class="tabs-title"> <li class="tabs-title">
<%= link_to name_for_locale(locale), "#", <%= link_to name_for_locale(locale), "#",
style: css_to_display_translation?(resource, locale), style: enable_translation_style(resource, locale),
class: "js-globalize-locale-link #{highlight_current?(locale)}", class: "js-globalize-locale-link #{highlight_class(locale)}",
data: { locale: locale }, data: { locale: locale },
remote: true %> remote: true %>
</li> </li>

View File

@@ -1,7 +1,7 @@
<% I18n.available_locales.each do |locale| %> <% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#", <%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{locale}", id: "delete-#{locale}",
style: show_delete?(locale), style: display_translation_style(locale),
class: 'float-right delete js-delete-language', class: 'float-right delete js-delete-language',
data: { locale: locale } %> data: { locale: locale } %>
@@ -11,8 +11,8 @@
<% I18n.available_locales.each do |locale| %> <% I18n.available_locales.each do |locale| %>
<li class="tabs-title"> <li class="tabs-title">
<%= link_to name_for_locale(locale), "#", <%= link_to name_for_locale(locale), "#",
style: site_customization_display_translation?(locale), style: site_customization_display_translation_style(locale),
class: "js-globalize-locale-link #{highlight_current?(locale)}", class: "js-globalize-locale-link #{highlight_class(locale)}",
data: { locale: locale }, data: { locale: locale },
remote: true %> remote: true %>
</li> </li>