Module: Mobility::Backends::Table

Extended by:
Mobility::Backend::OrmDelegator
Included in:
ActiveRecord::Table, Sequel::Table
Defined in:
lib/mobility/backends/table.rb

Overview

Stores attribute translation as rows on a model-specific translation table (similar to Globalize). By default, the table name for a model Post with table posts will be post_translations, and the translation class will be Post::Translation. The translation class is dynamically created when the backend is initialized on the model class, and subclasses ActiveRecord::ModelTranslation (for AR models) or inherits Sequel::ModelTranslation (for Sequel models).

The backend expects the translations table (+post_translations+) to have:

  • a string column named locale to store the locale of the translation
  • columns for each translated attribute that uses the table (in general, this will be all attributes of the model)
  • an integer column with name post_id (where post is the name of the model class)

If you are using Rails, you can use the mobility:translations generator to create a migration generating this table with:

rails generate mobility:translations post title:string content:text

Unlike Globalize, attributes need not all be on one table. Mobility supports any number of translation tables for a given model class (all of the structure described above), provided the association_name option is different for each. Some translations can be stored on one translation table, others on another, and Mobility will handle mapping reads/writes to each. The subclass used in this case will be generated from the association_name by singularizing it and converting it to camelcase.

For more details, see examples in ActiveRecord::Table.

==Backend Options

===+association_name+

Name of association on model. Defaults to :translations. 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).

===+table_name+

Name of translations table. By default, if the table used by the model is posts, the table name used for translations will be post_translations.

===+foreign_key+

Foreign key to use in defining the association on the model. By default, if the model is a Post, this will be post_id. Generally this does not need to be set.

===+subclass_name+

Subclass to use when dynamically generating translation class for model, by default :Translation. Should be a symbol. Generally this does not need to be set.

Defined Under Namespace

Modules: Cache, ClassMethods

Instance Attribute Summary collapse

Backend Accessors collapse

Instance Method Summary collapse

Methods included from Mobility::Backend::OrmDelegator

for

Instance Attribute Details

#association_nameSymbol (readonly)

Returns name of the association method.

Returns:

  • (Symbol)

    name of the association method



70
71
72
# File 'lib/mobility/backends/table.rb', line 70

def association_name
  @association_name
end

Instance Method Details

#each_locale {|Locale| ... } ⇒ Object

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)


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

def each_locale
  translations.each { |t| yield t.locale.to_sym }
end

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

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • association_name (Symbol)

    Name of association



74
75
76
77
# File 'lib/mobility/backends/table.rb', line 74

def initialize(model, attribute, options = {})
  super
  @association_name = options[:association_name]
end

#read(locale, options = {}) ⇒ Object

Gets the translated value for provided locale from configured backend.

Parameters:

  • locale (Symbol)

    Locale to read

Returns:

  • (Object)

    Value of translation



81
82
83
# File 'lib/mobility/backends/table.rb', line 81

def read(locale, options = {})
  translation_for(locale, options).send(attribute)
end

#write(locale, value, options = {}) ⇒ Object

Updates translation for provided locale without calling backend's methods to persist the changes.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

Returns:

  • (Object)

    Updated value



86
87
88
# File 'lib/mobility/backends/table.rb', line 86

def write(locale, value, options = {})
  translation_for(locale, options).send("#{attribute}=", value)
end