Class: Mobility::Backends::Sequel::Table

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

Overview

Implements the Table backend for Sequel models.

Defined Under Namespace

Modules: Cache, Translation Classes: CacheRequired

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Table

#association_name, #each_locale, #foreign_key, #read, #subclass_name, #table_name, #write

Methods included from Mobility::Backends::Sequel

included

Class Method Details

.build_op(attr, locale) ⇒ Sequel::SQL::QualifiedIdentifier

Parameters:

  • name (Symbol)

    Attribute name

  • locale (Symbol)

    Locale

Returns:

  • (Sequel::SQL::QualifiedIdentifier)


51
52
53
# File 'lib/mobility/backends/sequel/table.rb', line 51

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

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • association_name (Symbol) — default: :translations

    Name of association method

  • table_name (Symbol)

    Name of translation table

  • foreign_key (Symbol)

    Name of foreign key

  • subclass_name (Symbol)

    Name of subclass to append to model class to generate translation class

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mobility/backends/sequel/table.rb', line 33

def configure(options)
  raise CacheRequired, "Cache required for Sequel::Table backend" if options[:cache] == false
  table_name = Util.singularize(model_class.table_name)
  options[:table_name]  ||= :"#{table_name}_translations"
  options[:foreign_key] ||= Util.foreign_key(Util.camelize(table_name.downcase))
  if association_name = options[:association_name]
    options[:subclass_name] ||= Util.camelize(Util.singularize(association_name))
  else
    options[:association_name] = :translations
    options[:subclass_name] ||= :Translation
  end
  %i[table_name foreign_key association_name subclass_name].each { |key| options[key] = options[key].to_sym }
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



59
60
61
# File 'lib/mobility/backends/sequel/table.rb', line 59

def prepare_dataset(dataset, predicate, locale)
  join_translations(dataset, locale, visit(predicate, locale))
end

.translation_classSymbol

Returns class for translations.

Returns:

  • (Symbol)

    class for translations



23
24
25
# File 'lib/mobility/backends/sequel/table.rb', line 23

def translation_class
  @translation_class ||= model_class.const_get(subclass_name)
end

Instance Method Details

#translation_classObject



17
18
19
# File 'lib/mobility/backends/sequel/table.rb', line 17

def translation_class
  self.class.translation_class
end

#translation_for(locale) ⇒ Object



159
160
161
162
163
# File 'lib/mobility/backends/sequel/table.rb', line 159

def translation_for(locale, **)
  translation = model.send(association_name).find { |t| t.locale == locale.to_s }
  translation ||= translation_class.new(locale: locale)
  translation
end