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
-
#association_name ⇒ Symbol
readonly
Name of the association.
Backend Accessors collapse
-
#read(locale, options = {}) ⇒ Object
Gets the translated value for provided locale from configured backend.
-
#write(locale, value, options = {}) ⇒ Object
Gets the translated value for provided locale from configured backend.
Instance Method Summary collapse
-
#each_locale {|Locale| ... } ⇒ Object
Yields locales available for this attribute.
- #initialize(model, attribute, options = {}) ⇒ Object
Methods included from Mobility::Backend::OrmDelegator
Instance Attribute Details
#association_name ⇒ Symbol (readonly)
Returns 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.
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
52 53 54 55 |
# File 'lib/mobility/backends/key_value.rb', line 52 def initialize(model, attribute, = {}) super @association_name = [:association_name] end |
#read(locale, options = {}) ⇒ Object
Gets the translated value for provided locale from configured backend.
59 60 61 |
# File 'lib/mobility/backends/key_value.rb', line 59 def read(locale, = {}) translation_for(locale, ).value end |
#write(locale, value, options = {}) ⇒ Object
Gets the translated value for provided locale from configured backend.
64 65 66 |
# File 'lib/mobility/backends/key_value.rb', line 64 def write(locale, value, = {}) translation_for(locale, ).value = value end |