Module: Mobility::Backend::Column

Includes:
OrmDelegator
Included in:
ActiveRecord::Column, Sequel::Column
Defined in:
lib/mobility/backend/column.rb

Overview

Stores translated attribute as a column on the model table.

To use this backend, ensure that the model table has columns named <attribute>_<locale> for every locale in I18n.available_locales.

Backend Options

There are no options for this backend. Also, the locale_accessors option will be ignored if set, since it would cause a conflict with column accessors.

Backend Accessors collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OrmDelegator

#for, included

Class Method Details

.column_name_for(attribute, locale = Mobility.locale) ⇒ String

Returns name of column where translated attribute is stored

Parameters:

  • attribute (String)
  • locale (Symbol) (defaults to: Mobility.locale)

Returns:



46
47
48
49
# File 'lib/mobility/backend/column.rb', line 46

def self.column_name_for(attribute, locale = Mobility.locale)
  normalized_locale = locale.to_s.downcase.sub("-", "_")
  "#{attribute}_#{normalized_locale}".to_sym
end

Instance Method Details

#column(locale = Mobility.locale) ⇒ String

Returns name of column where translated attribute is stored

Parameters:

  • locale (Symbol) (defaults to: Mobility.locale)

Returns:



38
39
40
# File 'lib/mobility/backend/column.rb', line 38

def column(locale = Mobility.locale)
  Column.column_name_for(attribute, locale)
end

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



25
26
27
# File 'lib/mobility/backend/column.rb', line 25

def read(locale, **options)
  model.send(column(locale))
end

#write(locale, value, **options) ⇒ Object

Returns Updated value.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (Object)

    Value to write

  • options (Hash)

Returns:



30
31
32
# File 'lib/mobility/backend/column.rb', line 30

def write(locale, value, **options)
  model.send("#{column(locale)}=", value)
end