Simplify getting I18nContent translations

This code might be slightly slower because it performs one query per
field in the form, but I didn't notice any differences on my development
machine, and the code is now much easier to understand.
This commit is contained in:
Javi Martín
2019-05-09 18:24:12 +02:00
parent 7db1cac3bc
commit 1135441cbd
4 changed files with 17 additions and 45 deletions

View File

@@ -2,9 +2,8 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
before_action :delete_translations, only: [:update]
def index
fetch_existing_keys
append_or_create_keys
@content = @content[@tab.to_s]
@tab = params[:tab] || :debates
@content = I18nContent.content_for(@tab)
end
def update
@@ -50,29 +49,6 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
end
end
def fetch_existing_keys
@existing_keys = {}
@tab = params[:tab] || :debates
I18nContent.begins_with_key(@tab).map { |content|
@existing_keys[content.key] = content
}
end
def append_or_create_keys
@content = {}
I18n.backend.send(:init_translations) unless I18n.backend.initialized?
locale = params[:locale] || I18n.locale
translations = I18n.backend.send(:translations)[locale.to_sym]
translations.each do |key, value|
@content[key.to_s] = I18nContent.flat_hash(value).keys.map { |string|
@existing_keys["#{key.to_s}.#{string}"] || I18nContent.new(key: "#{key.to_s}.#{string}")
}
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)