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:
@@ -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)
|
||||
|
||||
@@ -8,11 +8,9 @@ module SiteCustomizationHelper
|
||||
end
|
||||
|
||||
def translation_for_locale(content, locale)
|
||||
i18n_content = I18nContent.where(key: content.key).first
|
||||
|
||||
if i18n_content.present?
|
||||
if content.present?
|
||||
I18nContentTranslation.where(
|
||||
i18n_content_id: i18n_content.id,
|
||||
i18n_content_id: content.id,
|
||||
locale: locale
|
||||
).first.try(:value)
|
||||
else
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
class I18nContent < ApplicationRecord
|
||||
|
||||
scope :by_key, ->(key) { where(key: key) }
|
||||
scope :begins_with_key, ->(key) { where("key ILIKE ?", "#{key}%") }
|
||||
|
||||
validates :key, uniqueness: true
|
||||
|
||||
@@ -46,4 +45,17 @@ class I18nContent < ApplicationRecord
|
||||
return output
|
||||
end
|
||||
|
||||
def self.content_for(tab)
|
||||
flat_hash(translations_for(tab)).keys.map do |string|
|
||||
I18nContent.find_or_initialize_by(key: string)
|
||||
end
|
||||
end
|
||||
|
||||
def self.translations_for(tab)
|
||||
I18n.backend.send(:init_translations) unless I18n.backend.initialized?
|
||||
|
||||
I18n.backend.send(:translations)[I18n.locale].select do |key, _translations|
|
||||
key.to_s == tab.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,20 +27,6 @@ RSpec.describe I18nContent, type: :model do
|
||||
expect(query.size).to eq(1)
|
||||
expect(query).to eq([debate_title])
|
||||
end
|
||||
|
||||
it "return all matching records when #begins_with_key is used" do
|
||||
debate_text = create(:i18n_content, key: "debates.form.debate_text")
|
||||
debate_title = create(:i18n_content, key: "debates.form.debate_title")
|
||||
proposal_title = create(:i18n_content, key: "proposals.form.proposal_title")
|
||||
|
||||
expect(I18nContent.all.size).to eq(3)
|
||||
|
||||
query = I18nContent.begins_with_key("debates")
|
||||
|
||||
expect(query.size).to eq(2)
|
||||
expect(query).to eq([debate_text, debate_title])
|
||||
expect(query).not_to include(proposal_title)
|
||||
end
|
||||
end
|
||||
|
||||
context "Globalize" do
|
||||
|
||||
Reference in New Issue
Block a user