Class: Mobility::Backend::ActiveRecord::KeyValue

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backend
Defined in:
lib/mobility/backend/active_record/key_value.rb

Overview

Implements the KeyValue backend for ActiveRecord models.

Examples:

class Post < ActiveRecord::Base
  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

Classes: QueryMethods

Instance Attribute Summary collapse

Attributes included from Mobility::Backend

#attribute, #model, #options

Backend Accessors collapse

Backend Configuration collapse

Cache Methods collapse

Instance Method Summary collapse

Methods included from Mobility::Backend

included, method_name

Constructor Details

#initialize(model, attribute, **options) ⇒ KeyValue

Returns a new instance of KeyValue.

Parameters:

  • model

    Model on which backend is defined

  • attribute (String)

    Backend attribute

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • fallbacks (Hash)

    Fallbacks hash

  • association_name (Symbol)

    Name of association



31
32
33
34
# File 'lib/mobility/backend/active_record/key_value.rb', line 31

def initialize(model, attribute, **options)
  super
  @association_name = options[:association_name]
end

Instance Attribute Details

#association_nameSymbol (readonly)

Returns Name of the association.

Returns:

  • (Symbol)

    Name of the association



27
28
29
# File 'lib/mobility/backend/active_record/key_value.rb', line 27

def association_name
  @association_name
end

Class Method Details

.configure!(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • type (Symbol) — default: :text

    Column type to use

  • association_name (Symbol) — default: :mobility_text_translations

    Name of association method

  • class_name (String, Class) — default: {Mobility::ActiveRecord::TextTranslation}

    Translation class

Raises:

  • (ArgumentError)

    if type is not either :text or :string



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mobility/backend/active_record/key_value.rb', line 53

def self.configure!(options)
  options[:type]             ||= :text
  case type = options[:type].to_sym
  when :text, :string
    options[:class_name]     ||= Mobility::ActiveRecord.const_get("#{type.capitalize}Translation")
  else
    raise ArgumentError, "type must be one of: [text, string]"
  end
  options[:class_name] = options[:class_name].constantize if options[:class_name].is_a?(String)
  options[:association_name] ||= options[:class_name].table_name.to_sym
  %i[type association_name].each { |key| options[key] = options[key].to_sym }
end

Instance Method Details

#new_cacheKeyValue::TranslationsCache



102
103
104
# File 'lib/mobility/backend/active_record/key_value.rb', line 102

def new_cache
  KeyValue::TranslationsCache.new(self)
end

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



38
39
40
# File 'lib/mobility/backend/active_record/key_value.rb', line 38

def read(locale, **options)
  translation_for(locale).value
end

#translation_for(locale) ⇒ Mobility::ActiveRecord::TextTranslation, Mobility::ActiveRecord::StringTranslation

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



115
116
117
118
119
# File 'lib/mobility/backend/active_record/key_value.rb', line 115

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

#translationsObject



121
122
123
# File 'lib/mobility/backend/active_record/key_value.rb', line 121

def translations
  model.send(association_name)
end

#write(locale, value, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



43
44
45
# File 'lib/mobility/backend/active_record/key_value.rb', line 43

def write(locale, value, **options)
  translation_for(locale).tap { |t| t.value = value }.value
end

#write_to_cache?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/mobility/backend/active_record/key_value.rb', line 107

def write_to_cache?
  true
end