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
-
#read(locale, **options) ⇒ Object
Value of translation.
-
#write(locale, value, **options) ⇒ Object
Updated value.
Class Method Summary collapse
-
.column_name_for(attribute, locale = Mobility.locale) ⇒ String
Returns name of column where translated attribute is stored.
Instance Method Summary collapse
-
#column(locale = Mobility.locale) ⇒ String
Returns name of column where translated attribute is stored.
Methods included from OrmDelegator
Class Method Details
.column_name_for(attribute, locale = Mobility.locale) ⇒ String
Returns name of column where translated attribute is stored
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
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.
25 26 27 |
# File 'lib/mobility/backend/column.rb', line 25 def read(locale, **) model.send(column(locale)) end |
#write(locale, value, **options) ⇒ Object
Returns Updated value.
30 31 32 |
# File 'lib/mobility/backend/column.rb', line 30 def write(locale, value, **) model.send("#{column(locale)}=", value) end |