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: Coder, 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:



54
55
56
57
58
59
60
61
62
# File 'lib/mobility/backends/active_record/container.rb', line 54

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



64
65
66
# File 'lib/mobility/backends/active_record/container.rb', line 64

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:



44
45
46
47
# File 'lib/mobility/backends/active_record/container.rb', line 44

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

Instance Method Details

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

Yields locales available for this attribute.

Yield Parameters:

  • Locale (Symbol)


80
81
82
83
84
# File 'lib/mobility/backends/active_record/container.rb', line 80

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



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

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



34
35
36
37
# File 'lib/mobility/backends/active_record/container.rb', line 34

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