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

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

Overview

Implements the Container backend for ActiveRecord models.

Defined Under Namespace

Classes: InvalidColumnType

Backend Accessors collapse

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Container

included

Methods included from Mobility::Backends::ActiveRecord

included

Class Method Details

.build_node(attr, locale) ⇒ Mobility::Plugins::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
    Plugins::Arel::Nodes::JsonContainer.new(column, build_quoted(locale), build_quoted(attr))
  when :jsonb
    Plugins::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:



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

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

Instance Method Details

#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_key do |l|
    yield l.to_sym
  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



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

def read(locale, _ = nil)
  locale_translations = model_translations(locale)

  return unless locale_translations

  locale_translations[attribute.to_s]
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



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

def write(locale, value, _ = nil)
  set_attribute_translation(locale, value)
  read(locale)
end