class Mobility::Backends::ActiveRecord::Column
Implements the {Mobility::Backends::Column} backend for ActiveRecord
models.
You can use the mobility:translations
generator to create a migration adding translatable columns to the model table with:
rails generate mobility:translations post title:string
The generated migration will add columns +title_<locale>+ for every locale in Mobility.available_locales
(i.e. I18n.available_locales
). (The generator can be run again to add new attributes or locales.)
@example
class Post < ActiveRecord::Base extend Mobility translates :title, backend: :column end Mobility.locale = :en post = Post.create(title: "foo") post.title #=> "foo" post.title_en #=> "foo"
Public Class Methods
build_node(attr, locale)
click to toggle source
@param [String] attr Attribute name @param [Symbol] locale Locale @return [Arel::Attributes::Attribute] Arel node for translation column
on model table
# File lib/mobility/backends/active_record/column.rb, line 58 def self.build_node(attr, locale) model_class.arel_table[Column.column_name_for(attr, locale)] .extend(Plugins::Arel::MobilityExpressions) end
Public Instance Methods
each_locale() { |l| ... }
click to toggle source
@!macro backend_iterator
# File lib/mobility/backends/active_record/column.rb, line 50 def each_locale available_locales.each { |l| yield(l) if present?(l) } end
read(locale, _ = {})
click to toggle source
@!group Backend
Accessors @!macro backend_reader
# File lib/mobility/backends/active_record/column.rb, line 39 def read(locale, _ = {}) model.read_attribute(column(locale)) end
write(locale, value, _ = {})
click to toggle source
@!macro backend_writer
# File lib/mobility/backends/active_record/column.rb, line 44 def write(locale, value, _ = {}) model.send(:write_attribute, column(locale), value) end
Private Instance Methods
available_locales()
click to toggle source
# File lib/mobility/backends/active_record/column.rb, line 65 def available_locales @available_locales ||= get_column_locales end
get_column_locales()
click to toggle source
# File lib/mobility/backends/active_record/column.rb, line 69 def get_column_locales column_name_regex = /\A#{attribute}_([a-z]{2}(_[a-z]{2})?)\z/.freeze model.class.columns.map do |c| (match = c.name.match(column_name_regex)) && match[1].to_sym end.compact end