Class: Mobility::Backends::Sequel::KeyValue
- Inherits:
-
Object
- Object
- Mobility::Backends::Sequel::KeyValue
- Includes:
- KeyValue, Mobility::Backends::Sequel, Util
- Defined in:
- lib/mobility/backends/sequel/key_value.rb
Overview
This backend requires the cache to be enabled in order to track and store changed translations, since Sequel does not support build
-type methods on associations like ActiveRecord.
Implements the KeyValue backend for Sequel models.
Defined Under Namespace
Modules: Cache, DestroyKeyValueTranslations, Translation Classes: CacheRequired, QualifiedIdentifier, StringTranslation, TextTranslation
Constant Summary
Constants included from Util
Util::VALID_CONSTANT_NAME_REGEXP
Backend Configuration collapse
Class Method Summary collapse
- .build_op(attr, locale) ⇒ Object
-
.prepare_dataset(dataset, predicate, locale) ⇒ Sequel::Dataset
Prepared dataset.
Instance Method Summary collapse
-
#save_translations ⇒ Object
Saves translation which have been built and which have non-blank values.
-
#translation_for(locale) ⇒ Mobility::Backends::Sequel::KeyValue::TextTranslation, Mobility::Backends::Sequel::KeyValue::StringTranslation
Returns translation for a given locale, or initializes one if none is present.
Methods included from Util
#blank?, #camelize, #constantize, #demodulize, #foreign_key, included, #presence, #present?, #singularize, #underscore
Methods included from KeyValue
#association_name, #class_name, #each_locale, #read, #write
Methods included from Mobility::Backends::Sequel
Class Method Details
.build_op(attr, locale) ⇒ Object
40 41 42 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 40 def build_op(attr, locale) QualifiedIdentifier.new(table_alias(attr, locale), :value, locale, self, attr) end |
.configure(options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 27 def configure() raise CacheRequired, "Cache required for Sequel::KeyValue backend" if [:cache] == false super if type = [:type] [:association_name] ||= :"#{[:type]}_translations" [:class_name] ||= const_get("#{type.capitalize}Translation") end [:table_alias_affix] = "#{model_class}_%s_#{[:association_name]}" rescue NameError raise ArgumentError, "You must define a Mobility::Sequel::#{type.capitalize}Translation class." end |
.prepare_dataset(dataset, predicate, locale) ⇒ Sequel::Dataset
Returns Prepared dataset.
48 49 50 51 52 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 48 def prepare_dataset(dataset, predicate, locale) visit(predicate, locale).inject(dataset) do |ds, (attr, join_type)| join_translations(ds, attr, locale, join_type) end end |
Instance Method Details
#save_translations ⇒ Object
Saves translation which have been built and which have non-blank values.
173 174 175 176 177 178 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 173 def save_translations cache.each_value do |translation| next unless present?(translation.value) translation.id ? translation.save : model.send("add_#{singularize(association_name)}", translation) end end |
#translation_for(locale) ⇒ Mobility::Backends::Sequel::KeyValue::TextTranslation, Mobility::Backends::Sequel::KeyValue::StringTranslation
Returns translation for a given locale, or initializes one if none is present.
166 167 168 169 170 |
# File 'lib/mobility/backends/sequel/key_value.rb', line 166 def translation_for(locale, **) translation = model.send(association_name).find { |t| t.key == attribute && t.locale == locale.to_s } translation ||= class_name.new(locale: locale, key: attribute) translation end |