Class: Mongoid::Fields::Localized

Inherits:
Standard
  • Object
show all
Defined in:
build/mongoid-8.1/lib/mongoid/fields/localized.rb

Instance Attribute Summary

Attributes inherited from Standard

#default_val, #label, #name, #options

Instance Method Summary collapse

Methods inherited from Standard

#add_atomic_changes, #association, #eval_default, #foreign_key?, #initialize, #lazy?, #object_id_field?, #pre_processed?, #type

Constructor Details

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

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.



16
17
18
19
20
21
22
# File 'build/mongoid-8.1/lib/mongoid/fields/localized.rb', line 16

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

#localize_present?true | false

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.



40
41
42
# File 'build/mongoid-8.1/lib/mongoid/fields/localized.rb', line 40

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

#localized?true | false

Is the field localized or not?

Examples:

Is the field localized?

field.localized?

Returns:

  • (true | false)

    If the field is localized.



30
31
32
# File 'build/mongoid-8.1/lib/mongoid/fields/localized.rb', line 30

def localized?
  true
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.



52
53
54
# File 'build/mongoid-8.1/lib/mongoid/fields/localized.rb', line 52

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