Class: Mobility::Backends::ActiveRecord::Serialized

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

Overview

Implements Serialized backend for ActiveRecord models.

Examples:

Define attribute with serialized backend

class Post < ActiveRecord::Base
  extend Mobility
  translates :title, backend: :serialized, format: :yaml
end

Read and write attribute translations

post = Post.create(title: "foo")
post.title
#=> "foo"
Mobility.locale = :ja
post.title = "あああ"
post.save
post.read_attribute(:title)      # get serialized value
#=> {:en=>"foo", :ja=>"あああ"}

Backend Configuration collapse

Cache Methods collapse

Class Method Summary collapse

Methods included from Mobility::Backends::ActiveRecord

included

Class Method Details

.build_node(attr, _locale) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.build_node(attr, _locale)
  raise ArgumentError,
    "You cannot query on mobility attributes translated with the Serialized backend (#{attr})."
end

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • format (Symbol) — default: :yaml

    Serialization format

Raises:

  • (ArgumentError)

    if a format other than :yaml or :json is passed in



41
42
43
44
# File 'lib/mobility/backends/active_record/serialized.rb', line 41

def self.configure(options)
  super
  Serialized.configure(options)
end

.valid_keysObject



33
34
35
# File 'lib/mobility/backends/active_record/serialized.rb', line 33

def self.valid_keys
  super + [:format]
end

Instance Method Details

#translationsHash

Returns column value as a hash

Returns:



66
67
68
# File 'lib/mobility/backends/active_record/serialized.rb', line 66

def translations
  model.read_attribute(column_name)
end