Update information texts translatable fields

This part used the code we deleted in order to make it easier to
refactor the rest of the translatable models. Now we add the code back.
This commit is contained in:
Javi Martín
2018-10-11 10:55:23 +02:00
committed by decabeza
parent 1f033383e5
commit 87484015da
7 changed files with 28 additions and 19 deletions

View File

@@ -35,9 +35,11 @@ App.Globalize =
enable_locale: (locale) -> enable_locale: (locale) ->
App.Globalize.destroy_locale_field(locale).val(false) App.Globalize.destroy_locale_field(locale).val(false)
App.Globalize.site_customization_enable_locale_field(locale).val(1)
disable_locale: (locale) -> disable_locale: (locale) ->
App.Globalize.destroy_locale_field(locale).val(true) App.Globalize.destroy_locale_field(locale).val(true)
App.Globalize.site_customization_enable_locale_field(locale).val(0)
enabled_locales: -> enabled_locales: ->
$.map( $.map(
@@ -48,6 +50,9 @@ App.Globalize =
destroy_locale_field: (locale) -> destroy_locale_field: (locale) ->
$(".destroy_locale[data-locale=" + locale + "]") $(".destroy_locale[data-locale=" + locale + "]")
site_customization_enable_locale_field: (locale) ->
$("#enabled_translations_" + locale)
refresh_visible_translations: -> refresh_visible_translations: ->
locale = $('.js-globalize-locale-link.is-active').data("locale") locale = $('.js-globalize-locale-link.is-active').data("locale")
App.Globalize.display_translations(locale) App.Globalize.display_translations(locale)

View File

@@ -1,5 +1,5 @@
class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomization::BaseController class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomization::BaseController
include Translatable before_action :delete_translations, only: [:update]
def index def index
fetch_existing_keys fetch_existing_keys
@@ -9,7 +9,7 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
def update def update
content_params.each do |content| content_params.each do |content|
values = content[:values].slice(*translation_params(I18nContent)) values = content[:values].slice(*translation_params)
unless values.empty? unless values.empty?
values.each do |key, value| values.each do |key, value|
@@ -73,4 +73,15 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
end end
end end
def translation_params
I18nContent.translated_attribute_names.product(enabled_translations).map do |attr_name, loc|
I18nContent.localized_attr_name_for(attr_name, loc)
end
end
def enabled_translations
params.fetch(:enabled_translations, {})
.select { |_, v| v == '1' }
.keys
end
end end

View File

@@ -9,10 +9,4 @@ module Translatable
resource_model.translated_attribute_names resource_model.translated_attribute_names
} }
end end
def enabled_translations
params.fetch(:enabled_translations, {})
.select { |_, v| v == '1' }
.keys
end
end end

View File

@@ -19,12 +19,4 @@ module SiteCustomizationHelper
false false
end end
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

@@ -6,7 +6,7 @@ class I18nContent < ActiveRecord::Base
validates :key, uniqueness: true validates :key, uniqueness: true
translates :value, touch: true translates :value, touch: true
globalize_accessors locales: [:en, :es, :fr, :nl] globalize_accessors
# flat_hash returns a flattened hash, a hash with a single level of # flat_hash returns a flattened hash, a hash with a single level of
# depth in which each key is composed from the keys of the original # depth in which each key is composed from the keys of the original

View File

@@ -2,5 +2,8 @@
<%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %> <%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %>
<%= text_area_tag "contents[content_#{content.key}]values[value_#{locale}]", <%= text_area_tag "contents[content_#{content.key}]values[value_#{locale}]",
translation_for_locale(content, locale) || t(content.key, locale: locale), translation_for_locale(content, locale) || t(content.key, locale: locale),
merge_translatable_field_options({rows: 5}, locale) %> rows: 5,
class: "js-globalize-attribute",
style: site_customization_display_translation_style(locale),
data: { locale: locale } %>
<% end %> <% end %>

View File

@@ -268,7 +268,11 @@ end
def expect_page_to_have_translatable_field(field, locale, with:) def expect_page_to_have_translatable_field(field, locale, with:)
if input_fields.include?(field) if input_fields.include?(field)
if translatable_class.name == "I18nContent" && with.blank?
expect(page).to have_field field_for(field, locale)
else
expect(page).to have_field field_for(field, locale), with: with expect(page).to have_field field_for(field, locale), with: with
end
else else
textarea_type = textarea_fields[field] textarea_type = textarea_fields[field]