From 1135441cbde5b5d5d0eed8bdef0a2c1d5a7d4df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 9 May 2019 18:24:12 +0200 Subject: [PATCH 1/2] 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. --- .../information_texts_controller.rb | 28 ++----------------- app/helpers/site_customization_helper.rb | 6 ++-- app/models/i18n_content.rb | 14 +++++++++- spec/models/i18n_content_spec.rb | 14 ---------- 4 files changed, 17 insertions(+), 45 deletions(-) diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index 85e765cb4..cd4566c58 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -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) diff --git a/app/helpers/site_customization_helper.rb b/app/helpers/site_customization_helper.rb index 14dfe1262..b32cf2ea8 100644 --- a/app/helpers/site_customization_helper.rb +++ b/app/helpers/site_customization_helper.rb @@ -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 diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb index 173c7310a..4fe3db4c7 100644 --- a/app/models/i18n_content.rb +++ b/app/models/i18n_content.rb @@ -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 diff --git a/spec/models/i18n_content_spec.rb b/spec/models/i18n_content_spec.rb index 23aff1ebb..f412d2946 100644 --- a/spec/models/i18n_content_spec.rb +++ b/spec/models/i18n_content_spec.rb @@ -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 From a2cb7501f7923c204cb3d7b2bdf6ea1d20c08889 Mon Sep 17 00:00:00 2001 From: decabeza Date: Tue, 14 May 2019 15:51:32 +0200 Subject: [PATCH 2/2] Create new basic tab for admin information texts --- .../information_texts_controller.rb | 2 +- app/helpers/site_customization_helper.rb | 4 ++ app/models/i18n_content.rb | 40 ++++++++++++++++++- .../information_texts/_tabs.html.erb | 2 +- config/locales/en/admin.yml | 1 + config/locales/es/admin.yml | 1 + .../information_texts_spec.rb | 33 +++++++++------ .../information_texts_spec.rb | 34 ++++++++++++++++ 8 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 spec/features/site_customization/information_texts_spec.rb diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index cd4566c58..4c4b07a81 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -2,7 +2,7 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz before_action :delete_translations, only: [:update] def index - @tab = params[:tab] || :debates + @tab = params[:tab] || :basic @content = I18nContent.content_for(@tab) end diff --git a/app/helpers/site_customization_helper.rb b/app/helpers/site_customization_helper.rb index b32cf2ea8..4a3e631b6 100644 --- a/app/helpers/site_customization_helper.rb +++ b/app/helpers/site_customization_helper.rb @@ -17,4 +17,8 @@ module SiteCustomizationHelper false end end + + def information_texts_tabs + [:basic, :debates, :community, :proposals, :polls, :layouts, :mailers, :management, :welcome] + end end diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb index 4fe3db4c7..79a04b20a 100644 --- a/app/models/i18n_content.rb +++ b/app/models/i18n_content.rb @@ -46,16 +46,54 @@ class I18nContent < ApplicationRecord end def self.content_for(tab) - flat_hash(translations_for(tab)).keys.map do |string| + translations_for(tab).map do |string| I18nContent.find_or_initialize_by(key: string) end end def self.translations_for(tab) + if tab.to_s == "basic" + basic_translations + else + flat_hash(translations_hash_for(tab)).keys + end + end + + def self.translations_hash_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 + + def self.basic_translations + %w[ + debates.index.section_footer.title + debates.index.section_footer.description + debates.index.section_footer.help_text_1 + debates.index.section_footer.help_text_2 + debates.new.info + debates.new.info_link + debates.new.more_info + debates.new.recommendation_one + debates.new.recommendation_two + debates.new.recommendation_three + debates.new.recommendation_four + debates.new.recommendations_title + proposals.index.section_footer.title + proposals.index.section_footer.description + proposals.new.more_info + proposals.new.recommendation_one + proposals.new.recommendation_two + proposals.new.recommendation_three + proposals.new.recommendations_title + polls.index.section_footer.title + polls.index.section_footer.description + legislation.processes.index.section_footer.title + legislation.processes.index.section_footer.description + budgets.index.section_footer.title + budgets.index.section_footer.description + ] + end end diff --git a/app/views/admin/site_customization/information_texts/_tabs.html.erb b/app/views/admin/site_customization/information_texts/_tabs.html.erb index 0a459d139..d3d195503 100644 --- a/app/views/admin/site_customization/information_texts/_tabs.html.erb +++ b/app/views/admin/site_customization/information_texts/_tabs.html.erb @@ -1,5 +1,5 @@