Module: Mobility::Backends::KeyValue

Extended by:
Mobility::Backend::OrmDelegator
Included in:
ActiveRecord::KeyValue, Sequel::KeyValue
Defined in:
lib/mobility/backends/key_value.rb

Overview

Stores attribute translation as attribute/value pair on a shared translations table, using a polymorphic relationship between a translation class and models using the backend. By default, two tables are assumed to be present supporting string and text translations: a mobility_text_translations table for text-valued translations and a string_translations table for string-valued translations (the only difference being the column type of the value column on the table).

Backend Options

type

Currently, either :text or :string is supported, but any value is allowed as long as a corresponding class_name can be found (see below). Determines which class to use for translations, which in turn determines which table to use to store translations (by default text_translations for text type, string_translations for string type).

class_name

Class to use for translations when defining association. By default, ActiveRecord::TextTranslation or ActiveRecord::StringTranslation for ActiveRecord models (similar for Sequel models). If string is passed in, it will be constantized to get the class.

association_name

Name of association on model. Defaults to <type>_translations, which will typically be either :text_translations (if type is :text) or +:string_translations (if type is :string). If specified, ensure name does not overlap with other methods on model or with the association name used by other backends on model (otherwise one will overwrite the other).

Defined Under Namespace

Modules: Cache, ClassMethods

Backend Accessors collapse

Instance Method Summary collapse

Methods included from Mobility::Backend::OrmDelegator

for

Instance Method Details

#association_nameSymbol

Returns the name of the polymorphic association.

Returns:

  • (Symbol)

    Name of the association


# File 'lib/mobility/backends/key_value.rb', line 49

#class_nameClass

Returns translation class used in polymorphic association.

Returns:

  • (Class)

    Translation class


# File 'lib/mobility/backends/key_value.rb', line 53

#each_locale {|Locale| ... } ⇒ Object

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)

70
71
72
# File 'lib/mobility/backends/key_value.rb', line 70

def each_locale
  translations.each { |t| yield(t.locale.to_sym) if t.key == attribute }
end

#read(locale, options = {}) ⇒ Object

Gets the translated value for provided locale from configured backend.

Parameters:

  • locale (Symbol)

    Locale to read

Returns:

  • (Object)

    Value of translation


59
60
61
# File 'lib/mobility/backends/key_value.rb', line 59

def read(locale, options = {})
  translation_for(locale, options).value
end

#write(locale, value, options = {}) ⇒ Object

Updates translation for provided locale without calling backend's methods to persist the changes.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

Returns:

  • (Object)

    Updated value


64
65
66
# File 'lib/mobility/backends/key_value.rb', line 64

def write(locale, value, options = {})
  translation_for(locale, options).value = value
end