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.present?
        i18n_content.translations.find_by(locale: locale)&.value
      end
    end

    def i18n_text
      t(i18n_content.key, locale: locale)
    end
end

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

Improve performance when editing custom texts
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.
