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:
@@ -11,15 +11,19 @@ module GlobalizeHelper
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def translation_enabled_tag(locale, enabled)
|
||||
hidden_field_tag("enabled_translations[#{locale}]", (enabled ? 1 : 0))
|
||||
end
|
||||
|
||||
def css_to_display_translation?(resource, locale)
|
||||
enable_locale?(resource, locale) ? "" : "display: none;"
|
||||
def enable_translation_style(resource, locale)
|
||||
"display: none;" unless enable_locale?(resource, locale)
|
||||
end
|
||||
|
||||
def enable_locale?(resource, locale)
|
||||
@@ -28,12 +32,8 @@ module GlobalizeHelper
|
||||
resource.translations.reject(&:_destroy).map(&:locale).include?(locale) || locale == I18n.locale
|
||||
end
|
||||
|
||||
def highlight_current?(locale)
|
||||
same_locale?(I18n.locale, locale) ? 'is-active' : ''
|
||||
end
|
||||
|
||||
def show_delete?(locale)
|
||||
display_translation?(locale)
|
||||
def highlight_class(locale)
|
||||
"is-active" if display_translation?(locale)
|
||||
end
|
||||
|
||||
def globalize(locale, &block)
|
||||
|
||||
@@ -3,7 +3,15 @@ module SiteCustomizationHelper
|
||||
I18nContentTranslation.existing_languages.include?(locale) || locale == I18n.locale
|
||||
end
|
||||
|
||||
def site_customization_display_translation?(locale)
|
||||
def site_customization_display_translation_style(locale)
|
||||
site_customization_enable_translation?(locale) ? "" : "display: none;"
|
||||
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
|
||||
|
||||
@@ -5,14 +5,6 @@ module TranslatableFormHelper
|
||||
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
|
||||
def translatable_fields(&block)
|
||||
@object.globalize_locales.map do |locale|
|
||||
@@ -62,7 +54,7 @@ module TranslatableFormHelper
|
||||
content_tag :div,
|
||||
label_help_text_and_field,
|
||||
class: "js-globalize-attribute",
|
||||
style: @template.display_translation?(locale),
|
||||
style: display_style,
|
||||
data: { locale: locale }
|
||||
else
|
||||
label_help_text_and_field
|
||||
@@ -75,30 +67,34 @@ module TranslatableFormHelper
|
||||
end
|
||||
|
||||
def label(attribute, text = nil, options = {})
|
||||
label_options = options.merge(
|
||||
class: "#{options[:class]} js-globalize-attribute".strip,
|
||||
style: "#{options[:style]} #{@template.display_translation?(locale)}".strip,
|
||||
data: (options[:data] || {}) .merge(locale: locale)
|
||||
)
|
||||
|
||||
label_options = translations_options(options)
|
||||
hint = label_options.delete(:hint)
|
||||
|
||||
super(attribute, text, label_options) + help_text(hint)
|
||||
end
|
||||
|
||||
def display_style
|
||||
@template.display_translation_style(locale)
|
||||
end
|
||||
|
||||
private
|
||||
def help_text(text)
|
||||
if text
|
||||
content_tag :span, text,
|
||||
class: "help-text js-globalize-attribute",
|
||||
data: { locale: locale },
|
||||
style: @template.display_translation?(locale)
|
||||
style: display_style
|
||||
else
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<%= content_tag :div,
|
||||
class: "markdown-editor clear js-globalize-attribute",
|
||||
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="markdown-editor-header truncate">
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<% I18n.available_locales.each do |locale| %>
|
||||
<%= link_to t("admin.translations.remove_language"), "#",
|
||||
id: "delete-#{locale}",
|
||||
style: show_delete?(locale),
|
||||
style: display_translation_style(locale),
|
||||
class: 'float-right delete js-delete-language',
|
||||
data: { locale: locale } %>
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
<% I18n.available_locales.each do |locale| %>
|
||||
<li class="tabs-title">
|
||||
<%= link_to name_for_locale(locale), "#",
|
||||
style: css_to_display_translation?(resource, locale),
|
||||
class: "js-globalize-locale-link #{highlight_current?(locale)}",
|
||||
style: enable_translation_style(resource, locale),
|
||||
class: "js-globalize-locale-link #{highlight_class(locale)}",
|
||||
data: { locale: locale },
|
||||
remote: true %>
|
||||
</li>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<% I18n.available_locales.each do |locale| %>
|
||||
<%= link_to t("admin.translations.remove_language"), "#",
|
||||
id: "delete-#{locale}",
|
||||
style: show_delete?(locale),
|
||||
style: display_translation_style(locale),
|
||||
class: 'float-right delete js-delete-language',
|
||||
data: { locale: locale } %>
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
<% I18n.available_locales.each do |locale| %>
|
||||
<li class="tabs-title">
|
||||
<%= link_to name_for_locale(locale), "#",
|
||||
style: site_customization_display_translation?(locale),
|
||||
class: "js-globalize-locale-link #{highlight_current?(locale)}",
|
||||
style: site_customization_display_translation_style(locale),
|
||||
class: "js-globalize-locale-link #{highlight_class(locale)}",
|
||||
data: { locale: locale },
|
||||
remote: true %>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user