Class: Mongoid::Fields::ForeignKey

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

Instance Attribute Summary

Attributes inherited from Standard

#default_val, #label, #name, #options

Instance Method Summary collapse

Methods inherited from Standard

#association, #eval_default, #initialize, #localized?, #pre_processed?, #type

Constructor Details

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

Instance Method Details

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

Adds the atomic changes for this type of resizable field.

@todo: Durran: 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.

Since:

  • 2.4.0



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'build/mongoid-7.3/lib/mongoid/fields/foreign_key.rb', line 23

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

#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.

Since:

  • 3.0.0



66
67
68
69
70
71
72
73
74
75
76
# File 'build/mongoid-7.3/lib/mongoid/fields/foreign_key.rb', line 66

def evolve(object)
  if object_id_field? || object.is_a?(Document)
    if association.polymorphic?
      association.convert_to_foreign_key(object)
    else
      object.__evolve_object_id__
    end
  else
    related_id_field.evolve(object)
  end
end

#foreign_key?true, false

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.

Since:

  • 2.4.0



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

def foreign_key?
  true
end

#lazy?true, false

Does this field do lazy default evaluation?

Examples:

Is the field lazy?

field.lazy?

Returns:

  • (true, false)

    If the field is lazy.

Since:

  • 3.1.0



86
87
88
# File 'build/mongoid-7.3/lib/mongoid/fields/foreign_key.rb', line 86

def lazy?
  type.resizable?
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.

Since:

  • 3.0.0



100
101
102
103
104
105
106
# File 'build/mongoid-7.3/lib/mongoid/fields/foreign_key.rb', line 100

def mongoize(object)
  if type.resizable? || object_id_field?
    type.__mongoize_fk__(association, object)
  else
    related_id_field.mongoize(object)
  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



116
117
118
119
# File 'build/mongoid-7.3/lib/mongoid/fields/foreign_key.rb', line 116

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

#resizable?true, false

Returns true if an array, false if not.

Examples:

Is the field resizable?

field.resizable?

Returns:

  • (true, false)

    If the field is resizable.

Since:

  • 3.0.2



129
130
131
# File 'build/mongoid-7.3/lib/mongoid/fields/foreign_key.rb', line 129

def resizable?
  type.resizable?
end