123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Fields::Localized

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Standard, Forwardable
Instance Chain:
self, Standard
Inherits: Mongoid::Fields::Standard
Defined in: lib/mongoid/fields/localized.rb

Overview

Represents a ::BSON document field definition which stores different values for different user locale keys in a Ruby hashmap (BSON “Object” type). Used for internationalization (I18n) support.

Class Method Summary

Standard - Inherited

.new

Create the new field with a name and optional additional options.

Instance Attribute Summary

Standard - Inherited

#default_val

Defines the behavior for defined fields in the document.

#foreign_key?

Is this field a foreign key?

#label

Defines the behavior for defined fields in the document.

#lazy?

Does this field do lazy default evaluation?

#localize_present?

Is the localized field enforcing values to be present?

#localized?

Is the field localized or not?

#name

Defines the behavior for defined fields in the document.

#object_id_field?

Is the field a BSON::ObjectId?

#options

Defines the behavior for defined fields in the document.

#pre_processed?

Does the field pre-process its default value?

Instance Method Summary

Standard - Inherited

#add_atomic_changes

Adds the atomic changes for this type of resizable field.

#association

Get the metadata for the field if its a foreign key.

#eval_default

Evaluate the default value and return it.

#type

Get the type of this field - inferred from the class name.

#default_name

Get the name of the default method for this field.

#define_default_method

Define the method for getting the default on the document.

#evaluate_default_proc

Evaluate the default proc.

#evaluated_default

Get the evaluated default.

#included?

Is the field included in the fields that were returned from the database? We can apply the default if:

#serialize_default

This is used when default values need to be serialized.

Constructor Details

This class inherits a constructor from Mongoid::Fields::Standard

Instance Attribute Details

#fallbacks?true | false (readonly, private)

This method is for internal use only.

Are fallbacks being used for this localized field.

Examples:

Should fallbacks be used.

field.fallbacks?

Returns:

  • (true | false)

    If fallbacks should be used.

[ GitHub ]

  
# File 'lib/mongoid/fields/localized.rb', line 72

def fallbacks?
  return true if options[:fallbacks].nil?
  !!options[:fallbacks]
end

#localize_present?true | false (readonly)

Is the localized field enforcing values to be present?

Examples:

Is the localized field enforcing values to be present?

field.localize_present?

Returns:

  • (true | false)

    If the field enforces present.

[ GitHub ]

  
# File 'lib/mongoid/fields/localized.rb', line 46

def localize_present?
  options[:localize] == :present
end

#localized?true | false (readonly)

Is the field localized or not?

Examples:

Is the field localized?

field.localized?

Returns:

  • (true | false)

    If the field is localized.

[ GitHub ]

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

def localized?
  true
end

Instance Method Details

#demongoize(object) ⇒ Object

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

Examples:

Get the demongoized value.

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

Parameters:

  • object (Hash)

    The hash of translations.

Returns:

  • (Object)

    The value for the current locale.

[ GitHub ]

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

def demongoize(object)
  return if object.nil?
  case object
  when Hash
    type.demongoize(lookup(object))
  end
end

#lookup(object) ⇒ Object (private)

This method is for internal use only.

Lookup the value from the provided object.

Examples:

Lookup the value.

field.lookup({ "en" => "test" })

Parameters:

  • object (Hash)

    The localized object.

Returns:

  • (Object)

    The object for the locale.

[ GitHub ]

  
# File 'lib/mongoid/fields/localized.rb', line 87

def lookup(object)
  locale = ::I18n.locale

  value = if object.key?(locale.to_s)
    object[locale.to_s]
  elsif object.key?(locale)
    object[locale]
  end
  return value unless value.nil?
  if fallbacks? && ::I18n.respond_to?(:fallbacks)
    fallback_key = ::I18n.fallbacks[locale].find do |loc|
      object.key?(loc.to_s) || object.key?(loc)
    end
    object[fallback_key.to_s] || object[fallback_key]
  end
end

#mongoize(object) ⇒ Hash

Convert the provided string into a hash for the locale.

Examples:

Serialize the value.

field.mongoize("testing")

Parameters:

  • object (String)

    The string to convert.

Returns:

  • (Hash)

    The locale with string translation.

[ GitHub ]

  
# File 'lib/mongoid/fields/localized.rb', line 58

def mongoize(object)
  { ::I18n.locale.to_s => type.mongoize(object) }
end