module Mobility::Backends::KeyValue

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, {Mobility::ActiveRecord::TextTranslation} or {Mobility::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).

@see Mobility::Backends::ActiveRecord::KeyValue @see Mobility::Backends::Sequel::KeyValue

Private Class Methods

included(backend_class) click to toggle source
# File lib/mobility/backends/key_value.rb, line 78
def self.included(backend_class)
  backend_class.extend ClassMethods
  backend_class.option_reader :association_name
  backend_class.option_reader :class_name
  backend_class.option_reader :key_column
  backend_class.option_reader :value_column
  backend_class.option_reader :belongs_to
end

Public Instance Methods

each_locale() { |locale.to_sym| ... } click to toggle source

@!macro backend_iterator

# File lib/mobility/backends/key_value.rb, line 68
def each_locale
  translations.each { |t| yield(t.locale.to_sym) if t.send(key_column) == attribute }
end
read(locale, **options) click to toggle source

@!group Backend Accessors @!macro backend_reader

# File lib/mobility/backends/key_value.rb, line 57
def read(locale, **options)
  translation_for(locale, **options).send(value_column)
end
write(locale, value, **options) click to toggle source

@!macro backend_writer

# File lib/mobility/backends/key_value.rb, line 62
def write(locale, value, **options)
  translation_for(locale, **options).send(:"#{value_column}=", value)
end

Private Instance Methods

translations() click to toggle source
# File lib/mobility/backends/key_value.rb, line 74
def translations
  model.send(association_name)
end