Class: Mobility::Backends::ActiveRecord::KeyValue

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

Overview

Implements the KeyValue backend for ActiveRecord models.

Examples:

class Post < ApplicationRecord
  extend Mobility
  translates :title, backend: :key_value, association_name: :translations, type: :string
end

post = Post.create(title: "foo")
post.translations
#=> #<ActiveRecord::Associations::CollectionProxy ... >
post.translations.first.value
#=> "foo"
post.translations.first.class
#=> Mobility::ActiveRercord::StringTranslation

Defined Under Namespace

Modules: DestroyKeyValueTranslations Classes: Visitor

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KeyValue

#association_name, #class_name, #each_locale, #read, #write

Methods included from Mobility::Backend::OrmDelegator

#for

Methods included from Mobility::Backends::ActiveRecord

included

Class Method Details

.apply_scope(relation, predicate, locale = Mobility.locale, invert: false) ⇒ ActiveRecord::Relation

Joins translations using either INNER/OUTER join appropriate to the query.

Parameters:

  • relation (ActiveRecord::Relation)

    Relation to scope

  • predicate (Object)

    Arel predicate

  • locale (Symbol) (defaults to: Mobility.locale)

    (Mobility.locale) Locale

  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (ActiveRecord::Relation)

    relation Relation with joins applied (if needed)


63
64
65
66
67
68
69
# File 'lib/mobility/backends/active_record/key_value.rb', line 63

def apply_scope(relation, predicate, locale = Mobility.locale, invert: false)
  visitor = Visitor.new(self, locale)
  visitor.accept(predicate).inject(relation) do |rel, (attr, join_type)|
    join_type &&= ::Arel::Nodes::InnerJoin if invert
    join_translations(rel, attr, locale, join_type)
  end
end

.build_node(attr, locale) ⇒ Mobility::Arel::Attribute

Returns Arel attribute for aliased translation table value column.

Parameters:

  • attr (String)

    Attribute name

  • _locale (Symbol)

    Locale

Returns:


52
53
54
55
# File 'lib/mobility/backends/active_record/key_value.rb', line 52

def build_node(attr, locale)
  aliased_table = class_name.arel_table.alias(table_alias(attr, locale))
  Arel::Attribute.new(aliased_table, :value, locale, self, attribute_name: attr.to_sym)
end

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • type (Symbol, String)

    Column type to use

  • associaiton_name (Symbol) — default: :<type>_translations

    Name of association method, defaults to <type>_translations

  • class_name (Symbol)

    Translation class, defaults to Mobility::<ORM>::<type>Translation

Raises:

  • (ArgumentError)

    if type is not set, and both class_name and association_name are also not set


36
37
38
39
40
41
42
43
44
45
# File 'lib/mobility/backends/active_record/key_value.rb', line 36

def configure(options)
  super
  if type = options[:type]
    options[:association_name] ||= :"#{options[:type]}_translations"
    options[:class_name]       ||= Mobility::ActiveRecord.const_get("#{type.capitalize}Translation")
  end
  options[:table_alias_affix] = "#{options[:model_class]}_%s_#{options[:association_name]}"
rescue NameError
  raise ArgumentError, "You must define a Mobility::ActiveRecord::#{type.capitalize}Translation class."
end

Instance Method Details

#translation_for(locale, _options = {}) ⇒ Mobility::ActiveRecord::TextTranslation, Mobility::ActiveRecord::StringTranslation

Returns translation for a given locale, or builds one if none is present.


198
199
200
201
202
# File 'lib/mobility/backends/active_record/key_value.rb', line 198

def translation_for(locale, _options = {})
  translation = translations.find { |t| t.key == attribute && t.locale == locale.to_s }
  translation ||= translations.build(locale: locale, key: attribute)
  translation
end