Class: Mongoid::Fields::Internal::ForeignKeys::Array

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/mongoid/fields/internal/foreign_keys/array.rb

Overview

Defines the behaviour for array foreign key fields.

Instance Attribute Summary

Attributes included from Serializable

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

Instance Method Summary collapse

Methods included from Serializable

#constraint, #deserialize, #eval_default, #foreign_key?, #localized?, #metadata, #selection, #type, #versioned?

Instance Method Details

#add_atomic_changes(document, key, mods, new, old) ⇒ Object

Adds the atomic changes for this type of resizable field.

Examples:

Add the atomic changes.

field.add_atomic_changes(doc, "key", {}, [], [])

Parameters:

  • document (Document)

    The document to add to.

  • key (String)

    The name of the field.

  • mods (Hash)

    The current modifications.

  • new (Array)

    The new elements to add.

  • old (Array)

    The old elements getting removed.

Since:

  • 2.4.0



23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 23

def add_atomic_changes(document, key, mods, new, old)
  if new.any? && old.any?
    mods[key] = document.attributes[key]
  elsif new.any?
    document.atomic_array_add_to_sets[key] = new
  elsif old.any?
    document.atomic_array_pulls[key] = old
  end
end

#object_id_field?true, false

Is the field a BSON::ObjectId?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

  • (true, false)

    If the field is a BSON::ObjectId.

Since:

  • 2.2.0



41
42
43
44
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 41

def object_id_field?
  @object_id_field ||=
    .polymorphic? ? true : .klass.using_object_ids?
end

#resizable?true

Array fields are resizable.

Examples:

Is this field resizable?

field.resizable?

Returns:

  • (true)

    Always true.

Since:

  • 2.4.0



54
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 54

def resizable?; true; end

#serialize(object) ⇒ Array

Serialize the object from the type defined in the model to a MongoDB compatible object to store.

Examples:

Serialize the field.

field.serialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Array)

    The converted object.

Since:

  • 2.1.0



67
68
69
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 67

def serialize(object)
  object ? constraint.convert(object) : []
end