Merge pull request #4707 from consul/information_texts_performance

Improve performance when editing custom texts
This commit is contained in:
Javi Martín
2021-10-18 11:58:53 +02:00
committed by GitHub
6 changed files with 49 additions and 18 deletions

View File

@@ -0,0 +1,9 @@
<% globalize(locale) do %>
<%= hidden_field_tag "contents[content_#{i18n_content.key}][id]", i18n_content.key %>
<%= text_area_tag "contents[content_#{i18n_content.key}]values[value_#{locale}]",
text,
rows: 5,
class: "js-globalize-attribute",
style: site_customization_display_translation_style(locale),
data: { locale: locale } %>
<% end %>

View File

@@ -0,0 +1,25 @@
class Admin::SiteCustomization::InformationTexts::FormFieldComponent < ApplicationComponent
attr_reader :i18n_content, :locale
delegate :globalize, :site_customization_display_translation_style, to: :helpers
def initialize(i18n_content, locale:)
@i18n_content = i18n_content
@locale = locale
end
private
def text
database_text || i18n_text
end
def database_text
if i18n_content.persisted?
i18n_content.translations.find_by(locale: locale)&.value
end
end
def i18n_text
I18n.translate(i18n_content.key, locale: locale)
end
end

View File

@@ -7,14 +7,6 @@ module SiteCustomizationHelper
site_customization_enable_translation?(locale) ? "" : "display: none;"
end
def translation_for_locale(content, locale)
if content.present?
I18nContentTranslation.find_by(i18n_content_id: content.id, locale: locale)&.value
else
false
end
end
def information_texts_tabs
[:basic, :debates, :community, :proposals, :polls, :layouts, :mailers, :management, :welcome, :machine_learning]
end

View File

@@ -8,7 +8,7 @@
<% group.each do |content| %>
<b><%= content.key %></b>
<% content.globalize_locales.each do |locale| %>
<%= render "form_field", content: content, locale: locale %>
<%= render Admin::SiteCustomization::InformationTexts::FormFieldComponent.new(content, locale: locale) %>
<% end %>
<% end %>
<% end %>

View File

@@ -1,9 +0,0 @@
<% globalize(locale) do %>
<%= 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),
rows: 5,
class: "js-globalize-attribute",
style: site_customization_display_translation_style(locale),
data: { locale: locale } %>
<% end %>

View File

@@ -0,0 +1,14 @@
require "rails_helper"
describe Admin::SiteCustomization::InformationTexts::FormFieldComponent do
after { I18n.backend.reload! }
it "uses the I18n translation when the record exists without a database translation" do
I18n.backend.store_translations(:en, { testing: "It works!" })
content = I18nContent.create!(key: "testing")
render_inline Admin::SiteCustomization::InformationTexts::FormFieldComponent.new(content, locale: :en)
expect(page).to have_field with: "It works!"
end
end