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)

43
44
45
46
# File 'lib/mobility/backends/active_record/serialized.rb', line 43

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


37
38
39
40
# File 'lib/mobility/backends/active_record/serialized.rb', line 37

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

Instance Method Details

#translationsHash

Returns column value as a hash

Returns:

  • (Hash)

56
57
58
# File 'lib/mobility/backends/active_record/serialized.rb', line 56

def translations
  model.read_attribute(column_name)
end