From 87484015dab898d8a1ba7bce16768687271a2bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 11 Oct 2018 10:55:23 +0200 Subject: [PATCH] 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. --- app/assets/javascripts/globalize.js.coffee | 5 +++++ .../information_texts_controller.rb | 15 +++++++++++++-- app/controllers/concerns/translatable.rb | 6 ------ app/helpers/site_customization_helper.rb | 8 -------- app/models/i18n_content.rb | 2 +- .../information_texts/_form_field.html.erb | 5 ++++- spec/shared/features/translatable.rb | 6 +++++- 7 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/globalize.js.coffee b/app/assets/javascripts/globalize.js.coffee index 06b1722f5..01ea9ec61 100644 --- a/app/assets/javascripts/globalize.js.coffee +++ b/app/assets/javascripts/globalize.js.coffee @@ -35,9 +35,11 @@ App.Globalize = enable_locale: (locale) -> App.Globalize.destroy_locale_field(locale).val(false) + App.Globalize.site_customization_enable_locale_field(locale).val(1) disable_locale: (locale) -> App.Globalize.destroy_locale_field(locale).val(true) + App.Globalize.site_customization_enable_locale_field(locale).val(0) enabled_locales: -> $.map( @@ -48,6 +50,9 @@ App.Globalize = destroy_locale_field: (locale) -> $(".destroy_locale[data-locale=" + locale + "]") + site_customization_enable_locale_field: (locale) -> + $("#enabled_translations_" + locale) + refresh_visible_translations: -> locale = $('.js-globalize-locale-link.is-active').data("locale") App.Globalize.display_translations(locale) diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index 3a5492bcc..4706dfd08 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -1,5 +1,5 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomization::BaseController - include Translatable + before_action :delete_translations, only: [:update] def index fetch_existing_keys @@ -9,7 +9,7 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz def update content_params.each do |content| - values = content[:values].slice(*translation_params(I18nContent)) + values = content[:values].slice(*translation_params) unless values.empty? values.each do |key, value| @@ -73,4 +73,15 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz 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 diff --git a/app/controllers/concerns/translatable.rb b/app/controllers/concerns/translatable.rb index 272a97e99..94cdcaaab 100644 --- a/app/controllers/concerns/translatable.rb +++ b/app/controllers/concerns/translatable.rb @@ -9,10 +9,4 @@ module Translatable resource_model.translated_attribute_names } end - - def enabled_translations - params.fetch(:enabled_translations, {}) - .select { |_, v| v == '1' } - .keys - end end diff --git a/app/helpers/site_customization_helper.rb b/app/helpers/site_customization_helper.rb index a8f01038a..14dfe1262 100644 --- a/app/helpers/site_customization_helper.rb +++ b/app/helpers/site_customization_helper.rb @@ -19,12 +19,4 @@ module SiteCustomizationHelper false 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 diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb index bc7544f6a..d3b5f4858 100644 --- a/app/models/i18n_content.rb +++ b/app/models/i18n_content.rb @@ -6,7 +6,7 @@ class I18nContent < ActiveRecord::Base validates :key, uniqueness: 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 # depth in which each key is composed from the keys of the original diff --git a/app/views/admin/site_customization/information_texts/_form_field.html.erb b/app/views/admin/site_customization/information_texts/_form_field.html.erb index 3c5ae9b5c..799a76b6e 100644 --- a/app/views/admin/site_customization/information_texts/_form_field.html.erb +++ b/app/views/admin/site_customization/information_texts/_form_field.html.erb @@ -2,5 +2,8 @@ <%= hidden_field_tag "contents[content_#{content.key}][id]", content.key %> <%= text_area_tag "contents[content_#{content.key}]values[value_#{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 %> diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 81c034aeb..f23b037a3 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -268,7 +268,11 @@ end def expect_page_to_have_translatable_field(field, locale, with:) if input_fields.include?(field) - expect(page).to have_field field_for(field, locale), with: with + 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 + end else textarea_type = textarea_fields[field]