Class: Mongoid::Relations::Referenced::In

Inherits:
One show all
Defined in:
lib/mongoid/relations/referenced/in.rb

Overview

This class handles all behaviour for relations that are either one-to-many or one-to-one, where the foreign key is store on this side of the relation and the reference is to document(s) in another collection.

Instance Attribute Summary

Attributes inherited from Proxy

#base, #loaded, #metadata, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from One

#in_memory, #respond_to?

Methods inherited from Proxy

#init, #substitutable

Constructor Details

#initialize(base, target, metadata) ⇒ In

Instantiate a new referenced_in relation.

Examples:

Create the new relation.

Referenced::In.new(game, person, )

Parameters:

  • base (Document)

    The document this relation hangs off of.

  • target (Document, Array<Document>)

    The target (parent) of the relation.

  • metadata (Metadata)

    The relation’s metadata.



21
22
23
24
25
26
# File 'lib/mongoid/relations/referenced/in.rb', line 21

def initialize(base, target, )
  init(base, target, ) do
    characterize_one(target)
    bind_one
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy

Class Method Details

.builder(base, meta, object) ⇒ Builder

Return the builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the builder.

Referenced::In.builder(meta, object)

Parameters:

  • base (Document)

    The base document.

  • meta (Metadata)

    The metadata of the relation.

  • object (Document, Hash)

    A document or attributes to build with.

Returns:

  • (Builder)

    A new builder object.

Since:

  • 2.0.0.rc.1



93
94
95
# File 'lib/mongoid/relations/referenced/in.rb', line 93

def builder(base, meta, object)
  Builders::Referenced::In.new(base, meta, object)
end

.criteria(metadata, object, type = nil) ⇒ Criteria

Get the standard criteria used for querying this relation.

Examples:

Get the criteria.

Proxy.criteria(meta, id, Model)

Parameters:

  • metadata (Metadata)

    The metadata.

  • object (Object)

    The value of the foreign key.

  • type (Class) (defaults to: nil)

    The optional type.

Returns:

Since:

  • 2.1.0



109
110
111
# File 'lib/mongoid/relations/referenced/in.rb', line 109

def criteria(, object, type = nil)
  type.where(:_id => object)
end

.eager_load(metadata, criteria) ⇒ Criteria

Get the criteria that is used to eager load a relation of this type.

Examples:

Get the eager load criteria.

Proxy.eager_load(, criteria)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • criteria (Criteria)

    The criteria being used.

Returns:

  • (Criteria)

    The criteria to eager load the relation.

Raises:

Since:

  • 2.2.0



125
126
127
128
129
130
131
# File 'lib/mongoid/relations/referenced/in.rb', line 125

def eager_load(, criteria)
  raise Errors::EagerLoad.new(.name) if .polymorphic?
  klass, foreign_key = .klass, .foreign_key
  klass.any_in("_id" => criteria.load_ids(foreign_key).uniq).each do |doc|
    IdentityMap.set(doc)
  end
end

.embedded?false

Returns true if the relation is an embedded one. In this case always false.

Examples:

Is this relation embedded?

Referenced::In.embedded?

Returns:

  • (false)

    Always false.

Since:

  • 2.0.0.rc.1



142
143
144
# File 'lib/mongoid/relations/referenced/in.rb', line 142

def embedded?
  false
end

.foreign_key_defaultnil

Get the default value for the foreign key.

Examples:

Get the default.

Referenced::In.foreign_key_default

Returns:

  • (nil)

    Always nil.

Since:

  • 2.0.0.rc.1



154
155
156
# File 'lib/mongoid/relations/referenced/in.rb', line 154

def foreign_key_default
  nil
end

.foreign_key_suffixString

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Referenced::In.foreign_key_suffix

Returns:

Since:

  • 2.0.0.rc.1



166
167
168
# File 'lib/mongoid/relations/referenced/in.rb', line 166

def foreign_key_suffix
  "_id"
end

.macroSymbol

Returns the macro for this relation. Used mostly as a helper in reflection.

Examples:

Get the macro.

Referenced::In.macro

Returns:



177
178
179
# File 'lib/mongoid/relations/referenced/in.rb', line 177

def macro
  :referenced_in
end

.nested_builder(metadata, attributes, options) ⇒ NestedBuilder

Return the nested builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the nested builder.

Referenced::In.builder(attributes, options)

Parameters:

  • metadata (Metadata)

    The relation metadata.

  • attributes (Hash)

    The attributes to build with.

  • options (Hash)

    The options for the builder.

Options Hash (options):

  • :allow_destroy (true, false)

    Can documents be deleted?

  • :limit (Integer)

    Max number of documents to create at once.

  • :reject_if (Proc, Symbol)

    If documents match this option then they are ignored.

  • :update_only (true, false)

    Only existing documents can be modified.

Returns:

  • (NestedBuilder)

    A newly instantiated nested builder object.

Since:

  • 2.0.0.rc.1



203
204
205
# File 'lib/mongoid/relations/referenced/in.rb', line 203

def nested_builder(, attributes, options)
  Builders::NestedAttributes::One.new(, attributes, options)
end

.path(document) ⇒ Root

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Parameters:

  • document (Document)

    The document to calculate on.

Returns:

  • (Root)

    The root atomic path calculator.

Since:

  • 2.1.0



217
218
219
# File 'lib/mongoid/relations/referenced/in.rb', line 217

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

.stores_foreign_key?true

Tells the caller if this relation is one that stores the foreign key on its own objects.

Examples:

Does this relation store a foreign key?

Referenced::In.stores_foreign_key?

Returns:

  • (true)

    Always true.

Since:

  • 2.0.0.rc.1



230
231
232
# File 'lib/mongoid/relations/referenced/in.rb', line 230

def stores_foreign_key?
  true
end

.valid_optionsArray<Symbol>

Get the valid options allowed with this relation.

Examples:

Get the valid options.

Relation.valid_options

Returns:

Since:

  • 2.1.0



242
243
244
# File 'lib/mongoid/relations/referenced/in.rb', line 242

def valid_options
  [ :autosave, :foreign_key, :index, :polymorphic ]
end

.validation_defaulttrue, false

Get the default validation setting for the relation. Determines if by default a validates associated will occur.

Examples:

Get the validation default.

Proxy.validation_default

Returns:

  • (true, false)

    The validation default.

Since:

  • 2.1.9



255
256
257
# File 'lib/mongoid/relations/referenced/in.rb', line 255

def validation_default
  false
end

Instance Method Details

#substitute(replacement) ⇒ In?

Substitutes the supplied target documents for the existing document in the relation.

Examples:

Substitute the relation.

name.substitute(new_name)

Parameters:

  • new_target (Document, Array<Document>)

    The replacement.

  • building (true, false)

    Are we in build mode?

Returns:

  • (In, nil)

    The relation or nil.

Since:

  • 2.0.0.rc.1



40
41
42
43
44
45
46
47
# File 'lib/mongoid/relations/referenced/in.rb', line 40

def substitute(replacement)
  tap do |proxy|
    proxy.unbind_one
    return nil unless replacement
    proxy.target = replacement
    proxy.bind_one
  end
end