Module: Pageflow::TranslatedAttributes Private

Extended by:
ActiveSupport::Concern
Included in:
Site
Defined in:
app/models/concerns/pageflow/translated_attributes.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Store per locale overrides for a set of attributes in an attribute_translations column. The attribute itself keeps holding the value used for locales without translation. Since that value is read via the attribute's reader, overriding the reader also changes the value locales without translation fall back to.

Not to be confused with Translatable, which groups entries that are translations of each other.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attribute_translationsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'app/models/concerns/pageflow/translated_attributes.rb', line 40

def attribute_translations
  self[:attribute_translations] || {}
end

#translated(attribute, locale:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the translation for the given locale, or the value of the attribute itself if there is none. The fallback goes through the attribute's reader, so models can override it to compute a default. Such an override must not call translated itself, since that would recurse.



49
50
51
52
53
54
# File 'app/models/concerns/pageflow/translated_attributes.rb', line 49

def translated(attribute, locale:)
  locale_chain(locale)
    .lazy
    .map { |candidate| attribute_translations.dig(candidate, attribute.to_s) }
    .find(&:present?) || public_send(attribute)
end