Class: Mongoid::Fields::Internal::Localized
- Includes:
- Serializable
- Defined in:
- lib/mongoid/fields/internal/localized.rb
Overview
Defines the behaviour for localized string fields.
Instance Attribute Summary
Attributes included from Serializable
#default_val, #label, #localize, #name, #options
Instance Method Summary collapse
-
#deserialize(object) ⇒ String
Deserialize the object based on the current locale.
-
#selection(object) ⇒ Object
Special case to serialize the object.
-
#serialize(object) ⇒ Hash
Convert the provided string into a hash for the locale.
Methods included from Serializable
#constraint, #eval_default, #foreign_key?, #localized?, #metadata, #object_id_field?, #resizable?, #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.
21 22 23 24 25 26 27 28 29 |
# File 'lib/mongoid/fields/internal/localized.rb', line 21 def deserialize(object) return nil if object.nil? locale = ::I18n.locale if ::I18n.respond_to?(:fallbacks) object[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| object[loc] }] else object[locale.to_s] end end |
#selection(object) ⇒ Object
Special case to serialize the object.
41 42 43 44 |
# File 'lib/mongoid/fields/internal/localized.rb', line 41 def selection(object) return object if object.is_a?(::Hash) serialize(object) end |
#serialize(object) ⇒ Hash
Convert the provided string into a hash for the locale.
56 57 58 |
# File 'lib/mongoid/fields/internal/localized.rb', line 56 def serialize(object) { ::I18n.locale.to_s => object } end |