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

===+association_name+

Name of association on model. Defaults to 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).

===+type+

Currently, either :text or :string is supported. 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.

Defined Under Namespace

Modules: Cache, ClassMethods

Instance Attribute Summary collapse

Backend Accessors collapse

Instance Method Summary collapse

Methods included from Mobility::Backend::OrmDelegator

for

Instance Attribute Details

#association_nameSymbol (readonly)

Returns name of the association.

Returns:

  • (Symbol)

    name of the association



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

def association_name
  @association_name
end

Instance Method Details

#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

#initialize(model, attribute, options = {}) ⇒ Object

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • association_name (Symbol)

    Name of association



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

def initialize(model, attribute, options = {})
  super
  @association_name = options[:association_name]
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

Gets the translated value for provided locale from configured backend.

Parameters:

  • locale (Symbol)

    Locale to read

Returns:

  • (Object)

    Value of translation



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

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