123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Fields::ForeignKey

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/foreign_key.rb

Overview

Represents a ::BSON document field definition which stores a foreign key that references the ID of another document. Used for association behavior.

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

#foreign_key?true | false (readonly)

Is this field a foreign key?

Examples:

Is the field a foreign key?

field.foreign_key?

Returns:

  • (true | false)

    If the field is a foreign key.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 52

def foreign_key?
  true
end

#lazy?true | false (readonly)

Does this field do lazy default evaluation?

Examples:

Is the field lazy?

field.lazy?

Returns:

  • (true | false)

    If the field is lazy.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 84

def lazy?
  type.resizable?
end

#object_id_field?true | false (readonly)

Is the field a BSON::ObjectId?

Examples:

Is the field a BSON::ObjectId?

field.object_id_field?

Returns:

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 110

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

#resizable?true | false (readonly)

Returns true if an array, false if not.

Examples:

Is the field resizable?

field.resizable?

Returns:

  • (true | false)

    If the field is resizable.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 121

def resizable?
  type.resizable?
end

Instance Method Details

#add_atomic_changes(document, name, key, mods, new_elements, old_elements)

Adds the atomic changes for this type of resizable field.

@todo: Refactor, big time.

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_elements (Array)

    The new elements to add.

  • old_elements (Array)

    The old elements getting removed.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 25

def add_atomic_changes(document, name, key, mods, new_elements, old_elements)
  old = (old_elements || [])
  new = (new_elements || [])
  if new.length > old.length
    if new.first(old.length) == old
      document.atomic_array_add_to_sets[key] = new.drop(old.length)
    else
      mods[key] = document.attributes[name]
    end
  elsif new.length < old.length
    pulls = old - new
    if new == old - pulls
      document.atomic_array_pulls[key] = pulls
    else
      mods[key] = document.attributes[name]
    end
  elsif new != old
    mods[key] = document.attributes[name]
  end
end

#evaluate_default_proc(doc) ⇒ Object (private)

Evaluate the default proc. In some cases we need to instance exec, in others we don’t.

Examples:

Eval the default proc.

field.evaluate_default_proc(band)

Parameters:

Returns:

  • (Object)

    The called proc.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 158

def evaluate_default_proc(doc)
  serialize_default(default_val[])
end

#evolve(object) ⇒ Object

Evolve the object into an id compatible object.

Examples:

Evolve the object.

field.evolve(object)

Parameters:

  • object (Object)

    The object to evolve.

Returns:

  • (Object)

    The evolved object.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 64

def evolve(object)
  if object_id_field? || object.is_a?(Document)
    if association.polymorphic?
      association.convert_to_foreign_key(object)
    elsif object.is_a?(Document) && object.respond_to?(association.primary_key)
      primary_key_field.evolve(object.send(association.primary_key))
    else
      object.__evolve_object_id__
    end
  else
    related_id_field.evolve(object)
  end
end

#mongoize(object) ⇒ Object

Mongoize the object into the Mongo friendly value.

Examples:

Mongoize the object.

field.mongoize(object)

Parameters:

  • object (Object)

    The object to Mongoize.

Returns:

  • (Object)

    The mongoized object.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 96

def mongoize(object)
  if type.resizable? || object_id_field?
    mongoize_foreign_key(object)
  else
    related_id_field.mongoize(object)
  end
end

#mongoize_foreign_key(object) ⇒ Object (private)

Convert the provided object to a Mongo-friendly foreign key.

Examples:

Convert the object to a foreign key.

mongoize_foreign_key(object)

Parameters:

  • object (Object)

    The object to convert.

Returns:

  • (Object)

    The converted object.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 135

def mongoize_foreign_key(object)
  if type == Array || type == Set
    object = object.to_a if type == Set || object.is_a?(Set)

    if object.resizable?
      object.blank? ? object : association.convert_to_foreign_key(object)
    else
      object.blank? ? [] : association.convert_to_foreign_key(Array(object))
    end
  elsif !(object.nil? || object == '')
    association.convert_to_foreign_key(object)
  end
end

#primary_key_field (private)

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 174

def primary_key_field
  @primary_key_field ||= association.klass.fields[association.primary_key]
end

#serialize_default(object) ⇒ Object (private)

This method is for internal use only.

This is used when default values need to be serialized. Most of the time just return the object.

Examples:

Serialize the default value.

field.serialize_default(obj)

Parameters:

  • object (Object)

    The default.

Returns:

  • (Object)

    The serialized default.

[ GitHub ]

  
# File 'lib/mongoid/fields/foreign_key.rb', line 189

def serialize_default(object); object; end