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, name, 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.

  • name (String)

    The name of the field.

  • key (String)

    The atomic location 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



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 24

def add_atomic_changes(document, name, key, mods, new, old)
  pushes = (new || []) - (old || [])
  pulls = (old || []) - (new || [])
  if old.nil?
    mods[key] = pushes
  elsif !pushes.empty? && !pulls.empty?
    mods[key] = document.attributes[name]
  elsif !pushes.empty?
    document.atomic_array_add_to_sets[key] = pushes
  elsif !pulls.empty?
    document.atomic_array_pulls[key] = pulls
  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



46
47
48
49
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 46

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



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

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



72
73
74
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 72

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