Class: Mobility::Backends::Sequel::KeyValue

Inherits:
Object
  • Object
show all
Includes:
KeyValue, Mobility::Backends::Sequel, Util
Defined in:
lib/mobility/backends/sequel/key_value.rb

Overview

Note:

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 Classes: CacheRequired

Constant Summary

Constants included from Util

Util::VALID_CONSTANT_NAME_REGEXP

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

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::Backend::OrmDelegator

#for

Methods included from Mobility::Backends::Sequel

included

Class Method Details

.build_op(attr, locale) ⇒ Object


45
46
47
# File 'lib/mobility/backends/sequel/key_value.rb', line 45

def build_op(attr, locale)
  ::Mobility::Sequel::SQL::QualifiedIdentifier.new(table_alias(attr, locale), :value, locale, self, attribute_name: attr)
end

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • type (Symbol, String)

    Column type to use

  • associaiton_name (Symbol) — default: :<type>_translations

    Name of association method, defaults to <type>_translations

  • class_name (Symbol)

    Translation class, defaults to Mobility::<ORM>::<type>Translation

Raises:

  • (CacheRequired)

    if cache is disabled

  • (ArgumentError)

    if type is not set, and both class_name and association_name are also not set


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mobility/backends/sequel/key_value.rb', line 32

def configure(options)
  raise CacheRequired, "Cache required for Sequel::KeyValue backend" if options[:cache] == false
  super
  if type = options[:type]
    options[:association_name] ||= :"#{options[:type]}_translations"
    options[:class_name]       ||= Mobility::Sequel.const_get("#{type.capitalize}Translation")
  end
  options[:table_alias_affix] = "#{options[:model_class]}_%s_#{options[: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.

Parameters:

  • dataset (Sequel::Dataset)

    Dataset to prepare

  • predicate (Object)

    Predicate

  • locale (Symbol)

    Locale

Returns:

  • (Sequel::Dataset)

    Prepared dataset


53
54
55
56
57
# File 'lib/mobility/backends/sequel/key_value.rb', line 53

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_translationsObject

Saves translation which have been built and which have non-blank values.


175
176
177
178
179
180
# File 'lib/mobility/backends/sequel/key_value.rb', line 175

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::Sequel::TextTranslation, Mobility::Sequel::StringTranslation

Returns translation for a given locale, or initializes one if none is present.


168
169
170
171
172
# File 'lib/mobility/backends/sequel/key_value.rb', line 168

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