Class: Mobility::Backends::ActiveRecord::Container

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backends::ActiveRecord
Defined in:
lib/mobility/backends/active_record/container.rb

Overview

Implements the Container backend for ActiveRecord models.

Defined Under Namespace

Classes: Coder, InvalidColumnType

Backend Accessors collapse

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mobility::Backends::ActiveRecord

included

Class Method Details

.build_node(attr, locale) ⇒ Mobility::Arel::Nodes::Json, Mobility::Arel::Nodes::Jsonb

Returns Arel node for attribute on json or jsonb column.

Parameters:

  • attr (String)

    Attribute name

  • locale (Symbol)

    Locale

Returns:


57
58
59
60
61
62
63
64
65
# File 'lib/mobility/backends/active_record/container.rb', line 57

def build_node(attr, locale)
  column = model_class.arel_table[column_name]
  case column_type
  when :json
    Arel::Nodes::JsonContainer.new(column, build_quoted(locale), build_quoted(attr))
  when :jsonb
    Arel::Nodes::JsonbContainer.new(column, build_quoted(locale), build_quoted(attr))
  end
end

.column_typeObject


67
68
69
# File 'lib/mobility/backends/active_record/container.rb', line 67

def column_type
  @column_type ||= get_column_type
end

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • column_name (Symbol) — default: :translations

    Name of column on which to store translations

Raises:


47
48
49
50
# File 'lib/mobility/backends/active_record/container.rb', line 47

def configure(options)
  options[:column_name] ||= :translations
  options[:column_name] = options[:column_name].to_sym
end

Instance Method Details

#column_nameSymbol

Returns name of json or jsonb column used to store translations

Returns:

  • (Symbol)

    (:translations) Name of translations column


18
# File 'lib/mobility/backends/active_record/container.rb', line 18

option_reader :column_name

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

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)

83
84
85
86
87
# File 'lib/mobility/backends/active_record/container.rb', line 83

def each_locale
  model[column_name].each do |l, v|
    yield l.to_sym if v.present?
  end
end

#read(locale, _ = nil) ⇒ String, ...

Note:

Translation may be a string, integer, boolean, hash or array since value is stored on a JSON hash.

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (String, Integer, Boolean)

    Value of translation


27
28
29
# File 'lib/mobility/backends/active_record/container.rb', line 27

def read(locale, _ = nil)
  model_translations(locale)[attribute]
end

#write(locale, value, _ = nil) ⇒ String, ...

Note:

Translation may be a string, integer, boolean, hash or array since value is stored on a JSON hash.

Returns Updated value.

Parameters:

  • locale (Symbol)

    Locale to write

  • value (String, Integer, Boolean)

    Value to write

  • options (Hash)

Returns:

  • (String, Integer, Boolean)

    Updated value


37
38
39
40
# File 'lib/mobility/backends/active_record/container.rb', line 37

def write(locale, value, _ = nil)
  set_attribute_translation(locale, value)
  model_translations(locale)[attribute]
end