class Mobility::Backends::ActiveRecord::Container

Implements the {Mobility::Backends::Container} backend for ActiveRecord models.

Public Class Methods

build_node(attr, locale) click to toggle source

@param [String] attr Attribute name @param [Symbol] locale Locale @return [Mobility::Plugins::Arel::Nodes::Json,Mobility::Arel::Nodes::Jsonb] Arel

node for attribute on json or jsonb column
# 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_type() click to toggle source
# File lib/mobility/backends/active_record/container.rb, line 64
def column_type
  @column_type ||= get_column_type
end
configure(options) click to toggle source

@!group Backend Configuration @option options [Symbol] column_name (:translations) Name of column on which to store translations @raise [InvalidColumnType] if the type of the container column is not json or jsonb

# 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

Private Class Methods

get_column_type() click to toggle source
# File lib/mobility/backends/active_record/container.rb, line 70
def get_column_type
  model_class.type_for_attribute(options[:column_name].to_s).try(:type).tap do |type|
    unless %i[json jsonb].include? type
      raise InvalidColumnType, "#{options[:column_name]} must be a column of type json or jsonb"
    end
  end
end

Public Instance Methods

each_locale() { |to_sym| ... } click to toggle source

@!macro backend_iterator

# 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) click to toggle source

@!group Backend Accessors

@note Translation may be a string, integer, boolean, hash or array

since value is stored on a JSON hash.

@param [Symbol] locale Locale to read @param [Hash] options @return [String,Integer,Boolean] Value of translation

# File lib/mobility/backends/active_record/container.rb, line 24
def read(locale, _ = nil)
  model_translations(locale)[attribute]
end
write(locale, value, _ = nil) click to toggle source

@note Translation may be a string, integer, boolean, hash or array

since value is stored on a JSON hash.

@param [Symbol] locale Locale to write @param [String,Integer,Boolean] value Value to write @param [Hash] options @return [String,Integer,Boolean] Updated value

# 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

Private Instance Methods

model_translations(locale) click to toggle source
# File lib/mobility/backends/active_record/container.rb, line 109
def model_translations(locale)
  model[column_name][locale] ||= {}
end
set_attribute_translation(locale, value) click to toggle source
# File lib/mobility/backends/active_record/container.rb, line 113
def set_attribute_translation(locale, value)
  translations = model[column_name] || {}
  translations[locale.to_s] ||= {}
  translations[locale.to_s][attribute] = value
  model[column_name] = translations
end