Class: Mobility::Backend::ActiveRecord::Serialized

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

Overview

Implements Serialized backend for ActiveRecord models.

Examples:

Define attribute with serialized backend

class Post < ActiveRecord::Base
  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=>"あああ"}

Defined Under Namespace

Classes: QueryMethods

Instance Attribute Summary

Attributes included from Mobility::Backend

#attribute, #model, #options

Backend Accessors collapse

Backend Configuration collapse

Cache Methods collapse

Methods included from Mobility::Backend

included, #initialize, method_name

Class Method Details

.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



44
45
46
47
48
# File 'lib/mobility/backend/active_record/serialized.rb', line 44

def self.configure!(options)
  options[:format] ||= :yaml
  options[:format] = options[:format].downcase.to_sym
  raise ArgumentError, "Serialized backend only supports yaml or json formats." unless [:yaml, :json].include?(options[:format])
end

Instance Method Details

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



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

def read(locale, **options)
  translations[locale]
end

#translationsHash Also known as: new_cache

Returns column value as a hash

Returns:

  • (Hash)


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

def translations
  model.read_attribute(attribute)
end

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

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



36
37
38
# File 'lib/mobility/backend/active_record/serialized.rb', line 36

def write(locale, value, **options)
  translations[locale] = value
end

#write_to_cache?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/mobility/backend/active_record/serialized.rb', line 72

def write_to_cache?
  true
end