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

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

Overview

Implements the Table backend for Sequel models.

Defined Under Namespace

Modules: Cache Classes: CacheRequired, QueryMethods

Constant Summary

Constants included from Util

Util::VALID_CONSTANT_NAME_REGEXP

Instance Attribute Summary collapse

Attributes included from Table

#association_name

Backend Configuration collapse

Instance Method Summary collapse

Methods included from Util

#blank?, #camelize, #constantize, #demodulize, #foreign_key, included, #presence, #present?, #singularize, #underscore

Methods included from Table

#each_locale, #read, #write

Methods included from Mobility::Backend::OrmDelegator

#for

Methods included from Mobility::Backends::Sequel

included, #setup_query_methods

Constructor Details

#initialize(model, attribute, options = {}) ⇒ Table

Returns a new instance of Table.

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute



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

def initialize(model, attribute, options = {})
  super
  @translation_class = options[:model_class].const_get(options[:subclass_name])
end

Instance Attribute Details

#translation_classSymbol (readonly)

Returns class for translations.

Returns:

  • (Symbol)

    class for translations



21
22
23
# File 'lib/mobility/backends/sequel/table.rb', line 21

def translation_class
  @translation_class
end

Class Method Details

.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:



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

def self.configure(options)
  raise CacheRequired, "Cache required for Sequel::Table backend" if options[:cache] == false
  table_name = options[:model_class].table_name
  options[:table_name]  ||= :"#{singularize(table_name)}_translations"
  options[:foreign_key] ||= foreign_key(camelize(singularize(table_name.to_s.downcase)))
  if association_name = options[:association_name]
    options[:subclass_name] ||= camelize(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

Instance Method Details

#translation_for(locale, _) ⇒ Object



91
92
93
94
95
# File 'lib/mobility/backends/sequel/table.rb', line 91

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