Class: Mongoid::Fields::Serializable::Localized

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Fields::Serializable
Defined in:
lib/mongoid/fields/serializable/localized.rb

Overview

Defines the behaviour for localized string fields.

Instance Attribute Summary

Attributes included from Mongoid::Fields::Serializable

#default_val, #label, #localize, #name, #options

Instance Method Summary collapse

Methods included from Mongoid::Fields::Serializable

#constraint, #eval_default, #localized?, #metadata, #object_id_field?, #type, #versioned?

Instance Method Details

#deserialize(object) ⇒ String

Deserialize the object based on the current locale. Will look in the hash for the current locale.

Examples:

Get the deserialized value.

field.deserialize({ "en" => "testing" })

Parameters:

  • object (Hash)

    The hash of translations.

Returns:

  • (String)

    The value for the current locale.

Since:

  • 2.3.0



21
22
23
# File 'lib/mongoid/fields/serializable/localized.rb', line 21

def deserialize(object)
  object[::I18n.locale.to_s]
end

#serialize(object) ⇒ Hash

Convert the provided string into a hash for the locale.

Examples:

Serialize the value.

field.serialize("testing")

Parameters:

  • object (String)

    The string to convert.

Returns:

  • (Hash)

    The locale with string translation.

Since:

  • 2.3.0



35
36
37
# File 'lib/mongoid/fields/serializable/localized.rb', line 35

def serialize(object)
  { ::I18n.locale.to_s => object.try(:to_s) }
end