From 7243ace903e4cedb7c0340d10317fe21515cc7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 5 Oct 2021 23:37:20 +0200 Subject: [PATCH] Improve performance when editing custom texts We were executing SQL queries searching for translations even for records which were not persisted in the database and therefore had no translations. So we're now only looking for translations when the record is persisted. Note that, when the record isn't persisted, we're using `I18n.translate` instead of the `t` method. That's because the `t` method would also perform database queries since we overwrite the `t` method in the `i18n_translation` initializer. On tabs with many records, like the proposals tab, requests are now up to three times faster on production environments. Tests related to this part of the application were failing sometimes because requests took longer than `Capybara.default_max_wait_time`. After these changes, the custom information texts pages load about 30% faster when running the tests. --- .../information_texts/form_field_component.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/admin/site_customization/information_texts/form_field_component.rb b/app/components/admin/site_customization/information_texts/form_field_component.rb index 8c2233504..c5278a305 100644 --- a/app/components/admin/site_customization/information_texts/form_field_component.rb +++ b/app/components/admin/site_customization/information_texts/form_field_component.rb @@ -14,12 +14,12 @@ class Admin::SiteCustomization::InformationTexts::FormFieldComponent < Applicati end def database_text - if i18n_content.present? + if i18n_content.persisted? i18n_content.translations.find_by(locale: locale)&.value end end def i18n_text - t(i18n_content.key, locale: locale) + I18n.translate(i18n_content.key, locale: locale) end end