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.
This commit is contained in:
Javi Martín
2021-10-05 23:37:20 +02:00
parent c7230e668f
commit 7243ace903

View File

@@ -14,12 +14,12 @@ class Admin::SiteCustomization::InformationTexts::FormFieldComponent < Applicati
end end
def database_text def database_text
if i18n_content.present? if i18n_content.persisted?
i18n_content.translations.find_by(locale: locale)&.value i18n_content.translations.find_by(locale: locale)&.value
end end
end end
def i18n_text def i18n_text
t(i18n_content.key, locale: locale) I18n.translate(i18n_content.key, locale: locale)
end end
end end